当前位置: 首页 > 知识库问答 >
问题:

在Spring靴中上传多部分文件失败,出现 415

白翔
2023-03-14

我有一个将多部分文件作为请求的后操作,我想稍后将其存储在FTP上。

但是现在我只有方法设置,当尝试从postman调用时,它给了我一个错误响应HTTP 415。

这就是我所拥有的:

控制器:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Component;


@Path("/")
@Produces("application/json;charset=UTF-8")
@Component
public class GUIController {

    @Path("/upload")
    @POST
    @Consumes(MediaType.WILDCARD)
    public Response uploadPicture(@RequestParam("file") MultipartFile input) {
        return Response.ok().build();
    }

}

我的邮递员请求和响应:

你能帮我修一下吗?谢谢

更新:

根据评论进行了一些更改后,这就是我所拥有的:

控制器:

 import java.io.InputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.springframework.stereotype.Component;

@Path("/media")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class FileUploadController {

    @Path("/upload")
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadPicture(
            @FormDataParam("file") InputStream file,
            @FormDataParam("file") FormDataContentDisposition filFormDataContentDisposition) {
        return Response.ok().build();
    }
    }


public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
    register(MultiPartFeature.class);
    register(FileUploadController.class);
}

}

@Bean
public ServletRegistrationBean uploadServlet() {
    ServletContainer servletContainer = new ServletContainer(new JerseyConfig());
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servletContainer, "/media/*");
    servletRegistrationBean.setName("uploadServlet");
    return servletRegistrationBean;
}

我在调用上传方法时收到以下错误:

01:40:57.970 [http-nio-8030-exec-2] ERROR o.a.c.c.C.[.[.[/].[rest-api-v2] - Allocate exception for servlet rest-api-v2
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.example.tsi.channel.endpoint.v1.impl.FileUploadController.uploadPicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.example.tsi.channel.endpoint.v1.impl.FileUploadController, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@50286f19]}, definitionMethod=public javax.ws.rs.core.Response com.example.tsi.channel.endpoint.v1.impl.FileUploadController.uploadPicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555) ~[jersey-server-2.23.2.jar:na]

依赖关系树:

|    |    |    \--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.4.
5 -> 2.8.4
|    |    |         +--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.8.4
|    |    |         |    +--- com.fasterxml.jackson.core:jackson-core:2.8.4
|    |    |         |    \--- com.fasterxml.jackson.core:jackson-databind:2.8.4
(*)
|    |    |         +--- com.fasterxml.jackson.core:jackson-core:2.8.4
|    |    |         +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |    |         \--- com.fasterxml.jackson.module:jackson-module-jaxb-annota
tions:2.8.4 (*)
|    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-core:2.1
 -> 2.23.2
|    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2
|    |    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    |    +--- javax.annotation:javax.annotation-api:1.2
|    |    |    |    +--- org.glassfish.jersey.bundles.repackaged:jersey-guava:2.
23.2
|    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    |    \--- org.glassfish.hk2.external:aopalliance-repackaged:
2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2.external:aopalliance-repackaged:
2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    |    \--- org.javassist:javassist:3.20.0-GA
|    |    |    |    \--- org.glassfish.hk2:osgi-resource-locator:1.0.1
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2
|    |    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |    |    |    +--- org.glassfish.jersey.core:jersey-client:2.23.2
|    |    |    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)

|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    |    \--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    |    +--- org.glassfish.jersey.media:jersey-media-jaxb:2.23.2
|    |    |    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)

|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    |    \--- org.glassfish.hk2:osgi-resource-locator:1.0.1
|    |    |    |    +--- javax.annotation:javax.annotation-api:1.2
|    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    \--- javax.validation:validation-api:1.1.0.Final
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    \--- org.glassfish.jersey.media:jersey-media-multipart:2.1 -> 2.22.2
|    |         +--- org.glassfish.jersey.core:jersey-common:2.22.2 -> 2.23.2 (*)

|    |         \--- org.jvnet.mimepull:mimepull:1.9.6
|    +--- org.springframework.boot:spring-boot-starter-actuator:1.4.2.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot:1.4.2.RELEASE
|    |    |    |    +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    |    \--- org.springframework:spring-context:4.3.4.RELEASE
|    |    |    |         +--- org.springframework:spring-aop:4.3.4.RELEASE
|    |    |    |         |    +--- org.springframework:spring-beans:4.3.4.RELEAS
E
|    |    |    |         |    |    \--- org.springframework:spring-core:4.3.4.RE
LEASE
|    |    |    |         |    \--- org.springframework:spring-core:4.3.4.RELEASE

|    |    |    |         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)

|    |    |    |         +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    |         \--- org.springframework:spring-expression:4.3.4.RELEAS
E
|    |    |    |              \--- org.springframework:spring-core:4.3.4.RELEASE

|    |    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.4.2.REL
EASE
|    |    |    |    \--- org.springframework.boot:spring-boot:1.4.2.RELEASE (*)
|    |    |    +--- org.springframework.boot:spring-boot-starter-logging:1.4.2.R
ELEASE
|    |    |    |    +--- ch.qos.logback:logback-classic:1.1.7
|    |    |    |    |    +--- ch.qos.logback:logback-core:1.1.7
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.20 -> 1.7.21
|    |    |    |    +--- org.slf4j:jcl-over-slf4j:1.7.21
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    |    |    +--- org.slf4j:jul-to-slf4j:1.7.21
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    |    |    \--- org.slf4j:log4j-over-slf4j:1.7.21
|    |    |    |         \--- org.slf4j:slf4j-api:1.7.21
|    |    |    +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    \--- org.yaml:snakeyaml:1.17
|    |    \--- org.springframework.boot:spring-boot-actuator:1.4.2.RELEASE
|    |         +--- org.springframework.boot:spring-boot:1.4.2.RELEASE (*)
|    |         +--- org.springframework.boot:spring-boot-autoconfigure:1.4.2.REL
EASE (*)
|    |         +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |         +--- org.springframework:spring-core:4.3.4.RELEASE
|    |         \--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-starter-jersey:1.4.2.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RELEASE

|    |    |    +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6
|    |    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.6
|    |    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.6
|    |    |         \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6
|    |    +--- org.springframework.boot:spring-boot-starter-validation:1.4.2.REL
EASE
|    |    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (
*)
|    |    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.6
|    |    |    \--- org.hibernate:hibernate-validator:5.2.4.Final
|    |    |         +--- javax.validation:validation-api:1.1.0.Final
|    |    |         +--- org.jboss.logging:jboss-logging:3.2.1.Final -> 3.3.0.Fi
nal
|    |    |         \--- com.fasterxml:classmate:1.1.0 -> 1.3.3
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |    +--- org.springframework:spring-web:4.3.4.RELEASE
|    |    |    +--- org.springframework:spring-aop:4.3.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:4.3.4.RELEASE
|    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-core:2.2
3.2 (*)
|    |    +--- org.glassfish.jersey.containers:jersey-container-servlet:2.23.2
|    |    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-cor
e:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    +--- org.glassfish.jersey.ext:jersey-bean-validation:2.23.2
|    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    |    +--- javax.validation:validation-api:1.1.0.Final
|    |    |    +--- org.hibernate:hibernate-validator:5.1.3.Final -> 5.2.4.Final
 (*)
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    +--- org.glassfish.jersey.ext:jersey-spring3:2.23.2
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-cor
e:2.23.2 (*)
|    |    |    +--- org.glassfish.hk2:hk2:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:config-types:2.5.0-b05
|    |    |    |    |    \--- org.glassfish.hk2:hk2-config:2.5.0-b05
|    |    |    |    |         \--- org.glassfish.hk2:hk2-core:2.5.0-b05
|    |    |    |    |              +--- org.glassfish.hk2:hk2-locator:2.5.0-b05
(*)
|    |    |    |    |              \--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-core:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:hk2-config:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:hk2-runlevel:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    \--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    \--- org.glassfish.hk2:class-model:2.5.0-b05
|    |    |    |         \--- org.glassfish.hk2.external:asm-all-repackaged:2.5.
0-b05
|    |    |    +--- org.glassfish.hk2:spring-bridge:2.5.0-b05
|    |    |    |    \--- org.springframework:spring-context:3.2.3.RELEASE -> 4.3
.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-beans:3.2.3.RELEASE -> 4.3.4.RELE
ASE (*)
|    |    |    +--- org.springframework:spring-core:3.2.3.RELEASE -> 4.3.4.RELEA
SE
|    |    |    +--- org.springframework:spring-web:3.2.3.RELEASE -> 4.3.4.RELEAS
E (*)
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    \--- org.glassfish.jersey.media:jersey-media-json-jackson:2.23.2
|    |         +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |         +--- org.glassfish.jersey.ext:jersey-entity-filtering:2.23.2
|    |         |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |         +--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.5.4 -> 2.8.
4 (*)
|    |         +--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.5.
4 -> 2.8.4 (*)
|    |         \--- com.fasterxml.jackson.core:jackson-annotations:2.5.4 -> 2.8.
4
|    +--- org.springframework.boot:spring-boot-starter-web-services:1.4.2.RELEAS
E
|    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (
*)
|    |    |    +--- org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RE
LEASE (*)
|    |    |    +--- org.hibernate:hibernate-validator:5.2.4.Final (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |    |    +--- org.springframework:spring-web:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-webmvc:4.3.4.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.3.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |         +--- org.springframework:spring-expression:4.3.4.RELEASE (*)

|    |    |         \--- org.springframework:spring-web:4.3.4.RELEASE (*)
|    |    +--- org.springframework:spring-oxm:4.3.4.RELEASE
|    |    |    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:4.3.4.RELEASE
|    |    \--- org.springframework.ws:spring-ws-core:2.3.1.RELEASE
|    |         +--- org.springframework.ws:spring-xml:2.3.1.RELEASE
|    |         |    +--- org.springframework:spring-beans:4.0.9.RELEASE -> 4.3.4
.RELEASE (*)
|    |         |    +--- org.springframework:spring-context:4.0.9.RELEASE -> 4.3
.4.RELEASE (*)
|    |         |    \--- org.springframework:spring-core:4.0.9.RELEASE -> 4.3.4.
RELEASE
|    |         +--- org.springframework:spring-aop:4.0.9.RELEASE -> 4.3.4.RELEAS
E (*)
|    |         +--- org.springframework:spring-beans:4.0.9.RELEASE -> 4.3.4.RELE
ASE (*)
|    |         +--- org.springframework:spring-core:4.0.9.RELEASE -> 4.3.4.RELEA
SE
|    |         +--- org.springframework:spring-oxm:4.0.9.RELEASE -> 4.3.4.RELEAS
E (*)
|    |         +--- org.springframework:spring-web:4.0.9.RELEASE -> 4.3.4.RELEAS
E (*)
|    |         \--- org.springframework:spring-webmvc:4.0.9.RELEASE -> 4.3.4.REL
EASE (*)
|    +--- io.dropwizard.metrics:metrics-jersey2:3.1.2
|    |    +--- io.dropwizard.metrics:metrics-core:3.1.2 (*)
|    |    +--- io.dropwizard.metrics:metrics-annotation:3.1.2
|    |    |    \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.21
|    |    +--- org.glassfish.jersey.core:jersey-server:2.11 -> 2.23.2 (*)
|    |    \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.21
|    +--- org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE (*)
|    \--- net.logstash.logback:logstash-logback-encoder:4.7
|         +--- ch.qos.logback:logback-core:1.1.6 -> 1.1.7
|         \--- com.fasterxml.jackson.core:jackson-databind:2.6.5 -> 2.8.4 (*)
+--- org.springframework.boot:spring-boot-starter-jdbc: -> 1.4.2.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    +--- org.apache.tomcat:tomcat-jdbc:8.5.6
|    |    \--- org.apache.tomcat:tomcat-juli:8.5.6
|    \--- org.springframework:spring-jdbc:4.3.4.RELEASE
|         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|         +--- org.springframework:spring-core:4.3.4.RELEASE
|         \--- org.springframework:spring-tx:4.3.4.RELEASE
|              +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|              \--- org.springframework:spring-core:4.3.4.RELEASE
+--- org.springframework:spring-jdbc: -> 4.3.4.RELEASE (*)
+--- org.springframework.boot:spring-boot-starter-redis: -> 1.4.2.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    +--- org.springframework.data:spring-data-redis:1.7.5.RELEASE
|    |    +--- org.springframework.data:spring-data-keyvalue:1.1.5.RELEASE
|    |    |    +--- org.springframework.data:spring-data-commons:1.12.5.RELEASE
|    |    |    |    +--- org.springframework:spring-core:4.2.8.RELEASE -> 4.3.4.
RELEASE
|    |    |    |    +--- org.springframework:spring-beans:4.2.8.RELEASE -> 4.3.4
.RELEASE (*)
|    |    |    |    +--- org.slf4j:slf4j-api:1.7.21
|    |    |    |    \--- org.slf4j:jcl-over-slf4j:1.7.21 (*)
|    |    |    +--- org.springframework:spring-context:4.2.8.RELEASE -> 4.3.4.RE
LEASE (*)
|    |    |    +--- org.springframework:spring-tx:4.2.8.RELEASE -> 4.3.4.RELEASE
 (*)
|    |    |    +--- org.slf4j:slf4j-api:1.7.21
|    |    |    \--- org.slf4j:jcl-over-slf4j:1.7.21 (*)
|    |    +--- org.springframework:spring-tx:4.2.8.RELEASE -> 4.3.4.RELEASE (*)
|    |    +--- org.springframework:spring-oxm:4.2.8.RELEASE -> 4.3.4.RELEASE (*)

|    |    +--- org.springframework:spring-aop:4.2.8.RELEASE -> 4.3.4.RELEASE (*)

|    |    +--- org.springframework:spring-context-support:4.2.8.RELEASE -> 4.3.4
.RELEASE
|    |    |    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:4.3.4.RELEASE
|    |    +--- org.slf4j:slf4j-api:1.7.21
|    |    \--- org.slf4j:jcl-over-slf4j:1.7.21 (*)
|    \--- redis.clients:jedis:2.8.2
|         \--- org.apache.commons:commons-pool2:2.4.2
+--- org.springframework:spring-context-support: -> 4.3.4.RELEASE (*)
+--- io.projectreactor:reactor-bus: -> 2.0.8.RELEASE
|    +--- com.goldmansachs:gs-collections:5.1.0
|    |    \--- com.goldmansachs:gs-collections-api:5.1.0
|    \--- io.projectreactor:reactor-core:2.0.8.RELEASE
|         +--- org.reactivestreams:reactive-streams:1.0.0
|         \--- org.slf4j:slf4j-api:1.7.12 -> 1.7.21
+--- com.canaldigital.tsi:common-config-service:1.1.16 (*)
+--- com.thoughtworks.xstream:xstream:1.3
|    \--- xpp3:xpp3_min:1.1.4c
+--- com.oracle:ojdbc6:11.2.0.2.0
+--- io.swagger:swagger-jersey2-jaxrs:1.5.9 (*)
+--- org.glassfish.jersey.media:jersey-media-multipart:2.22.2 (*)
\--- junit:junit:4.11
     \--- org.hamcrest:hamcrest-core:1.3

共有1个答案

丌官炎彬
2023-03-14

您有多个问题:

  1. 您正在将与泽西岛相关的注释与SpringMVC注释混合在一起。这些不适用于泽西岛,因为它们是SpringMVC的细节。

而不是使用这些导入:

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

您需要将它们替换为:

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import java.io.InputStream;

您的控制器将如下所示:

import java.io.InputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.springframework.stereotype.Component;

@Path("/media")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class GUIController {

    @Path("/upload")
    @POST
    @Consumes(MediaType.WILDCARD)
    public Response uploadPicture(
            @FormDataParam("file") InputStream file,
            @FormDataParam("file") FormDataContentDisposition filFormDataContentDisposition) {
        return Response.ok().build();
    }
}

这相当于多部分表单数据的运动衫上传。

因此,您需要前往Maven Central Repository获取org.glassfish.jersey。media:jersey media multipart到类路径中。

您还需要在泽西岛配置中启用多部分功能,如下所示:

@Component
@ApplicationPath("media")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(MultiPartFeature.class);
        register(GUIController.class);
    }
}
 类似资料:
  • 不知道我还缺了什么。调试时我还发现 getAllowCasualMultiPartParsing()显示为false。看起来tomcat上下文中没有设置多部分配置。我使用的是嵌入式tomcat。

  • 我正在Spring controller中努力实现多部分文件上传。我读过很多问题,谷歌,但似乎什么都不管用。 我明白了 我的BE控制器: FE,angularJS: HTML: 还有应用程序。属性包括: 更新: 当我按照@Byeon0gam的建议从我的控制器中删除@RequestParam时,我不再会遇到这个错误,但是我的文件在控制器中是空的。虽然在FE服务中,如我所见,它不是空的:

  • 我已经实现了一个Spring MVC REST服务,它接受一个包含文件上传和JSON主体的多部分消息。以下是所涉及的主要类: 我的问题是servlet初始化器(c:/temp/)中定义的临时位置包含。tmp文件夹,这些文件夹是在每次对该服务的请求之后创建的,并且从未被删除。在记事本中打开它们,看起来它们只包含请求中发送的JSON的纯文本副本,而不包含上传文件的字节。我一辈子都想不出如何让这些文件在

  • http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 包com.upload.controller;这是我在Springmvc控制器中使用的示例代码,用于将文件上传到服务器,现在我在本地主机中使用

  • 我正在尝试使用基于文档的amazon s3客户端,在Kotlin中获得一个简单的多部分上传概念证明。第一部分上传成功,我得到了一个带有etag的响应。第二部分没有上传任何东西,超时了。它总是在第一部分之后失败。我是否需要手动清理一些连接? 凭据和权限都很好。下面的幻数只是为了达到5MB的最小零件大小。 我在这里做错了什么? 这总是在第二部分失败 只是为了先占一些我不想要的答案,我这样做的目的不是上

  • 我试图创建一个页面,用户可以张贴图像及其细节。现在,当我测试来自postman的spring boot服务时,我能够成功地在服务中获取文件。当我试图从angular5中做同样的事情时,多部分文件在服务中没有被识别,并且总是得到空数组。 我的角服务代码如下 } 我已经尝试添加标头,如multipart/form-data,并将其设置为un定义。无论哪种方式,我都收到了错误。在发布到这里之前,我已经广