Spring 3.0 MultipartFile上传

4

我正在将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;
    }
    

现在它已经可以运行了。如果有人能告诉我为什么@RequestParam对我不起作用,我将不胜感激。顺便说一句,我确实有

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

在我的servlet上下文文件中。

6个回答

3

我必须执行以下操作才能让它正常工作:

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

2
  1. Spring框架没有依赖commons-fileupload,所以您需要它。如果缺少它,Spring将使用其内部机制。
  2. 您应该将MultipartFile作为方法参数传递,而不是@RequestParam(..)

作为方法参数传递是什么意思?谁会从请求中提取它并将其提供给方法? - Gary
Spring Framework或Spring Boot不会引入对commons-fileupload的依赖。然而,Spring Cloud Open Feign将通过org.springframework.cloud:spring-cloud-openfeign-core引入此依赖。 - Krzysztof Tomaszewski

1

这对我有效。

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

0
在POST请求中,您只会将参数发送到请求正文中,而不是URL中(对于此操作,您可以使用@RequestParams)。
这就是为什么您的第二种方法有效的原因。

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

0
请求参数的通用语法是这样的:@RequestParam(value="你的值", required=true)。使用请求参数模式可以从URL中获取一个值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接