java Spring REST MultipartFile 文件始终为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11358069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:45:45  来源:igfitidea点击:

Spring REST MultipartFile file is always null

javaspringmodel-view-controllerrestfile-upload

提问by C. Locke

I'm trying to set up a simple upload with html and Spring 3.0.6 using REST services. I've followed the tutorial online but the MultipartFile parameter is always null. Here's the config and code:

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

application-context.xml:

应用程序上下文.xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="2000000"/>
</bean>

pom.xml:

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:

<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;
}

I copied the example from Spring's tutorial but no matter what I change, the file parameter is always null. "name" will have the value in the text box but file will be null.

我从 Spring 的教程中复制了示例,但无论我更改什么,文件参数始终为空。“名称”将在文本框中具有值,但文件将为空。

I have also tried using Jersey and I receive the InputStream for the file but the FormDataContentDisposition is null so I can't determine the file type.

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

This is running on Jetty as well.

这也在 Jetty 上运行。

What am I missing?

我错过了什么?

回答by dimas

As I remember I solved same issue by putting additional libs to my build path:

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

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

I hope this will help you.

我希望这能帮到您。

Edit.

编辑。

Ok. At last I had time for this issue. First of all, why do you use standart java features for building rest service (annotations @POST, @Path)? Because with Spring it is better to use spring MVC futures for REST. There is a lot of information about this in internet. Here is special part in reference documentation. Also here is good article on IBM site. Also very good description on how to build REST controller with Spring MVC is in Spring in Action (last 3-d edition).

行。终于有时间解决这个问题了。首先,为什么要使用标准的 Java 功能来构建休息服务(注释 @POST、@Path)?因为对于 Spring,最好将 Spring MVC 期货用于 REST。互联网上有很多关于这方面的信息。这是参考文档中的特殊部分 。IBM 网站上的好文章也在这里。在Spring in Action (last 3-d edition) 中也很好地描述了如何使用 Spring MVC 构建 REST 控制器。

Here how I have implemented simple file uploading functionality:

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

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

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>

View resolver configuration in dispatcher-servlet.xml:

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

I hope I'm not wasted my time and this is still necessary for you. )

我希望我没有浪费我的时间,这对你来说仍然是必要的。)

EDIT 2

编辑 2

Here is very good tutorialwhere described how to build RESTful web service with Spring 3.1.

这是一个非常好的教程,其中描述了如何使用 Spring 3.1 构建 RESTful Web 服务。

回答by Elena

It helped me to connect this library:

它帮助我连接了这个库:

 <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

All libs:

所有库:

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

See http://viralpatel.net/blogs/spring-mvc-multiple-file-upload-example/

http://viralpatel.net/blogs/spring-mvc-multiple-file-upload-example/