简体   繁体   中英

Spring mvc When using enctype=“multipart/form-data” modelAttribute values equal null in the controller

I use spring mvc I want to uplaod image to jsp form so I add enctype="multipart/form-data" to the form tag but when i add this, modelAttribute values equals null in the controller

This is my form in jsp page:

 <form:form action="saveContact" method="post" modelAttribute="Contacting"   id="container" enctype="multipart/form-data">

This is the header of the function in controller:

     @RequestMapping(value = "/saveContact", method = RequestMethod.POST)
     public ModelAndView saveContact(@ModelAttribute ("Contacting") Contacting Contacting,ModelAndView modelndView,HttpServletRequest request ,HttpServletResponse response
) throws Exception {............}

@ModelAttribute ("Contacting") Contacting Contacting all values are null . and When I erease the enctype="multipart/form-data" from form tag its work well but I cant upload the image

this is the uplaud function: public void uplaodImages(String url,HttpServletRequest request) {

     // configures upload settings
     DiskFileItemFactory factory = new DiskFileItemFactory();
     factory.setSizeThreshold(THRESHOLD_SIZE);

     ServletFileUpload upload = new ServletFileUpload(factory);
     upload.setFileSizeMax(MAX_FILE_SIZE);
     upload.setSizeMax(MAX_REQUEST_SIZE);

     String uuidValue = "";
     FileItem itemFile = null;

     try {
         // parses the request's content to extract file data
         List formItems = upload.parseRequest(request);
         Iterator iter = formItems.iterator();

         // iterates over form's fields to get UUID Value
         while (iter.hasNext()) {
             FileItem item = (FileItem) iter.next();
             if (item.isFormField()) {
                 if (item.getFieldName().equalsIgnoreCase(UUID_STRING)) {
                     uuidValue = item.getString();
                 }
             }
             // processes only fields that are not form fields
             if (!item.isFormField()) {
                 itemFile = item;
             }
         }

         if (itemFile != null) {
             // get item inputstream to upload file into s3 aws

             BasicAWSCredentials awsCredentials = new BasicAWSCredentials(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY);

             AmazonS3 s3client = new AmazonS3Client(awsCredentials);
             try {

                 ObjectMetadata om = new ObjectMetadata();
                 om.setContentLength(itemFile.getSize());
                 om.setContentType("image/png");

                 String ext = FilenameUtils.getExtension(itemFile.getName());
                 String keyName = uuidValue + '.' + ext;

                // s3client.putObject(new PutObjectRequest(S3_BUCKET_NAME,"99/after/img", itemFile,st om));
                // s3client.setObjectAcl(S3_BUCKET_NAME, "99/after/img", CannedAccessControlList.PublicRead);
                 TransferManager tm = new TransferManager(new ProfileCredentialsProvider());        
                System.out.println("Hello");
                // TransferManager processes all transfers asynchronously, 
                // so this call will return immediately.
                Upload upload1 = tm.upload(
                        S3_BUCKET_NAME, url, itemFile.getInputStream(),om);
                System.out.println("Hello2");

                try {
                    // Or you can block and wait for the upload to finish
                    upload1.waitForCompletion();
                    System.out.println("Upload complete.");
                } catch (AmazonClientException amazonClientException) {
                    System.out.println("Unable to upload file, upload was aborted.");
                    amazonClientException.printStackTrace();
                }


             } catch (AmazonServiceException ase) {
                // LOGGER.error(uuidValue + ":error:" + ase.getMessage());

             } catch (AmazonClientException ace) {
                 //LOGGER.error(uuidValue + ":error:" + ace.getMessage());
             }


         } else {
             //LOGGER.error(uuidValue + ":error:" + "No Upload file");
          System.out.println("No Upload file");
         }

     } catch (Exception ex) {
         //LOGGER.error(uuidValue + ":" + ":error: " + ex.getMessage());
      System.out.println(ex.getMessage());
     }
     //LOGGER.info(uuidValue + ":Upload done");
     System.out.println("Upload done");

}
@RequestMapping(value = "/form.html", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
    @RequestParam("file") MultipartFile  file) throws Exception {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM