0

httpClient 라이브러리를 사용하여 이미지 (다중 부분/양식 데이터)를 업로드하려고합니다. httpPost 메서드 및 byteArrayRequestEntity를 사용하여 이미지를 업로드 할 수 있습니다. 내가 사용하는 코드는 다음과 같은 :HTTP MultiPart 요청

File file = new File(imageFilePath); 

HttpClient client = new HttpClient(); 

PostMethod method = new PostMethod("https://domain/link/folderId/files?access_token="+accessToken); 


method.addRequestHeader("Content-Type","multipart/form-data;boundary=AaB03x"); 

String boundary = "AaB03x"; 

StringBuilder builder = new StringBuilder(); 
builder.append("--"); 
builder.append(boundary+"\r\n"); 
builder.append("Content-Disposition: form-data; name=\"file\"; filename=\"photo.jpg\""); 
builder.append("\r\n"); 
builder.append("Content-Type: image/jpeg"); 
builder.append("\r\n"); 
builder.append("\r\n"); 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
baos.write(builder.toString().getBytes("utf-8")); 
builder.setLength(0); 

InputStream is = new FileInputStream(file); 
byte[] buffer = new byte[4096]; 
int nbRead = is.read(buffer); 
while(nbRead > 0) { 
    baos.write(buffer, 0, nbRead); 
    nbRead = is.read(buffer); 
} 

is.close(); 
builder.append("\r\n"); 
builder.append("--"); 
builder.append(boundary); 
builder.append("--"); 
builder.append("\r\n"); 

baos.write(builder.toString().getBytes("utf-8")); 

method.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), "multipart/form-data; boundary=\"" + boundary + "\"")); 


System.out.println(method.getRequestEntity().toString()); 
client.executeMethod(method); 

그러나 내가 작업하고있는 프로젝트는 HttpRequest를하지 HTTP를 PostMethod를 사용하는 저를 필요로한다. basicHttpEntityEnclosingRequest로 시도했지만 동일한에 대한 setEntity 메서드는 httpEntity 만받습니다 (ByteArrayRequestEntity를 사용하고있었습니다).

누구든지 코드를 수정하여 PostMethod 대신 HttpRequest (또는 해당 하위 유형)를 사용하도록 도와 줄 수 있습니까?

답변

0

- 웹 서버에 메시지로 이미지를 게시하는 데 apache-mime library을 사용했습니다.

public String postDataCreation(final String url, final String xmlQuery,final String path){ 

     final StringBuilder sa = new StringBuilder(); 

     final File file1 = new File(path); 




     Thread t2 = new Thread(new Runnable(){ 


      public void run() { 

       try 
       { 
        HttpClient client = new DefaultHttpClient(); 
        HttpPost post = new HttpPost(url); 
        FileBody bin1 = new FileBody(file1); 

        MultipartEntity reqEntity = new MultipartEntity(); 

        reqEntity.addPart("dish_photo", bin1); 

        reqEntity.addPart("xml", new StringBody(xmlQuery)); 

        post.setEntity(reqEntity); 

        HttpResponse response = client.execute(post); 

        HttpEntity entity = response.getEntity(); 
         InputStream i = entity.getContent(); 

         Log.d("Vivek", i.toString()); 
         InputStreamReader isr = new InputStreamReader(i); 

         BufferedReader br = new BufferedReader(isr); 

         String s = null; 


         while ((s = br.readLine()) != null) { 

          Log.d("YumZing", s); 
          sa.append(s); 
         } 


         Log.d("Check Now",sa+""); 



       } 
       catch (Exception ex){ 
        Log.e("Debug", "error: " + ex.getMessage(), ex); 
       } 

      } 





     }); 

     t2.start(); 

     try { 
      t2.join(); 
     } catch (InterruptedException e) { 

      e.printStackTrace(); 
     } 

     System.out.println("Getting from Post Data Method "+sa.toString()); 
     return sa.toString(); 
    } 
: 여기

내 생산 환경에서 코드입니다