java - Spring REST MultipartFile 文件始终为空

标签 java spring model-view-controller rest file-upload

我正在尝试使用 REST 服务通过 html 和 Spring 3.0.6 设置一个简单的上传。我已经在线学习了教程,但 MultipartFile 参数始终为空。这是配置和代码:

应用程序上下文.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 Controller :

@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 的教程中复制了示例,但无论我更改什么,文件参数始终为 null。 “名称”将在文本框中具有值,但文件将为空。

我也尝试过使用 Jersey,我收到了文件的 InputStream,但 FormDataContentDisposition 为空,所以我无法确定文件类型。

这也在 Jetty 上运行。

我错过了什么?

最佳答案

我记得我通过在我的构建路径中添加额外的库解决了同样的问题:

commons-fileupload-1.2.2.jar
commons-io-2.1.jar

希望对您有所帮助。

编辑。

好的。我终于有时间来解决这个问题了。首先,为什么要使用标准 java 功能来构建 rest 服务(注释@POST、@Path)?因为对于 Spring,最好将 Spring MVC futures 用于 REST。互联网上有很多关于此的信息。这是reference documentation中的特殊部分.这里还有good article on IBM site .关于如何使用 Spring MVC 构建 REST Controller 的很好的描述在 Spring in Action (last 3-d edition) 中。 .

这里我是如何实现简单的文件上传功能的:

休息 Controller :

@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:

<!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>

在 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

这里是 very good tutorial其中描述了如何使用 Spring 3.1 构建 RESTful Web 服务。

关于java - Spring REST MultipartFile 文件始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11358069/

相关文章:

spring - 在不同项目中使用多个 Spring 上下文运行 JUnit 测试

javascript - 为什么 AngularJS 的 todomvc 示例中的 &lt;script&gt; 标签没有显示在 Firefox 的检查器中,但显示在源代码中?

JavaFX - 节点文本上的第一个下划线不呈现

spring - 测试 spring 应用程序上下文无法启动的最佳方法是什么?

java - 如何构建应用程序以帮助测试 sql

json - Spring Data Rest 不明确关联异常

model-view-controller - 与Web应用程序中的三层体系结构相比,MVC有何优势?

javascript - 如何解析 json 对象属性以将其绑定(bind)到控件

java - 编译器优化: will double checking a case cause removal?

java - 图像构造函数的差异