将文件从表单数据上载到Spring Server时出错

时间:2014-12-03 20:55:18

标签: spring-mvc encode multipart form-data

我正在使用form-data将客户PC上的图片上传到Spring MVC Server。

更新页面:

<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC - Upload File</title>
</head>
<body>
<form id="form1" method="post" action="/upload" enctype="multipart/form-data" accept-charset="utf-8">
<!-- File input -->     
<input name="file" id="file" type="file" /><br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>

这是 Spring Controller

 @RequestMapping(value = "upload", method = RequestMethod.POST)
 public @ResponseBody
 String provaUpdate(MultipartHttpServletRequest request,Principal p,HttpServletResponse response)throws IOException {
 String result="";
 LocalFileManager mLocalFileManager = LocalFileManager.get();
 Iterator<String> iterator = request.getFileNames();
     while(iterator.hasNext())
     {
         System.out.println("iterator.next()="+iterator.next());
     }
         System.out.println("request.getFileMap().isEmpty()??"+request.getFileMap().isEmpty());
       //  mLocalFileManager.saveLocalData(g,g.getPicturesCount(), request.getFile("new").getInputStream());
    return result;
 }

所以,当我运行它时,它只是打印:

  

request.getFileMap().isEmpty()??true

然后,似乎没有上传任何文件,但如果我得到request.getInputStream()我可以写一个TXT文件显示:

------WebKitFormBoundaryWG8vA5PuTFFxPBqK
Content-Disposition: form-data; name="file"; filename="1.jpg"
Content-Type: image/jpeg
�� JFIF      ��ICC_PROFILE   蠠     mntrRGB XYZ ٠   $ acsp                             ��    ӭ    )B9
                               desc  D   ybXYZ     bTRC  Ԡ dmdd     ࠠ ɧXYZ  
h   gTRC  Ԡ lumi  
|   meas  
 //(Symbols)  Long ETC, so the picture is sent !!
------WebKitFormBoundaryWG8vA5PuTFFxPBqK--

所以,似乎图片发送正确,但MultipartHttpServletRequest无法获取文件。

这是我的错误?

2 个答案:

答案 0 :(得分:0)

我认为,您spring configuration xml文件中的条目可能会丢失。

   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- below properties can be configured as per your need -->
        <property name="maxUploadSize" value="5000000" />
        <property name="maxInMemorySize" value="5000000" />
  </bean>

答案 1 :(得分:0)

错误在于我正在与Thymeleaf合作,因此形式不同。

解决了改变:

1-创建一个包含MultipartFile的类,如下所示:

public class Images {
MultipartFile image;
public MultipartFile getImage() {
return image;
}
public void setImage(MultipartFile image) {
this.image = image;
}
}

2-使用Thymeleaf的Spring服务器的正确表单代码:

<form id="myform"  action="#"  th:action="@{/upload}" th:object="${Images}"     method="POST" modelAttribute="Images"  enctype="multipart/form-data">
<input type="file"   th:field="${Images.image}"  name="file"/>
<input type="submit" value="Upload"/>
</form>

最后是正确的Controller方法:

@RequestMapping(value = "upload", method = RequestMethod.POST)
public String addVocabularyValadate( @ModelAttribute("Images") Images images,BindingResult bindingResult,
                    Model model) throws IOException 
{
System.out.println("inputstream Nombre!"+images.getImage().getOriginalFilename());
if(bindingResult.hasFieldErrors() == true)
return "error";
else
return "upload OK!";
}