0

I have a problem uploading a file with Spring RestTemplate. If the file contains characters with an accent, the original filename is not encoded correctly and not sent over the wire correctly. The characters with an accent are displayed with a question mark on the server.

If I use Postman or Advanced Rest Client, it works. When sniffing using wireshark, I can see that both tools encode the filename differently. Anybody got an idea on how to make RestTemplate handle accents in filenames correctly?

Below is the code I am using to call my webservice endpoint.

final String fileName = "Sécurité report.pdf";
final LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new ClassPathResource(fileName));
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

final HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);

final RestTemplate restTemplate = new RestTemplate();
final ResponseEntity<String> result = restTemplate.exchange("http://localhost:8080", HttpMethod.POST, requestEntity, String.class);
Tammeuh
  • 21
  • 1
  • 6

2 Answers2

1

You need to add a UTF-8 message converter to the RestTemplate

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
    .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

See also: How can I tell RestTemplate to POST with UTF-8 encoding?

Community
  • 1
  • 1
Nathan Russell
  • 3,428
  • 5
  • 30
  • 51
0

Configure your RestTemplate with custom FormHttpMessageConverter instance with property multipartCharset set to UTF-8.

FormHttpMessageConverter converter = new FormHttpMessageConverter();
converter.setMultipartCharset(Charset.forName("UTF-8"));
Alexander Yanyshin
  • 1,310
  • 10
  • 8
  • I see an effect, but the result is not yet as expected. The original filename field on the server now contains: =?UTF-8?Q?S=C3=A9curit=C3=A9_report.pdf?= Postman can send the filename and it simply reappears normally on the other side. – Tammeuh Dec 18 '16 at 21:24
  • If I remove the filter, this is what I get. S?curit? report.pdf – Tammeuh Dec 18 '16 at 21:25
  • A misconfiguration on my end. On an all new project, this works.I do find it strange that Spring uses this strategy to encode ISO-8859-1 content as well. I would expect it to be passed as is. Some RFCs mention ASCII as the required encoding for the filename header in a multipart, others mention ISO-8859-1. In any case, this way it works. The only question remaining is how portable is this solution? – Tammeuh Dec 21 '16 at 08:58
  • I think the only way to be 100% sure is test file upload against your target servers. AFAIK `encoded-word` method which `FormHttpMessageConverter` uses to encode non-ASCII flenames is described in [RFC2047](https://tools.ietf.org/html/rfc2047), recommended per [RFC2388](https://tools.ietf.org/html/rfc2388) and supported by every major webserver. – Alexander Yanyshin Dec 21 '16 at 21:14