gpt4 book ai didi

java - Spring文件上传 - 得到预期的MultipartHttpServletRequest : is a MultipartResolver configured?错误

转载 作者:IT老高 更新时间:2023-10-28 13:47:10 31 4
gpt4 key购买 nike

我正在尝试使用 angular-file-upload 在我的 Angular Web 应用程序中合并多个文件上传功能.目前,前端功能有效,但每次上传尝试都会抛出一个

java.lang.IllegalStateException,java.io.IOException]: 
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest:
is a MultipartResolver configured?

异常。

上传 Controller 定义为

@Controller
@PropertySource("classpath:application.properties")
public class FileUploadController {

@Resource
private Environment env;

@RequestMapping(value = "/fileupload", method = RequestMethod.POST)
@ResponseBody
public List<String> fileUpload(@RequestParam("file") MultipartFile[] uploadFiles) throws IllegalStateException, IOException {
//file processing logic
}
}

在我的AppConfig.java类,我声明 bean

@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding("utf-8");
commonsMultipartResolver.setMaxUploadSize(50000000);
return commonsMultipartResolver;
}

并使用

启动 Web 应用程序
@Override
public void onStartup(ServletContext servletContext) throws ServletException {

AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));

ctx.setServletContext(servletContext);
ctx.refresh();

Dynamic servlet = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
//servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
}

我不认为这是因为缺少 <form>我的 Angular View 中的元素,因为我可以看到 Content-Tylemultipart/form-data并且请求有效负载已正确设置。

Remote Address:192.168.33.10:80
Request URL:http://dev.jason.com/rest/fileupload
Request Method:POST
Status Code:500 Internal Server Error
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:415235
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryBHlsldPQysTVpvwZ
Host:dev.jason.com
Origin:http://dev.jason.com
Pragma:no-cache
Referer:http://dev.jason.com/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36
Request Payload
------WebKitFormBoundaryBHlsldPQysTVpvwZ
Content-Disposition: form-data; name="file"; filename="IMG_5072.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryBHlsldPQysTVpvwZ--

请注意,包含时仍然会出现此问题

@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
multipartConfigFactory.setMaxFileSize("10MB");
multipartConfigFactory.setMaxRequestSize("50MB");
return multipartConfigFactory.createMultipartConfig();
}

AppConfig.java并取消注释命令

servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));

在我的 servlet 初始化程序类中。

感谢任何帮助!

最佳答案

Spring 的 MVC 堆栈要求将 MultipartResolver bean 称为 multipartResolver。将您的 @Bean 方法更改为

@Bean
public CommonsMultipartResolver multipartResolver(){

@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){

关于java - Spring文件上传 - 得到预期的MultipartHttpServletRequest : is a MultipartResolver configured?错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050018/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com