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.