我有基于Spring MVC的REST应用程序。试图上传文件。这是代码段。
webmvc-config.xml中
<bean
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver">
<property name="maxUploadSize" value="2000000" />
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0" />
<property name="defaultContentType">
<ref bean="htmlMediaType" />
</property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="file" value="multipart/form-data"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.castor.CastorMarshaller" />
</property>
</bean>
</list>
</property>
FileUploadController.java
@Controller
@RequestMapping("/appfiles")
public class FileUploadController {
private String saveDirectory = "C:/Downloads/";
@RequestMapping(value = "/appfiles", method = RequestMethod.POST, consumes="multipart/form-data", produces="application/json")
public ResponseEntity<List<Map<String, String>>> save(
@RequestParam("file") MultipartFile file) {
System.out.println("received file with original filename: " + file.getOriginalFilename());
// List<MultipartFile> files = uploadForm.getFiles();
List<Map<String, String>> response = new ArrayList<Map<String, String>>();
Map<String, String> responseMap = new HashMap<String, String>();
List<String> fileNames = new ArrayList<String>();
if (null != file) {
// for (MultipartFile multipartFile : files) {
String fileName = file.getOriginalFilename();
fileNames.add(fileName);
try {
file.transferTo(new File(saveDirectory
+ file.getOriginalFilename()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
responseMap.put("displayText", file.getOriginalFilename());
responseMap.put("fileSize", ""+file.getSize());
response.add(responseMap);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept", "application/json");
return new ResponseEntity<List<Map<String, String>>>(response, httpHeaders, HttpStatus.OK);
}
}
当我使用chrome extension“postman”
时出现以下错误Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163)
org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1020)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:883)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
提交姓名时提供以下详细信息:
Headers
conent-type: multipart/form-data
Form-data:
file=<filename.txt>
Method=POST
您能告诉我我的代码有什么问题吗?
谢谢&amp;此致
答案 0 :(得分:4)
我也面临同样的问题,但是当我尝试没有任何Header值时,它工作正常。