我正尝试使用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