java Spring 3.0 MultipartFile 上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10527837/
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 01:25:57  来源:igfitidea点击:

Spring 3.0 MultipartFile upload

javaspringmultifile-uploader

提问by Gary

I am converting Java web application to Spring framework and appreciate some advice on the issues I am facing with the file upload. Original code was written using org.apache.commons.fileupload.

我正在将 Java Web 应用程序转换为 Spring 框架,并感谢有关我在上传文件时遇到的问题的一些建议。原始代码是使用 org.apache.commons.fileupload 编写的。

  1. Does Spring MultipartFile wraps org.apache.commons.fileupload or I can exclude this dependency from my POM file?

  2. I have seen following example:

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("file") MultipartFile file) {
    
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           return "redirect:uploadSuccess";
        } else {
            return "redirect:uploadFailure";
        }
    }
    

    Originally I tried to follow this example but was always getting an error as it couldn't find this request param. So, in my controller I have done the following:

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    ExtResponse upload(HttpServletRequest request, HttpServletResponse response)
    {
       // Create a JSON response object.
       ExtResponse extResponse = new ExtResponse();
       try {
           if (request instanceof MultipartHttpServletRequest)
           {
               MultipartHttpServletRequest multipartRequest =
                            (MultipartHttpServletRequest) request;
               MultipartFile file = multipartRequest.getFiles("file");
               InputStream input = file.getInputStream();
               // do the input processing
               extResponse.setSuccess(true);
            }
        } catch (Exception e) {
            extResponse.setSuccess(false);
            extResponse.setMessage(e.getMessage());
        }
        return extResponse;
    }
    
  1. Spring MultipartFile 是否包装 org.apache.commons.fileupload 或者我可以从我的 POM 文件中排除这个依赖项?

  2. 我见过以下例子:

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("file") MultipartFile file) {
    
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           return "redirect:uploadSuccess";
        } else {
            return "redirect:uploadFailure";
        }
    }
    

    最初我试图遵循这个例子,但总是收到错误,因为它找不到这个请求参数。因此,在我的控制器中,我完成了以下操作:

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    ExtResponse upload(HttpServletRequest request, HttpServletResponse response)
    {
       // Create a JSON response object.
       ExtResponse extResponse = new ExtResponse();
       try {
           if (request instanceof MultipartHttpServletRequest)
           {
               MultipartHttpServletRequest multipartRequest =
                            (MultipartHttpServletRequest) request;
               MultipartFile file = multipartRequest.getFiles("file");
               InputStream input = file.getInputStream();
               // do the input processing
               extResponse.setSuccess(true);
            }
        } catch (Exception e) {
            extResponse.setSuccess(false);
            extResponse.setMessage(e.getMessage());
        }
        return extResponse;
    }
    

and it is working. If someone can tell me why @RequestParam did not work for me, I will appreciate. BTW I do have

它正在工作。如果有人能告诉我为什么@RequestParam 对我不起作用,我将不胜感激。顺便说一句,我确实有

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

in my servlet context file.

在我的 servlet 上下文文件中。

回答by Bozho

  1. spring does not have a dependency on commons-fileupload, so you'll need it. If it's not there spring will use its internal mechanism
  2. You should pass a MultipartFileas a method parameter, rather than @RequestParam(..)
  1. spring 不依赖于 commons-fileupload,因此您需要它。如果它不存在,弹簧将使用其内部机制
  2. 您应该将 aMultipartFile作为方法参数传递,而不是@RequestParam(..)

回答by Surekha

I had to

我不得不

  1. add commons-fileupload dependency to my pom,
  2. add multipartResolver bean (mentioned in the question),
  3. use @RequestParam("file") MultipartFile file in the handleFormUpload method and
  4. add enctype in my jsp : <form:form method="POST" action="/form" enctype="multipart/form-data" >
  1. 将 commons-fileupload 依赖项添加到我的 pom,
  2. 添加 multipartResolver bean(在问题中提到),
  3. 在 handleFormUpload 方法中使用 @RequestParam("file") MultipartFile 文件和
  4. 在我的 jsp 中添加 enctype : <form:form method="POST" action="/form" enctype="multipart/form-data" >

to get it to work.

让它工作。

回答by bobmanc

This works for me.

这对我有用。

@RequestMapping(value = "upload.spr", method = RequestMethod.POST)
public ModelAndView upload(@RequestParam("file") MultipartFile file, HttpServletResponse response)
{
    //  handle file here
}

回答by viquar

The General sysntax for request param is this @RequestParam(value="Your value", required=true), mode over request param is used to get a value frm the Url.

请求参数的通用 sysntax 是这个 @RequestParam(value="Your value", required=true),请求参数的模式用于从 Url 获取值。

回答by killjoy

In a POST you will only send the params in the request body, not in the URL (for which you use @RequestParams)

在 POST 中,您只会在请求正文中发送参数,而不是在 URL 中(为此您使用 @RequestParams)

Thats why your second method worked.

这就是你的第二种方法奏效的原因。

回答by Mladen Uzelac

In Spring MVC 3.2 support for Servet 3.0 was introduced. So you need to include commons-file upload if you use earlier versions of Spring.

在 Spring MVC 3.2 中引入了对 Servet 3.0 的支持。因此,如果您使用 Spring 的早期版本,则需要包含 commons-file upload。