Java SpringREST多部分文件始终为空
我正在尝试使用REST服务使用html和Spring3.0.6建立一个简单的上传。我在线学习了教程,但MultipartFile参数始终为null。以下是配置和代码: application-context.xml:Java SpringREST多部分文件始终为空,java,spring,model-view-controller,rest,file-upload,Java,Spring,Model View Controller,Rest,File Upload,我正在尝试使用REST服务使用html和Spring3.0.6建立一个简单的上传。我在线学习了教程,但MultipartFile参数始终为null。以下是配置和代码: application-context.xml: <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUpload
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2000000"/>
</bean>
我复制了Spring教程中的示例,但无论我做了什么更改,file参数始终为null。“名称”将在文本框中具有值,但文件将为空
我也尝试过使用Jersey,收到文件的InputStream,但FormDataContentDisposition为null,因此无法确定文件类型
这也是在码头上运行的
我遗漏了什么?我记得我在构建路径中添加了额外的LIB,从而解决了同样的问题:
commons-fileupload-1.2.2.jar
commons-io-2.1.jar
我希望这对你有帮助
编辑。
嗯。我终于有时间讨论这个问题了。首先,为什么要使用标准java特性来构建rest服务(annotations@POST,@Path)?因为对于Spring,最好使用SpringMVC作为REST。互联网上有很多关于这方面的信息。这里有一个特殊的部分。这里也有。关于如何用SpringMVC构建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;
}
@Controller
@RequestMapping("/rest/files")
public class FilesController {
...
@RequestMapping(value="/rest/files", method=RequestMethod.POST)
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 "/testFileDownload";
}
}
html:
测试文件上传
请上传一个文件
在dispatcher-servlet.xml中查看解析器配置:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="file" value="multipart/form-data"/>
<entry key="html" value="text/html"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
我希望我没有浪费时间,这对你来说仍然是必要的。)
编辑2
这里描述了如何使用Spring3.1构建RESTfulWeb服务 它帮助我连接了这个库:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
jstl
我的Maven构建路径(pom.xml)中有这两个选项。我已经检查过了,它们被复制到lib文件夹。我仍然需要它。谢谢我正在尝试你的解决方案。我不能投票支持你,因为我没有足够的代表,但谢谢你的解决方案!一切都好)。如果我的回答有助于你解决这个问题,请接受我的回答。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test file upload</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="rest/files" enctype="multipart/form-data">
<input type="text" name="name" /> <input type="file" name="file" /> <input
type="submit" />
</form>
</body>
</html>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="file" value="multipart/form-data"/>
<entry key="html" value="text/html"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependencies>
<!-- Spring 3 MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Apache Commons file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- JSTL for c: tag -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>