简体   繁体   English

MultipartFile 始终为 NULL

[英]MultipartFile is always NULL

I'm having a problem when uploading a file using MultipartFile.使用 MultipartFile 上传文件时遇到问题。 I'm using Spring v2.2.6.RELEASE whenever i upload i get the value string but the file is NULL.每当我上传时,我都在使用 Spring v2.2.6.RELEASE 得到值字符串,但文件是 NULL。 Here's my code below下面是我的代码

UploadController.java上传控制器.java

 @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response upload(@BeanParam AttachmentRequestDTO attachmentRequestDTO) {
        log.info("Hello - from upload endpoint");
        log.info("senderId: "+ attachmentRequestDTO.getSenderId());
        log.info("groupId: "+ attachmentRequestDTO.getGroupId());
        log.info("file: "+ attachmentRequestDTO.getFile().isEmpty());
        return Response.status(200).entity("test123").build();
    
    }

AttachmentRequestDTO附件请求DTO

public class AttachmentRequestDTO {

    @FormDataParam("file")
    private MultipartFile file;

    @FormDataParam("senderId")
    private String senderId;

    @FormDataParam("groupId")
    private String groupId;

... ...

Config.java配置java

@Configuration
public class Config {

    @Bean(name="multipartResolver")
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("utf-8");
        commonsMultipartResolver.setMaxUploadSize(20000000);
        commonsMultipartResolver.setResolveLazily(false);
        return commonsMultipartResolver;
    }
}

SpringApplication.java SpringApplication.java

@SpringBootApplication
public class AttachmentApiApplication extends BaseMerakApplication {

    public static void main(String[] args) {
        SpringApplication.run(AttachmentApiApplication.class, args);
    }

    @Bean
    protected Consumer<ResourceConfig> customJerseyConfig() {
        return resourceConfig -> {
            resourceConfig.register(MultiPartFeature.class);
            resourceConfig.register(Config.class);
        };
    }

    @Bean
    public RequestContextFilter requestContextFilter() {
        return new RequestContextFilter();
    }
}

Here's the screenshot when hitting on postman这是点击 postman 时的屏幕截图邮递员表单数据

And here's the screenshot result for the log file.这是日志文件的屏幕截图结果。 as you see I can get the value for the string but the file I'm getting is always null.如您所见,我可以获得字符串的值,但我得到的文件始终是 null。 I tried to add the multipartResolver on the config file as this resolves their problem but for me, I'm not getting the same result.我尝试在配置文件中添加 multipartResolver,因为这解决了他们的问题,但对我来说,我没有得到相同的结果。 Does anyone encountered this issue before and how did you solve this.有没有人遇到过这个问题,你是如何解决这个问题的。 Is there specific version of jersey to be used for this?是否有特定版本的 jersey 可用于此?

