我在尝试解析包含多部分文件的HttpServletRequest时遇到问题。我已经尝试了有关我面临的异常的其他帖子的所有解决方案,但似乎没有任何效果。
这些是项目的相关依赖项:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
SpringBoot Application 初始值设定项:
@SpringBootApplication
public class ApplicationBoot extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ApplicationBoot.class);
}
public static void main(String[] args) {
SpringApplication.run(ApplicationBoot.class, args);
}
}
泽西初始值设定项配置和多部分解析程序 bean:
@Configuration
@ApplicationPath("/rest")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(AttachmentResource.class);
register(ContentResource.class);
register(MultiPartFeature.class);
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
return multipartResolver;
}
}
以及我面临有关多部分解析和资源问题的类:
@Component
@Path("/attachments")
public class AttachmentResource {
@POST
@Path("/store")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response store(@Context HttpServletRequest httpRequest) {
for (Part part : httpRequest.getParts()) {
...
}
}
}
当getParts((被调用时,我收到java.lang.IllegalStateException:无法处理部件,因为未提供多部件配置
我在 application.properties 中添加了这些属性:
spring.http.multipart.enabled=true
spring.http.multipart.max-file-size=100MB
spring.http.multipart.max-request-size=100MB
spring.http.multipart.file-size-threshold=0
如此处所示:https://docs.spring.io/spring-boot/docs/1.5.13.RELEASE/reference/html/common-application-properties.html
这也是 SpringBoot 在启动时启用有关多部分配置的调试输出的日志记录:
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:
-----------------
MultipartAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.multipart.support.StandardServletMultipartResolver', 'javax.servlet.MultipartConfigElement'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnProperty (spring.http.multipart.enabled) matched (OnPropertyCondition)
MultipartAutoConfiguration#multipartConfigElement matched:
- @ConditionalOnMissingBean (types: javax.servlet.MultipartConfigElement; SearchStrategy: all) did not find any beans (OnBeanCondition)
Negative matches:
-----------------
MultipartAutoConfiguration#multipartResolver:
Did not match:
- @ConditionalOnMissingBean (types: org.springframework.web.multipart.MultipartResolver; SearchStrategy: all) found bean 'multipartResolver' (OnBeanCondition)
后期编辑:
如果我在独立的 tomcat 8 中部署应用程序并将其添加到 con/context 中.xml它可以工作:
<Context allowCasualMultipartParsing="true">
或者在项目中的 src/main/webapp/META-INF 下添加一个上下文.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" allowCasualMultipartParsing="true"/>
但是如果我使用嵌入式 Tomcat 启动项目,它不起作用。
这不是如何将MultiPart与Jersey一起使用。如果要获取所有部件,可以使用FormDataMultiPart
作为实体参数
@POST
public Response post(FormDataMultiPart multiPart) {
Map<String, List<FormDataBodyPart>> parts = multiPart.getParts();
}
如果要按名称注入单个零件,可以使用@FormDataParam
注释,将零件的名称传递给注释。
@POST
public Response post(@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fileDatail) {
}
参见
- 用于多部分支持的泽西岛文档