2016-03-10 64 views
2

我使用彈簧mvc我想上傳圖像到jsp窗體,所以我添加enctype="multipart/form-data"到窗體標籤,但是當我添加此時,modelAttribute值等於在控制器春天mvc當使用enctype =「multipart/form-data」modelAttribute值等於null在控制器

null這是我在jsp頁面形式:

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

這是功能的控制器頭:

 @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所有值都是null。而當我從表單標籤的工作以及erease的enctype="multipart/form-data",但我不能上傳圖片

這是uplaud功能: 公共無效uplaodImages(字符串URL,HttpServletRequest的請求){

 // 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"); 

} 
+1

有你在你的配置中指定了一個MutlipartResolver? –

+0

當我把MutlipartResolver放在配置文件中modelAttribute運行良好,但圖像沒有揚起 – user5993555

+0

那麼你是如何上傳圖像?這是如何映射到你的「聯繫」對象? –

回答

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