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

纵然是瞬间 提交于 2019-12-22 12:39:08

问题


I have a Spring Boot Rest Service which uploads a file via the RestTemplate exchange method. The upload is working as it should but there is a problem with utf-8 filenames which for example contain german umlauts like äöü. When uploading a file from a HTML5 App it is working without any problem so it is not a problem at the receiving service.

Without setting any encoding for the MultipartCharset the umlauts are replaced by "?" (e.g. Überschrift.txt gets ?berschrift.txt), as US-ASCII is used for the filename encoding. I tried setting the MultipartCharset to UTF-8 with this code:

((AllEncompassingFormHttpMessageConverter)restTemplate.getMessageConverters().get(4)).setMultipartCharset(Charset.forName("UTF-8"));

Then the filename is put like this into the request: Content-Disposition: form-data; name="file"; filename="=?UTF-8?Q?=C3=9Cberschrift.txt?="

The umlauts are encoded but the filename is transfered exactly like this and not with the correct umlauts. I think I am missing some property to set so the umlauts are really set as umlauts in the request.

The relevant part of my code is this:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile),"UTF-8"));    bw.append(capturedDocument.getText());
bw.newLine();
bw.flush();
bw.close();

String complianceServiceUrl = complianceBackendRestSettings.getComplianceServiceURL();
            RestTemplate restTemplate = new RestTemplate();
            ((AllEncompassingFormHttpMessageConverter)restTemplate.getMessageConverters().get(4)).setMultipartCharset(Charset.forName("UTF-8"));
ResponseEntity<JSONBoolean> responseEntity = null;
HttpHeaders uploadHeaders = new HttpHeaders();
uploadHeaders.set("Authorization", authorization);
uploadHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
LinkedMultiValueMap<String, Object> uploadMap = new LinkedMultiValueMap<String, Object>();
uploadMap.add("file", new FileSystemResource(uploadFile.getAbsolutePath()));
uploadMap.add("abonnementId", abos.toString());
                HttpEntity<LinkedMultiValueMap<String, Object>> uploadRequestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
                        uploadMap, uploadHeaders);
responseEntity = restTemplate.exchange(complianceServiceUrl + "/uploadandassign", HttpMethod.POST,
uploadRequestEntity, JSONBoolean.class);

The umlauts in the files are complete correctly, so it's only the problem with the filename encoding.

I would appreciate any hint for a solution to this problem.


回答1:


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();
    }
    



回答2:


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



来源:https://stackoverflow.com/questions/39675773/spring-boot-1-2-5-release-resttemplate-multipart-file-upload-utf-8-filename-no

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