2013-10-26 1 views
1

multiparty 엔티티를 사용하여 파일을 업로드하고 싶습니다. 오디오 파일과 XML 파일을 업로드하고 싶습니다. 일부 머리글을 이미지와 XML 부분에도 설정하려고합니다. 그러나 내가 설정, 내가 Wireshark와의 POST 요청을 확인하면, 내가 예를 들어 XML에 Content-type 헤더가 없습니다 (이 얻을 그러나 MultipartEntity : StringBody의 헤더를 설정할 수 없습니다.

// adding the audio file 
      File f = new File(file_path); 

      FileBody body = new FileBody(f); 
      FormBodyPart fbp = new FormBodyPart("file", body); 
      fbp.addField("Content-Disposition", "form-data"); 
      fbp.addField("name", "\"file\""); 
      fbp.addField("filename", "\"" + fileName + "\""); 
      fbp.addField("Content-Type", "audio/mp4"); 
      fbp.addField("Content-Transfer-Encoding", "binary"); 

      entity.addPart(fbp); 

      // adding the XML file 
      String xml= createAudioInfoXML("test", 6, (int) file.length()); 
      StringBody strBody = new StringBody(xml); 
      FormBodyPart fbp2 = new FormBodyPart("file2",strBody); 
      fbp2.addField("Content-Disposition", "form-data"); 
      fbp2.addField("name", "\"file2\""); 
      fbp2.addField("filename", "\"" + fileName + "\""); 
      fbp2.addField("Content-Type", "text/xml"); 
      fbp2.addField("Content-Transfer-Encoding", "binary"); 

      entity.addPart(fbp2); 

내가 같은 헤더를 설정 : 이것은 내가 그것을 어떻게 그것은) :
POST /upload?recname=2d53352d-c840-48c7-b314-0fc324561ca9 HTTP/1.1 
Content-Type: multipart/form-data; boundary=---------------------------This is the boundary 
Content-Length: 25059 
Host: notes.verba.com 
Connection: Keep-Alive 
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) 

-----------------------------This is the boundary 
Content-Disposition: form-data; name="file"; filename="1382452301277.wav" 
Content-Type: application/octet-stream 

... some data... 

-----------------------------This is the boundary 
Content-Disposition: form-data; name="file2" 

<?xml version="1.0"> 
    <rec> 
     <name>test</name> 
     <length>6</length> 
     <size>24620</size> 
     <date>2013.10.15. 16:42</date> 
    </rec> 
-----------------------------This is the boundary-- 

여기 내 완성 기능입니다 :이 고투 누구든지

private void upload3(File file) { 

     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url + "?recname=" + guid); 

     String boundary = "---------------------------This is the boundary"; 

     MultipartEntity entity = new MultipartEntity(
       HttpMultipartMode.BROWSER_COMPATIBLE, boundary, null); 

     httpPost.addHeader("Content-Type", "multipart/form-data; boundary=" 
       + boundary); 

     try { 

      // adding the audio file 
      File f = new File(file_path); 

      FileBody body = new FileBody(f); 
      FormBodyPart fbp = new FormBodyPart("file", body); 
      fbp.addField("Content-Disposition", "form-data"); 
      fbp.addField("name", "\"file\""); 
      fbp.addField("filename", "\"" + fileName + "\""); 
      fbp.addField("Content-Type", "audio/mp4"); 
      fbp.addField("Content-Transfer-Encoding", "binary"); 

      entity.addPart(fbp); 

      // adding the XML file 
      String xml= createAudioInfoXML("test", 6, (int) file.length()); 

      //entity.addPart("xml", new StringBody(xml,"application/xml",Charset.forName("UTF-8"))); 

      StringBody strBody = new StringBody(xml); 
      FormBodyPart fbp2 = new FormBodyPart("file2",strBody); 
      fbp2.addField("Content-Disposition", "form-data"); 
      fbp2.addField("name", "\"file2\""); 
      fbp2.addField("filename", "\"" + fileName + "\""); 
      fbp2.addField("Content-Type", "text/xml"); 
      fbp2.addField("Content-Transfer-Encoding", "binary"); 

      entity.addPart(fbp2); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     httpPost.setEntity(entity); 

     try { 

      HttpResponse response = httpclient.execute(httpPost); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent(), "UTF-8")); 
      StringBuilder builder = new StringBuilder(); 

      for (String line = null; (line = reader.readLine()) != null;) { 
       builder.append(line).append("\n"); 
      } 

      reader.close(); 
      message = builder.toString(); 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        Toast.makeText(activity, "Error during the upload.", 
          Toast.LENGTH_LONG).show(); 

       } 
      }); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        Toast.makeText(activity, "Error during the upload.", 
          Toast.LENGTH_LONG).show(); 

       } 
      }); 
     } 
    } 

답변

3

이 솔루션은 HttpMultipartMode.BROWSER_COMPATIBLE을 변경했다. 이 코드 라인은 내용 - 처리 및 내용 - 유형 만 메시지에 넣기 때문입니다.

관련 문제