简体   繁体   English

如何在使用RestTemplate(来自其他客户端)时为分段上传中的文件设置内容类型

[英]How to set content-type for the file in multipart upload when using RestTemplate (from a rest client)

The file i'm trying to upload will always be a xml file. 我正在尝试上传的文件将始终是一个xml文件。 I want to set the content-type as application/xml Here is my code: 我想将content-type设置为application / xml这是我的代码:

         MultiValueMap<String, Object parts = new LinkedMultiValueMap<String,
         Object(); parts.add("subject", "some info"); 
         ByteArrayResource xmlFile = new    ByteArrayResource(stringWithXMLcontent.getBytes("UTF-8")){
                 @Override
                 public String getFilename(){
                     return documentName;
                 }             
             };

     parts.add("attachment", xmlFile);

//sending the request using RestTemplate template;, the request is successfull 
String result = template.postForObject(getRestURI(), httpEntity,String.class);      
//but the content-type of file is 'application/octet-stream'

the raw request looks like this: 原始请求如下所示:

    Content-Type:
    multipart/form-data;boundary=gbTw7ZJbcdbHIeCRqdX81DVTFfA-oteHHEqgmlz
    User-Agent: Java/1.7.0_67 Host: some.host Connection: keep-alive
    Content-Length: 202866

    --gbTw7ZJbcdbHIeCRqdX81DVTFfA-oteHHEqgmlz Content-Disposition: form-data;    name="subject" Content-Type: text/plain;charset=ISO-8859-1
    Content-Length: 19

    some info

    --gbTw7ZJbcdbHIeCRqdX81DVTFfA-oteHHEqgmlz Content-Disposition: form-data;   name="attachment"; filename="filename.xml" Content-Type:
    application/octet-stream Content-Length: 201402

    ....xml file contents here ..

The content-type of the file is being generated as 'application/octet-stream' where as i want it to be 'application/xml' How can i set the content type for the file? 文件的内容类型生成为'application / octet-stream',我希望它是'application / xml'我如何设置文件的内容类型?

I figured out the solution after taking hint from this link: 我从这个链接中获取提示后想出了解决方案:

Making a multipart post request with compressed jpeg byte array with spring for android 使用带有spring for android的压缩jpeg字节数组进行多部分发布请求

Solution is to put the ByteArrayResource in a HttpEntity with required header and add the HttpEntity to Multivaluemap (Instead of adding ByteArrayResource itself.) 解决方法是将ByteArrayResource放在带有必需头的HttpEntity中,并将HttpEntity添加到Multivaluemap(而不是添加ByteArrayResource本身。)

Code: 码:

Resource xmlFile = new ByteArrayResource(stringWithXMLcontent.getBytes("UTF-8")){
            @Override
            public String getFilename(){
                return documentName;
            }
        };
        HttpHeaders xmlHeaders = new HttpHeaders();
        xmlHeaders.setContentType(MediaType.APPLICATION_XML);
        HttpEntity<Resource> xmlEntity = new HttpEntity<Resource>(xmlFile, xmlHeaders);
        parts.add("attachment", xmlEntity);

I've not used RestTemplate but i've used HttpClient in past - This is how i pass the body part - 我没有使用RestTemplate,但我过去使用过HttpClient - 这就是我传递身体部位的方法 -

MultipartEntityBuilder eb = MultipartEntityBuilder.create().setBoundary(MULTIPART_BOUNDARY)
                .addTextBody(BODYPART_ENTITY, key, ContentType.create("application/xml", Charset.forName("UTF-8")));

You will have to look at for API in RestTemplate which can take content-type 您将不得不在RestTemplate中查看可以采用内容类型的API

As i can not comment the answer of @RGR I'm posting this as new answer although RGR's answer is absolutely correct. 由于我无法评论@RGR的答案,我将此作为新答案发布,尽管RGR的答案绝对正确。

The problem is, that the Sprint RestTemplates uses FormHttpMessageConverter to send the multi part request. 问题是,Sprint RestTemplates使用FormHttpMessageConverter发送多部分请求。 This converter detects everything that inherits from Resource and uses this as the request's "file" part. 此转换器检测从Resource继承的所有内容,并将其用作请求的“文件”部分。 eg If you use a MultiValueMap every property you add will be send in it's own part as soon as you add a "Resource"...--> Setting filename, Mime-Type, length,.. will not be part of the "file part". 例如,如果您使用MultiValueMap,只要添加“资源”,您添加的每个属性都将在其自己的部分中发送...-->设置文件名,Mime-Type,length,..将不会成为“档案部分“。

Not an answer, but it's the explanation why ByteArrayResource must be extended to return the filename and be send as the only part of the request. 不是答案,但它解释了为什么必须扩展ByteArrayResource以返回文件名并作为请求的唯一部分发送。 Sending multiple files will work with a MultiValueMap 发送多个文件将适用于MultiValueMap

It looks like this behaviour was fixed in Spring 4.3 by SPR-13571 SPR-13571在Spring 4.3中修复了这种行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 restTemplate 下载内容类型八位字节流的文件 - Download file of content-type octet-stream using restTemplate 使用Apache REST客户端上传多部分文件 - Multipart File upload using Apache REST client Multipart,设置一部分的 Content-Type - Multipart, set Content-Type of one part springboot RestTemplate 调用 url 和 https 响应头(内容类型:audio/wav),如何保存为 *.wav 文件? - springboot RestTemplate call url and https response headers(content-type:audio/wav) ,how to save as *.wav file? Quarkus rest-client 多部分文件上传 - Quarkus rest-client multipart file upload 使用RestTemplate的Spring Boot多部分内容类型HTTP请求 - Spring Boot multipart content type HTTP Request using RestTemplate 如何在Mulesoft中使用HTTP连接器上传具有内容类型multipart / form数据的文件 - How to upload a file using HTTP connector with content type multipart/form data in mulesoft REST客户端的Content-Type标头引发UnsupportedOperationException - REST client Content-Type header throws UnsupportedOperationException 使用多部分上传图像时如何在客户端获取过程 - How to get process in client side when upload image using multipart 使用Spring RestTemplate和Jackson分段上传文件 - Multipart file upload with spring RestTemplate and Jackson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM