Spring Boot(1.2.5.RELEASE)Resttemplate多部分文件上传UTF-8文件名不可能 [英] Spring Boot (1.2.5.RELEASE) Resttemplate Multipart File Upload UTF-8 Filename not possible

查看:473
本文介绍了Spring Boot(1.2.5.RELEASE)Resttemplate多部分文件上传UTF-8文件名不可能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot Rest服务,通过RestTemplate交换方法上传文件。上传是正常工作,因为它应该是utf-8文件名有问题,例如包含像äöü这样的德语变音单。
从HTML5应用程序上传文件时,它的工作没有任何问题,因此在接收服务中不会出现问题。



不要为多音节变奏曲由?替代(例如Überschrift.txt获取?berschrift.txt),因为US-ASCII用于文件名编码。我尝试使用以下代码将MultipartCharset设置为UTF-8:

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

然后将文件名放在请求中:
Content-Disposition:form-数据; NAME = 文件;文件名==?UTF-8?Q?= C3 = 9Cberschrift.txt?=



变音符是编码的,但文件名完全像这样传输,正确的变音器。我想我缺少一些财产来设置,所以在请求中,变音符真的设置为变音符。



我的代码的相关部分是这样的:

  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(授权,授权);
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);

文件中的变音符是完整的,所以这只是文件名编码的问题。 p>

我希望有任何解决这个问题的提示。

解决方案

我也遇到同样的问题,问题是Spring的 RestTemplate 遵循 RFC 2047 ,但 StandardMultipartHttpServletRequest 仅支持 RFC 6266 格式,或标题需要在UTF-8中(某些浏览器的行为是什么)。



我已经填写错误请求。我刚刚验证了commons-fileupload库将正确处理这个。如果您使用的是Spring Boot,您将需要:


  1. 在类路径上添加commons-fileupload 1.3.2

     <依赖关系> 
    < groupId> commons-fileupload< / groupId>
    < artifactId> commons-fileupload< / artifactId>
    < version> 1.3.2< / version>
    < / dependency>


  2. 禁用MultipartAutoConfiguration - 例如通过属性 spring.http.multipart .enabled = false @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})


  3. 在配置类中定义MultipartResolver

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



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.

解决方案

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

这篇关于Spring Boot(1.2.5.RELEASE)Resttemplate多部分文件上传UTF-8文件名不可能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