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)。

任何人都可以帮助我如何修改代码,以便它使用HttpRequest(或其子类型)而不是PostMethod?

回答

0

-我已经使用apache-mime library发布带有消息到Web服务器的图像。

这里是我的生产环境中的代码:

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(); 
    }