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