小编RGR*_*RGR的帖子

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

我正在尝试上传的文件将始终是一个xml文件.我想将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'
Run Code Online (Sandbox Code Playgroud)

原始请求如下所示:

    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 …
Run Code Online (Sandbox Code Playgroud)

java rest spring multipartform-data resttemplate

19
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

multipartform-data ×1

rest ×1

resttemplate ×1

spring ×1