我正嘗試使用REST服務設置使用html和Spring 3.0.6的簡單上載。我在線學習了該教程,但MultipartFile參數始終爲空。這裏的配置和代碼:Spring REST MultipartFile文件始終爲空
應用程序的context.xml:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2000000"/>
</bean>
的pom.xml:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
HTML:
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="/site/restServices/artworkUpload/" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
REST控制器:
@POST
@Path("/artworkUpload")
public String uploadFile(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
try {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:uploadSuccess";
} else {
return "redirect:uploadFailure";
}
}
catch (Exception ex)
{
}
return null;
}
我複製了Spring的教程中的例子,但不管我改變了什麼,文件參數都是空的。 「名稱」將在文本框中具有值,但文件將爲空。
我也嘗試使用澤西和我收到該文件的InputStream但FormDataContentDisposition爲空,所以我無法確定文件類型。
這也在Jetty上運行。
我錯過了什麼?
我有這兩個在我的Maven構建路徑(pom.xml中)。我已檢查並將它們複製到lib文件夾。 – 2012-07-06 17:26:04
我更新了我的答案。看看這些變化。 – dimas 2012-07-08 20:11:20
我仍然需要它。謝謝!我正在嘗試你的解決方案。 – 2012-07-12 00:55:54