使用以下deps。 Spring boot jersey starter使用Jersey版本2.26
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.26</version>
<scope>compile</scope>
</dependency>
在JerseyConfig中添加了配置。
register(MultiPartFeature.class);
下面是Rest方法的样子
@POST
@Path("/logos2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response uploadLogo2(@FormDataParam("uploadFile") InputStream stream){
}
按照Curl命令执行
curl -X POST "http://localhost:7070/service-manager/services/logos2" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "uploadFile=@1.png;type=image/png"
以下错误请参阅日志
018-04-26 17:13:11.777 DEBUG 30454 --- [nio-7070-exec-2] o.g.j.m.i.ReaderInterceptorExecutor : Message body reader (class org.glassfish.jersey.media.multipart.internal.MultiPartReaderServerSide) is trying to close the entity input stream. Not closing.
2018-04-26 17:13:11.778 DEBUG 30454 --- [nio-7070-exec-2] o.g.j.m.m.i.MultiPartReaderClientSide : Exception occurred during parsing MultiPart message. Performing cleanup.
org.jvnet.mimepull.MIMEParsingException: Missing start boundary
at org.jvnet.mimepull.MIMEParser.skipPreamble(MIMEParser.java:318) ~[mimepull-1.9.6.jar:1.9.6]
依赖树
下面给出依赖树。堆叠在流动要求投入更多文本。
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.0.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.0.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.1.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | | \- org.springframework:spring-core:jar:5.0.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.0.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.0.1.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.5:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.5:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.1.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.29:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.29:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.29:compile
[INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] | +- org.springframework:spring-web:jar:5.0.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.0.5.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.0.5.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:5.0.5.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:5.0.5.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jersey:jar:2.0.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-validation:jar:2.0.1.RELEASE:compile
[INFO] | +- org.glassfish.jersey.core:jersey-server:jar:2.26:compile
[INFO] | | +- javax.ws.rs:javax.ws.rs-api:jar:2.1:compile
[INFO] | | \- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.26:compile
[INFO] | +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.26:compile
[INFO] | +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.26:compile
[INFO] | +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.26:compile
[INFO] | +- org.glassfish.jersey.ext:jersey-spring4:jar:2.26:compile
[INFO] | | +- org.glassfish.jersey.inject:jersey-hk2:jar:2.26:compile
[INFO] | | | \- org.glassfish.hk2:hk2-locator:jar:2.5.0-b42:compile
[INFO] | | | \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.5.0-b42:compile
[INFO] | | +- org.glassfish.hk2:hk2:jar:2.5.0-b42:compile
[INFO] | | | +- org.glassfish.hk2:hk2-utils:jar:2.5.0-b42:compile
[INFO] | | | | \- javax.inject:javax.inject:jar:1:compile
[INFO] | | | +- org.glassfish.hk2:hk2-api:jar:2.5.0-b42:compile
[INFO] | | | +- org.glassfish.hk2:config-types:jar:2.5.0-b42:compile
[INFO] | | | +- org.glassfish.hk2:hk2-core:jar:2.5.0-b42:compile
[INFO] | | | +- org.glassfish.hk2:hk2-config:jar:2.5.0-b42:compile
[INFO] | | | +- org.glassfish.hk2:hk2-runlevel:jar:2.5.0-b42:compile
[INFO] | | | \- org.glassfish.hk2:class-model:jar:2.5.0-b42:compile
[INFO] | | | \- org.glassfish.hk2.external:asm-all-repackaged:jar:2.5.0-b42:compile
[INFO] | | \- org.glassfish.hk2:spring-bridge:jar:2.5.0-b42:compile
[INFO] | \- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.26:compile
[INFO] | +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.26:compile
[INFO] | \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.9.5:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.0.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:2.0.1.RELEASE:compile
[INFO] | | \- org.aspectj:aspectjweaver:jar:1.8.13:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.1.RELEASE:compile
[INFO] | | +- com.zaxxer:HikariCP:jar:2.7.8:compile
[INFO] | | \- org.springframework:spring-jdbc:jar:5.0.5.RELEASE:compile
[INFO] | +- org.hibernate:hibernate-core:jar:5.2.16.Final:compile
[INFO] | | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] | | +- org.javassist:javassist:jar:3.22.0-GA:compile
[INFO] | | +- antlr:antlr:jar:2.7.7:compile
[INFO] | | +- org.jboss:jandex:jar:2.0.3.Final:compile
[INFO] | | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
[INFO] | +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] | +- org.springframework.data:spring-data-jpa:jar:2.0.6.RELEASE:compile
[INFO] | | +- org.springframework.data:spring-data-commons:jar:2.0.6.RELEASE:compile
[INFO] | | +- org.springframework:spring-orm:jar:5.0.5.RELEASE:compile
[INFO] | | +- org.springframework:spring-tx:jar:5.0.5.RELEASE:compile
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | \- org.springframework:spring-aspects:jar:5.0.5.RELEASE:compile
[INFO] +- org.glassfish.jersey.media:jersey-media-multipart:jar:2.26:compile
[INFO] | +- org.glassfish.jersey.core:jersey-common:jar:2.26:compile
[INFO] | | +- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b42:compile
[INFO] | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] | \- org.jvnet.mimepull:mimepull:jar:1.9.6:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.4:compile
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
[INFO] | \- commons-codec:commons-codec:jar:1.11:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.7:compile
[INFO] +- commons-validator:commons-validator:jar:1.6:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] | +- commons-digester:commons-digester:jar:1.8.1:compile
[INFO] | +- commons-logging:commons-logging:jar:1.2:compile
[INFO] | \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] | \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] +- com.h2database:h2:jar:1.4.196:compile
[INFO] +- org.liquibase:liquibase-core:jar:3.5.5:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.19:compile
[INFO] +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] +- org.projectlombok:lombok:jar:1.16.20:compile
[INFO] +- io.swagger:swagger-jersey2-jaxrs:jar:1.5.19:compile
[INFO] | \- io.swagger:swagger-jaxrs:jar:1.5.19:compile
[INFO] | +- io.swagger:swagger-core:jar:1.5.19:compile
[INFO] | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.9.5:compile
[INFO] | | \- io.swagger:swagger-models:jar:1.5.19:compile
[INFO] | +- org.reflections:reflections:jar:0.9.11:compile
[INFO] | \- com.google.guava:guava:jar:20.0:compile
[INFO] +- io.swagger:swagger-annotations:jar:1.5.19:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.2.4:compile
[INFO] | +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] +- org.apache.commons:commons-csv:jar:1.5:compile
[INFO] +- org.mockito:mockito-all:jar:2.0.2-beta:test
[INFO] \- org.junit.jupiter:junit-jupiter-api:jar:5.1.0:test
[INFO] +- org.apiguardian:apiguardian-api:jar:1.0.0:test
[INFO] +- org.opentest4j:opentest4j:jar:1.0.0:test
[INFO] \- org.junit.platform:junit-platform-commons:jar:1.1.0:test
答案 0 :(得分:0)
我认为问题是你正在使用curl而你没有在multipart / form-data的content-header中设置边界。
尝试类似
的内容curl -X POST "http://localhost:7070/service-manager/services/logos2" -H "accept: application/json" -H "Content-Type: multipart/form-data; boundary=------------------------d74496d66958873e" -F "uploadFile=@1.png;type=image/png"
请注意在-H选项中添加边界字符串。