2012-03-06 1 views
0

Tomcat과 함께 호스팅되는 Java Servlet으로 Android 앱에서 HTTP 요청을 보내려고합니다. 응용 프로그램은 서블릿에 텍스트 및 이미지 데이터를 보내지 만 서블릿은 멀티 파트 양식 데이터를 보지 못합니다. , HttpClient를-4.1.3, httpcore-4.1.4 : http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/Tomcat 서블릿에 Apache http-client : 멀티 파트 엔티티를 수신 할 수 없습니다.

안드로이드 코드 :
//// libs와 나는이 튜토리얼에서 약간의 방향뿐만 아니라 들어오는 데이터를 확인하는 몇 가지 IRC의 도움을 했어 httpmime-4.1.3, 아파치 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); 

서블릿 코드 : 전송

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

실제 데이터 (와이어 샤크으로 확인) :

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 '답장 페이지의 어딘가에.