2015-02-06 13 views
13

Plik, który próbuję przesłać, będzie zawsze plikiem xml. Chcę ustawić typ zawartości jako application/xml Oto mój kod:Jak ustawić typ zawartości pliku w wielostronicowym przesyłaniu podczas korzystania z narzędzia RestTemplate (z klienta odpoczynku)

  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' 

wniosek surowy wygląda następująco:

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 .. 

Zawartość typ pliku jest generowany jako "application/octet-stream", gdzie chcę, aby był "application/xml" Jak mogę ustawić typ zawartości dla pliku?

+0

Spójrz na to: http://stackoverflow.com/questions/11579621/spring -resttemplate-postforobject-with-header-webservice-cant-find-my-header-p lub http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody – Dan

Odpowiedz

20

zorientowali się rozwiązanie po zażyciu podpowiedź z tego linku:

Making a multipart post request with compressed jpeg byte array with spring for android

rozwiązanie jest umieścić ByteArrayResource ja na HttpEntity z wymaganym nagłówkiem i dodaj HttpEntity do Multivaluemap (Zamiast dodawania ByteArrayResource sobie.)

Kod:

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); 
+0

uratował mój dzień, dziękuję! –

+0

zapisał też mój dzień! – Faraway

0

ja nie używałem RestTemplate ale Użyłem HttpClient w przeszłości - tak właśnie przekazać część ciała -

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

trzeba będzie spojrzeć na API w RestTemplate które mogą przyjąć na treści typ

Powiązane problemy