Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc can';t从multipartTTPServletRequest.getFile(“文件”)获取文件,但从request.getPart(“文件”)获取部分_Spring Mvc_Spring Boot_Spring Security - Fatal编程技术网

Spring mvc can';t从multipartTTPServletRequest.getFile(“文件”)获取文件,但从request.getPart(“文件”)获取部分

Spring mvc can';t从multipartTTPServletRequest.getFile(“文件”)获取文件,但从request.getPart(“文件”)获取部分,spring-mvc,spring-boot,spring-security,Spring Mvc,Spring Boot,Spring Security,这是我的全部功能 @RequestMapping(value = "/file",headers = "content-type=multipart/*", method = RequestMethod.POST) @ResponseBody public String fileUpload(HttpServletRequest request,Model model) throws IOException { MultipartHttpServletRequest multipartH

这是我的全部功能

@RequestMapping(value = "/file",headers = "content-type=multipart/*", method = RequestMethod.POST)
@ResponseBody
public String fileUpload(HttpServletRequest request,Model model) throws IOException {
    MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
    MultipartFile files = multipartHttpServletRequest.getFile("files");
    if(files!=null) {
        //do somethings
        log.info(files);
        return "true";
    }else {
        Part file = request.getPart("files");
        log.info(file.getSize());
        return "false";
    }
}
而且
多部分文件文件
为空,但是
部分文件
是我期望的结果; 为什么?我如何直接从
multipartTTPServletRequest.getFile(“文件”)

我的CommonsMultipartResolver配置如下

@Bean
public CommonsMultipartResolver multipartResolver(){
    CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
    commonsMultipartResolver.setMaxInMemorySize(3000000);
    return  commonsMultipartResolver;
}

确保web.xml中有多部分筛选器。如果您正在使用Spring安全过滤器链,则应将其放在其前面


多部件滤波器
org.springframework.web.multipart.support.MultipartFilter
多部件滤波器
/*
将多部分解析器定义为(名称很重要):

@Bean
公共多部分解析器筛选器多部分解析器()
{
返回新的StandardServletMultipartResolver();
}

因为
request.getPart
可以工作,所以您有一个最新版本的Servlet API,不需要Commons FileUpload bean/依赖项,请删除。

您是否配置了
Commons MultipartResolver
?当然,我已经编辑了我的问题,你把它放在你的配置中的什么地方?@chaolou我把它放在WebMVCConfigureAdapter ant中,这是由配置来注释的显示你的客户端html/Ajax,因为它不起作用,所以我有一个问题,为什么你要返回
StandardServletMultipartResolver()
当您有另一个带有
CommonsMultipartResolver
的bean时?@Kramer
StandardServletMultipartResolver
替换
CommonsMultipartResolver
。第一种方法的优点是不依赖于Commons FileUpload。我在这上面发布了一个问题。正如你提到的,我在我的配置文件中创建了bean,在那里我拥有Rest服务工作所需的所有bean。你能指出错误吗?因为我也部分地获得了该文件,但不是以其他方式获得的