3
我有基於Spring MVC的REST應用程序。嘗試上傳文件。這是代碼片段。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擴展 「郵差」
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)
postname提交與以下細節進行:
Headers
conent-type: multipart/form-data
Form-data:
file=<filename.txt>
Method=POST
能否請你讓我知道什麼是錯我的代碼?
感謝&問候
你是如何發佈你的數據? – gouki 2013-02-25 10:57:53
我使用postmaster插件作爲我的REST客戶端。我在頭字段中設置了content-type = multipart/form-data和accept = application/json。使用表單數據字段上傳文件。我在form-data中創建了一個名爲「file」的屬性,並在那裏上傳文件。並且在進行「POST」調用時沒有設置其他屬性。 – kalyanvgopal 2013-02-25 12:07:26
這聽起來像是請求缺少表單邊界。你有沒有辦法查看實際的HTTP請求?我有一種感覺,你的要求缺少一些關鍵部分。此外,它可能會幫助嘗試使用虛擬HTML表單進行發佈,只是獲取請求應該看起來像什麼的基線。 – CodeChimp 2013-02-25 13:47:24