【问题标题】:My multipartfile disappears between my front and my back我的 multipartfile 在我的正面和背面之间消失了
【发布时间】:2021-05-12 15:56:18
【问题描述】:

我有一个使用 Spring Boot 的 Java 11 应用程序。

我的请求控制器中有这个请求:

@RestController
public class ImportController {
     private static final Logger LOGGER = LoggerFactory.getLogger(ImportController.class);

     @PostMapping("/import")
     public ResponseEntity<List<CompareAnalysis>> importXML(@RequestParam("files") List<MultipartFile> files) {
        LOGGER.debug("issue here" + files.size());
...
     }
}

生成我的战争后,我把它放在我的 Tomcat Vanilla 9.0.45 中。

当我尝试像这样调用我的应用程序时:curl -X POST -F 'files=@toto.pdf' http://localhost:8080/import/,我的日志中总是有:

17:22:33.461 [http-nio-8080-exec-3] DEBUG my.app.controler.ImportController - issue here 0

我不知道我的多部分文件丢失在哪里...

【问题讨论】:

    标签: java spring-boot tomcat multipartfile


    【解决方案1】:

    我终于找到了问题:我错过了初始化程序中的配置。

    就像我在这个网站上看到的 https://www.dev2qa.com/spring-mvc-file-upload-unable-to-process-parts-as-no-multi-part-configuration-has-been-provided/ 我在我的 Initializer 中添加了多部分元素

    public class AppInitializer implements WebApplicationInitializer {
    
       @Override
       public void onStartup(ServletContext container) throws ServletException {
           ...
    
           MultipartConfigElement multipartConfig = new MultipartConfigElement("/tmp");
           dispatcher.setMultipartConfig(multipartConfig);
    
           dispatcher.setLoadOnStartup(1);
    
           dispatcher.addMapping("*.html");
    
           FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", MultipartFilter.class);
           multipartFilter.addMappingForUrlPatterns(null, true, "/*");
    
       }
    }
    

    它为我解决了所有问题。

    【讨论】:

    • 为什么首先要有一个初始化器?如果您使用的是 Spring 引导,则不需要这个。
    • 如果没有我的初始化程序,我所有的调用都会返回 404。我认为这是因为我的 tomcat 运行在我的背部和我的前角。
    • 如果你需要那个初始化器,那么你在 Spring Boot 中做错了,这意味着你正在解决问题。在我看来,好像您还没有阅读有关如何使用 Spring Boot 创建战争以进行部署的部分(提示它不仅仅是将打包切换为战争)。
    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多