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 Skokan <[email protected]> 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 > (90000)); // 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, rezar <[email protected]> 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(); > > } > > } > > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ > > At the android side (SDK v.2.0 ): > > private HttpPost post; > > public void doUpload(String filepath,String filename) { > > HttpClient httpClient = new DefaultHttpClient(); > > try { > > > > httpClient.getParams().setParameter("http.socket.timeout", new > > Integer(90000)); // 90 second > > post = new HttpPost(new URI(YOUR_SERVER_ADDRESS)); > > File file = new File(filepath); > > FileEntity entity; > > if (filepath.substring(filepath.length()-3, > > filepath.length > > ()).equalsIgnoreCase("txt") || > > filepath.substring(filepath.length()-3, > > filepath.length > > ()).equalsIgnoreCase("log")) { > > entity = new FileEntity(file,"text/plain; > > charset=\"UTF-8\""); > > entity.setChunked(true); > > }else { > > entity = new > > FileEntity(file,"binary/octet-stream"); > > entity.setChunked(true); > > } > > post.setEntity(entity); > > post.addHeader(FILENAME_STR, filename); > > > HttpResponse response = httpClient.execute(post); > > if (response.getStatusLine().getStatusCode() != > > HttpStatus.SC_OK) { > > Log.e(TAG,"--------Error--------Response > > Status line > > code:"+response.getStatusLine()); > > }else { > > // Here every thing is fine. > > } > > HttpEntity resEntity = response.getEntity(); > > if (resEntity == null) { > > Log.e(TAG,"---------Error No Response > > !!!-----"); > > } > > } catch (Exception ex) { > > Log.e(TAG,"---------Error-----"+ex.getMessage()); > > ex.printStackTrace(); > > } finally { > > httpClient.getConnectionManager().shutdown(); > > } > > } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

