2015-02-06 4 views
13

Il file che sto tentando di caricare sarà sempre un file xml. Voglio impostare il tipo di contenuto come application/xml Ecco il mio codice:Come impostare il tipo di contenuto per il file nel caricamento multipart quando si utilizza RestTemplate (da un client di riposo)

  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' 

richiesta grezzo assomiglia a questo:

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

Il contenuto-tipo di file sia generato come 'application/octet-stream' dove voglio che sia 'application/xml' Come posso impostare il tipo di contenuto per il file?

+0

Date un'occhiata a questo: http://stackoverflow.com/questions/11579621/spring -resttemplate-postforobject-with-header-webservice-cant-find-my-header-p o http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody – Dan

risposta

20

ho capito la soluzione dopo aver preso spunto da questo link:

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

soluzione è quella di mettere il ByteArrayResource i na HttpEntity con l'intestazione richiesta e aggiungere il HttpEntity a Multivaluemap (Invece di aggiungere ByteArrayResource stesso.)

Codice:

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

mi ha salvato la giornata, grazie! –

+0

ho salvato anche la mia giornata! – Faraway

0

non ho usato RestTemplate ma ho usato HttpClient in passato - questo è come mi passo la parte del corpo -

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

Si dovrà guardare per l'API in RestTemplate che può richiedere Content tipo

Problemi correlati