How to upload file with utf-8 symbols in name on jira task as attachment using spring boot

Viewed 297

I am trying to upload file on jira task as attachment.

for example I am trying to upload file with the name "test-äöü.txt" and it appears on jira like "test-???.txt"

Here is my original code:

RestTemplate restTemplate = restTemplateFactory.getInstance(RestTemplateUtil.getRequestFactory(proxyInfo,useEncryption));
File file = new File(filepath,filename);
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<>();
FileSystemResource var = new FileSystemResource(file);
multipartMap.add("file", var);

HttpEntity<MultiValueMap<String, Object>>  request = new HttpEntity<MultiValueMap<String, Object>>(multipartMap,
                            RestTemplateUtil.buildHttpHeadersForUploadAttachment(jiraUser, jiraPass,useEncryption)

ResponseEntity<String> result = restTemplate.exchange( jiraAdressAttachFile, HttpMethod.POST, request, String.class);

I am setting "Authorization" , "X-Atlassian-Token" headers in buildHttpHeadersForUploadAttachment metgod and "Content-Type" with MediaType.MULTIPART_FORM_DATA value

Like I said file goes on jira with name "test-???.txt", than I searched some solutions and tried them but nothing worked for me. Here is the solutions I tried to solve my problem:

restTemplate.getMessageConverters().stream()
             .filter(FormHttpMessageConverter.class::isInstance)
             .map(FormHttpMessageConverter.class::cast)
             .findFirst()
             .orElseThrow(() -> new IllegalStateException("Failed to find FormHttpMessageConverter"))
             .setMultipartCharset(StandardCharsets.UTF_8);

Did not work

restTemplate.getMessageConverters().stream()
             .filter(AllEncompassingFormHttpMessageConverter.class::isInstance)
             .map(AllEncompassingFormHttpMessageConverter.class::cast)
             .findFirst()
             .orElseThrow(() -> new IllegalStateException("Failed to find AllEncompassingFormHttpMessageConverter"))
             .setMultipartCharset(StandardCharsets.UTF_8);

Did not work

restTemplate.getMessageConverters().add(0, new FormHttpMessageConverter() {
                @Override
                protected String getFilename(Object part) {
                    if (part instanceof Resource) {
                        Resource resource = (Resource) part;
                        try {
                            return new String(resource.getFilename().getBytes(StandardCharsets.UTF_8));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                            return null;
                        }
                    } else {
                        return null;
                    }
                }
            });

In this example I tried every possible value of StandardCharsets but again nothing worked

I tried this one too

FormHttpMessageConverter converter = new FormHttpMessageConverter();
            converter.setMultipartCharset(Charset.forName("UTF-8"));
            restTemplate.getMessageConverters().add(0, converter);

and this

List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

            StringHttpMessageConverter stringMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
            ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
            byteArrayHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
            ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();
            resourceHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
            FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
            
            formHttpMessageConverter.setMultipartCharset(Charset.forName("UTF-8"));
            AllEncompassingFormHttpMessageConverter allEncompassingConverter = new AllEncompassingFormHttpMessageConverter();
            allEncompassingConverter.setCharset(Charset.forName("UTF-8"));
            allEncompassingConverter.setMultipartCharset(Charset.forName("UTF-8"));
            allEncompassingConverter.setPartConverters(Arrays.asList(stringMessageConverter,byteArrayHttpMessageConverter,resourceHttpMessageConverter));


for (Iterator<HttpMessageConverter<?>> iterator = messageConverters.iterator(); iterator.hasNext(); ) {
                HttpMessageConverter conv = iterator.next();
                if (conv instanceof AllEncompassingFormHttpMessageConverter) {
                    iterator.remove();
                }
            }
            messageConverters.add(allEncompassingConverter);
            restTemplate.setMessageConverters(messageConverters);

again bad result.

Am I doing something wrong in my original code or in a possible solutions I tried and described? or is there something I did not tried and I must do to work my code properly ?

Before posting request, in File, FileSystemResource and MultiValueMap<String, Object> objects this file has correct name like "test-äöü.txt"

update: issue is with spring boot version less than 2.0

0 Answers
Related