[android-developers] Re: Uploading a File with HttpClient and HttpPost
Hello, has anybody tried this? http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro On 1月24日, 上午6時06分, Hubert Behaghel behag...@gmail.com wrote: Have you tried without the header Expect: 100-Continue ? HttpProtocolParams.setUseExpectContinue(client.getParams(), false); -- Hubert On Wed, Dec 30, 2009 at 11:50 AM, Radek Skokan radek.skokan.c...@gmail.com wrote: As you can see from the sniffed packets, the problem is not in establishing network connection, but on a higher level: HTTP multipart file upload using Apache HttpClient from Android SDK Apache Commons File Upload. Radek On 12/24/2009 11:11 PM, jotobjects wrote: Apparently your HTC is able to connect over WIFI to your router and find a local network laptop running the servlet. Have you tried having the emulator running on the laptop connect to localhost using port 10.0.2.2? That could at least establish that the code works (or not) in that situation. See this link: http://developer.android.com/intl/zh-CN/resources/faq/commontasks.htm... On Dec 23, 10:38 pm, Radek Skokanradek.skokan.c...@gmail.com wrote: I tried it on both the emulator and a physical device (HTC Hero). Same behavior. 192.168.1.100 is my laptop running Tomcat. There is a servlet with Apache Commons File upload waiting for that multipart file. Works correctly with uploads from a browser (PC Android). Thanks, R. On 12/23/2009 10:56 PM, jotobjects wrote: Are you running this on the emulator? Is 192.168.1.100 a different computer on the same router? On Dec 22, 4:51 am, Radek Skokanradek.skokan.c...@gmail.com wrote: Hi, I'm trying to upload a file from Android to Tomcat server using HttpClient and HttpPost. The android-side code is: HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(http.socket.timeout, new Integer (9)); // 90 second HttpPost httpPost = new HttpPost(http://192.168.1.100:8080/ mediator/upload); FileEntity entity = new FileEntity(new File(filePath), binary/ octet-stream); httpPost.setEntity(entity); HttpResponse response = client.execute(httpPost); Log.i(TAG, Upload finished. Status: + response.getStatusLine()); client.getConnectionManager().shutdown(); The result always is HTTP/1.1 400 Bad Request. I captured the data sent over the network and it looks like: POST /mediator/upload HTTP/1.1 Content-Length: 8287 Content-Type: binary/octet-stream Host: 192.168.1.100:8080 Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) Expect: 100-Continue HTTP/1.1 100 Continue HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Content-Type: text/html;charset=UTF-8 Content-Length: 971 Date: Tue, 22 Dec 2009 12:14:47 GMT Connection: close I created a simple HTML form for POSTing a file. This works fine from browser and the data is different: 1) it doesn't use HTTP 100 continue 2) Content-Type: multipart/form-data; boundary= WebKitFormBoundaryDlIVGYpQG46Q46So In my client I tried to change the content type to other values than binary/octet-stream, but when I sniff the traffic, the binary/octet- stream is still there. I do this on HTC Here/SDK 1.5. Thanks, Radek On Nov 15, 11:46 am, rezarrraw...@gmail.com wrote: I solved the problem. I post my code here for others to have a working sample: on the server side I made a simple servlet: --- --- private void receiveFile(HttpServletRequest req, HttpServletResponse resp) throws Exception { Enumeration emns = req.getHeaderNames(); InputStream is = req.getInputStream(); OutputStream os = new FileOutputStream(req.getHeader(FILENAME_STR)); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); } protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { try { receiveFile(request,response); } catch (Exception e) { System.err.println(ERROR-+e.getMessage()); e.printStackTrace(); } }
[android-developers] Re: Uploading a File with HttpClient and HttpPost
http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro On 1月24日, 上午6時06分, Hubert Behaghel behag...@gmail.com wrote: Have you tried without the header Expect: 100-Continue ? HttpProtocolParams.setUseExpectContinue(client.getParams(), false); -- Hubert On Wed, Dec 30, 2009 at 11:50 AM, Radek Skokan radek.skokan.c...@gmail.com wrote: As you can see from the sniffed packets, the problem is not in establishing network connection, but on a higher level: HTTP multipart file upload using Apache HttpClient from Android SDK Apache Commons File Upload. Radek On 12/24/2009 11:11 PM, jotobjects wrote: Apparently your HTC is able to connect over WIFI to your router and find a local network laptop running the servlet. Have you tried having the emulator running on the laptop connect to localhost using port 10.0.2.2? That could at least establish that the code works (or not) in that situation. See this link: http://developer.android.com/intl/zh-CN/resources/faq/commontasks.htm... On Dec 23, 10:38 pm, Radek Skokanradek.skokan.c...@gmail.com wrote: I tried it on both the emulator and a physical device (HTC Hero). Same behavior. 192.168.1.100 is my laptop running Tomcat. There is a servlet with Apache Commons File upload waiting for that multipart file. Works correctly with uploads from a browser (PC Android). Thanks, R. On 12/23/2009 10:56 PM, jotobjects wrote: Are you running this on the emulator? Is 192.168.1.100 a different computer on the same router? On Dec 22, 4:51 am, Radek Skokanradek.skokan.c...@gmail.com wrote: Hi, I'm trying to upload a file from Android to Tomcat server using HttpClient and HttpPost. The android-side code is: HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(http.socket.timeout, new Integer (9)); // 90 second HttpPost httpPost = new HttpPost(http://192.168.1.100:8080/ mediator/upload); FileEntity entity = new FileEntity(new File(filePath), binary/ octet-stream); httpPost.setEntity(entity); HttpResponse response = client.execute(httpPost); Log.i(TAG, Upload finished. Status: + response.getStatusLine()); client.getConnectionManager().shutdown(); The result always is HTTP/1.1 400 Bad Request. I captured the data sent over the network and it looks like: POST /mediator/upload HTTP/1.1 Content-Length: 8287 Content-Type: binary/octet-stream Host: 192.168.1.100:8080 Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) Expect: 100-Continue HTTP/1.1 100 Continue HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Content-Type: text/html;charset=UTF-8 Content-Length: 971 Date: Tue, 22 Dec 2009 12:14:47 GMT Connection: close I created a simple HTML form for POSTing a file. This works fine from browser and the data is different: 1) it doesn't use HTTP 100 continue 2) Content-Type: multipart/form-data; boundary= WebKitFormBoundaryDlIVGYpQG46Q46So In my client I tried to change the content type to other values than binary/octet-stream, but when I sniff the traffic, the binary/octet- stream is still there. I do this on HTC Here/SDK 1.5. Thanks, Radek On Nov 15, 11:46 am, rezarrraw...@gmail.com wrote: I solved the problem. I post my code here for others to have a working sample: on the server side I made a simple servlet: --- --- private void receiveFile(HttpServletRequest req, HttpServletResponse resp) throws Exception { Enumeration emns = req.getHeaderNames(); InputStream is = req.getInputStream(); OutputStream os = new FileOutputStream(req.getHeader(FILENAME_STR)); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); } protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { try { receiveFile(request,response); } catch (Exception e) { System.err.println(ERROR-+e.getMessage()); e.printStackTrace(); } }