2012-03-06 45 views
0

我試圖從Android應用程序發送一個HTTP請求到Tomcat託管的Java Servlet。該應用程序會向servlet發送一些文本和圖像數據,但該servlet似乎沒有看到多部分表單數據。我已經從這個教程一點方向,以及與確認輸入數據的一些IRC幫助:http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/Apache HTTP客戶端到Tomcat servlet:無法接收多部分實體

的Android代碼:
////庫:HttpClient的-4.1.3,的HttpCore-4.1.4, httpmime-4.1.3,Apache的mime4j核-0.7.2

HttpPost httpPost = new HttpPost(mURI); 
MultipartEntity requestEntity = new MultipartEntity(); 
requestEntity.addPart("text", new StringBody("test text")); 
requestEntity.addPart("image", new ByteArrayBody(mImage, "image")); 
httpPost.setEntity(requestEntity); 
HttpResponse httpResponse = mHttpClient.execute(httpPost); 

servlet代碼:

@Override 
public void doPost(HttpServletRequest request, HttpServletResponse response) { 
    try { 
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 

     File file = new File(getServletContext().getRealPath("/") + File.separator + "WEB-INF" + File.separator + "sms-mobile-image.jpg"); 
     file.createNewFile(); 
     Writer outfile = new OutputStreamWriter(new FileOutputStream(file)); 
     List<Part> formData = new ArrayList(request.getParts()); 
     if(formData.size()>0) 
      System.out.println(formData.get(0).getName()); 
     else 
      System.out.println("no form data found"); 
    } catch(Exception e) { 
     System.out.println(e.toString()); 
    } 
} 

發送實際數據(已確認使用Wireshark):

POST /mobile-image/ProcessRequest HTTP/1.1 
Content-Length: 921897 
Content-Type: multipart/form-data; boundary=ZiB5ibYqpxux_mP6HeswY9B__17vOLCVvay01 
Host: 192.168.1.167:8080 
Connection: Keep-Alive 

--ZiB5ibYqpxux_mP6HeswY9B__17vOLCVvay01 
Content-Disposition: form-data; name="text" 
Content-Type: text/plain; charset=US-ASCII 
Content-Transfer-Encoding: 8bit 

test text 
--ZiB5ibYqpxux_mP6HeswY9B__17vOLCVvay01 
Content-Disposition: form-data; name="image"; filename="image" 
Content-Type: application/octet-stream 
Content-Transfer-Encoding: binary 

[image-data-here] 
--ZiB5ibYqpxux_mP6HeswY9B__17vOLCVvay01-- 

輸出:

no form data found 

有人建議我通過web.xml配置看,所以那將是我的下一個步驟,但我茫然的感覺在這裏。這不是它應該工作的方式嗎?

回答

0

我能夠通過使用com.oreilly.servlet包來解決這個問題。這是一個恥辱,但我仍然認爲我可能做錯了什麼。這似乎是應該內置到httpclient庫中的東西...無論如果你感興趣,有很多使用這個庫的例子,我跟隨的是這裏: http://www.jguru.com/faq/view.jsp?EID=1045507

發表於srinivas'在頁面下方的回覆。

相關問題