繁体   English   中英

在Spring MVC中上传多个文件

[英]Multiple Files upload in spring mvc

我正在尝试使用spring mvc和commons-fileupload * .jar上传多个文件。 我正在使用HttpServletRequest获取请求对象,然后使用request.getPart()获取文件部分。 但是,当我运行代码时,发现以下错误。

java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

代码如下:

public ModelAndView uploadCon(@RequestParam Map<String,String> map, HttpServletRequest request)
{
    Part part1=request.getPart("wallpaper0");
    Part part2=request.getPart("wallpaperx");
    //then write these files 
}

spring-servlet.xml文件具有如下定义的bean:

<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

这是样本

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<Map<String, Object>> handleFileUpload(
        @RequestParam("file1") MultipartFile file1,
        @RequestParam("file2") MultipartFile file2) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("status", "failure");
    try {
        if (!file1.isEmpty() && !file2.isEmpty()) {

            // get the files here

            map.put("status", "success");
        }
    } catch (Exception e) {
        LOGGER.severe(e.toString());
    }

    ResponseEntity<Map<String, Object>> responseEntity = new ResponseEntity<Map<String, Object>>(
            map, HttpStatus.OK);

    return responseEntity;
}

并确保您已添加

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

在您的spring.xml文件中。

或者,如果您想以List<MultipartFile>获取文件列表, 请参考此示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM