I am dumstruck he开发者_JAVA技巧re! I've written an android app that uploads an image from a device to a servlet. The app works FLAWLESSLY on the emulator on both my windows 7 and linux pcs. However when i run the app on my real device and the servlet is on my windows pc, I get a SocketTimeoutException! But if the servlet is running on the linux pc, it works perfectly!! Any ideas what i have to tweak on windows to get this to work?! I even changed my application servers from glassfish to tomcat and still the same results!! Any tips would be appreciated.. Thanks
Here's part of the servlet that reads the image from the android client. I used apache fileupload at the client end
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.getFieldName().equals("imgFile")) {
String fileName = item.getName();
InputStream fileContent = item.getInputStream();
int d;
FileOutputStream fout = new FileOutputStream(new File( DIR + "savedImage.jpg"));
while((d = fileContent.read()) != -1)
{
fout.write(d);
}
fout.close();
}
}
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
With the servlet deployed on my windows machine, i get the exception at the new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); line.
Here's the android client code segment that sends the file to the servlet
File f = new File("/mnt/sdcard/img/imgToUpload.jpg");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.2.2:8084/WebApplication5/imgServlet");
MultipartEntity entity = new MultipartEntity();
FileBody fb = new FileBody(f);
entity.addPart("imgFile", fb);
post.setEntity(entity);
try {
HttpResponse servletResponse = client.execute(post);
HttpEntity respentity = servletResponse.getEntity();
} catch (IOException ex) {
Logger.getLogger(FTXWActivity.class.getName()).log(Level.SEVERE, null, ex);
}
The most likely explanations are
That your device and your Windows box are not on the same subnet (or even the same network). Are you sure your device is connected up to your wifi?
Your windows box has a firewall blocking port 8084. If you were running the emulator from your Windows box, it still would have worked.
You might try looking at netstat -ab output on the windows box and make sure you see it listening on the right port.
精彩评论