Spring Boot (1.2.5.RELEASE) Resttemplate Multipart File Upload UTF-8 Filename not possible

感情迁移 提交于 2019-12-06 06:43:10

I also faced same issue, problem is that Spring's RestTemplate follows RFC 2047, but StandardMultipartHttpServletRequest supports only RFC 6266 format, or headers needs to be in UTF-8 already (what is some browsers behavior).

I've already filled bug request. I've just verified that commons-fileupload library will handle this correctly. If you are using Spring Boot, you will need:

  1. Add commons-fileupload 1.3.2 on classpath

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.2</version>
    </dependency>
    
  2. Disable MultipartAutoConfiguration - for example by property spring.http.multipart.enabled=false or by @EnableAutoConfiguration(exclude={MultipartAutoConfiguration.class})

  3. Define MultipartResolver in your configuration class

    @Bean
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
    
Mihai Tudor

I was facing the same problem, but setting the multipartCharset fixed it for me. Your client code looks correct, and the filename is correctly encoded in RFC 2047 format. This encoding is necessary because HTTP only accepts ascii in its headers. (What character encoding should I use for a HTTP header?)

On the server it should then be decoded back to Überschrift.txt. I'm not completely sure which spring component does this (assuming your serverside is also written in Spring), but I assume it's the multipart resolver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!