2020-07-21 12:42:19.774  INFO 17683 --- [nio-8092-exec-1] c.i.c.a.a.p.FileUploadController         : Hello - from upload endpoint
2020-07-21 12:42:19.774  INFO 17683 --- [nio-8092-exec-1] c.i.c.a.a.p.FileUploadController         : senderId: testCjid123
2020-07-21 12:42:19.774  INFO 17683 --- [nio-8092-exec-1] c.i.c.a.a.p.FileUploadController         : groupId: ChannelId123
2020-07-21 12:42:19.779 ERROR 17683 --- [nio-8092-exec-1] c.i.a.t.s.f.ThrowableProcessingFilter    : An unexpected exception occurred while handling the request
javax.servlet.ServletException: java.lang.NullPointerException
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:410) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205) ~[jersey-container-servlet-core-2.30.1.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.LocalDtabFilter.$anonfun$doFilter$6(LocalDtabFilter.scala:48) ~[toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.LocalDtabFilter$$Lambda$1204.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at com.twitter.finagle.DtabCompanionBase.unwind(DtabBase.scala:338) ~[finagle-core_2.12-20.4.0.jar:20.4.0]
        at com.ing.apisdk.toolkit.servlet.filter.LocalDtabFilter.doFilter(LocalDtabFilter.scala:46) ~[toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.$anonfun$callChain$1(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter$$Lambda$1166.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at scala.util.Try$.apply(Try.scala:213) ~[scala-library-2.12.11.jar:na]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.callChain(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.tracing.servlet.TracingServletFilter.$anonfun$doFilter$1(TracingServletFilter.scala:61) ~[toolkit-tracing-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.tracing.servlet.TracingServletFilter$$Lambda$1190.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at com.twitter.util.Local.let(Local.scala:4978) ~[util-core_2.12-20.4.0.jar:20.4.0]
        at com.twitter.finagle.context.LocalContext.letLocal(LocalContext.scala:51) ~[finagle-core_2.12-20.4.0.jar:20.4.0]
        at com.twitter.finagle.context.LocalContext.let(LocalContext.scala:24) ~[finagle-core_2.12-20.4.0.jar:20.4.0]
        at com.ing.apisdk.toolkit.connectivity.tracing.core.OpenTracingContext$.withParentSpan(OpenTracingContext.scala:20) ~[toolkit-connectivity-tracing-core_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.tracing.servlet.TracingServletFilter.doFilter(TracingServletFilter.scala:58) ~[toolkit-tracing-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.$anonfun$callChain$1(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter$$Lambda$1166.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at scala.util.Try$.apply(Try.scala:213) ~[scala-library-2.12.11.jar:na]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.callChain(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.doFilter(AsyncAwareHttpServletFilter.scala:52) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.$anonfun$callChain$1(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter$$Lambda$1166.0000000000000000.apply$mcV$sp(Unknown Source) ~[na:na]
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) ~[scala-library-2.12.11.jar:na]
        at scala.util.Try$.apply(Try.scala:213) ~[scala-library-2.12.11.jar:na]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.callChain(AsyncAwareHttpServletFilter.scala:72) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at com.ing.apisdk.toolkit.servlet.filter.AsyncAwareHttpServletFilter.doFilter(AsyncAwareHttpServletFilter.scala:52) [toolkit-servlet_2.12-11.1.0.jar:11.1.0]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_232]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_232]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
        at java.lang.Thread.run(Thread.java:819) [na:1.8.0_232]
Caused by: java.lang.NullPointerException: null
        at com.ing.contacting.attachments.api.presentation.FileUploadController.upload(FileUploadController.java:46) ~[classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_232]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_232]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_232]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_232]
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$$Lambda$1272.0000000000000000.invoke(Unknown Source) ~[na:na]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:244) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) ~[jersey-common-2.30.1.jar:na]
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) ~[jersey-server-2.30.1.jar:na]
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394) ~[jersey-container-servlet-core-2.30.1.jar:na]
        ... 72 common frames omitted

also whenever start this, a log warning tells me that the ConfigSetup.class is ignored.也每当启动这个,日志警告告诉我 ConfigSetup.class 被忽略。 I'm not sure if this is one of the reasons why I'm having NULL pointer.我不确定这是否是我拥有 NULL 指针的原因之一。

2020-07-21 21:02:29.352  WARN 51860 --- [           main] o.g.jersey.internal.inject.Providers     : A provider com.ing.contacting.attachments.api.settings.ConfigSetup registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.ing.contacting.attachments.api.settings.ConfigSetup will be ignored. 

The form-data part is called file in your source code, not files . form-data 部分在您的源代码中称为file ,而不是files

Solved by using FormDataBodyPart instead of MultipartFile on AttachmentRequestDTO class.通过在 AttachmentRequestDTO class 上使用FormDataBodyPart而不是 MultipartFile 解决。

public class AttachmentRequestDTO {


@NotNull
@FormDataParam("file")
private FormDataBodyPart file;

@NotNull
@FormDataParam("senderId")
private String senderId;

@NotNull
@FormDataParam("groupId")
private String groupId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM