在Windows上获取套接字超时异常但在Linux上没有

时间:2011-05-13 18:26:24

标签: android android-emulator

我在这里捣乱!我写了一个Android应用程序,将图像从设备上传到servlet。该应用程序在我的Windows 7和Linux PC上的模拟器上无瑕疵地工作。然而,当我在我的真实设备上运行应用程序并且servlet在我的Windows PC上时,我得到一个SocketTimeoutException!但是如果servlet在linux上运行,那就完美了!!我有什么想法在Windows上调整以使其工作?!我甚至将我的应用程序服务器从glassfish改为tomcat,结果仍然相同!任何提示将不胜感激..谢谢

这是从android客户端读取图像的servlet的一部分。我在客户端使用了apache fileupload

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

在我的Windows机器上部署了servlet,我得到了例外          新的ServletFileUpload(new DiskFileItemFactory())。parseRequest(request); 线。

这是将文件发送到servlet的android客户端代码段

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

1 个答案:

答案 0 :(得分:1)

最可能的解释是

  1. 您的设备和Windows机箱不在同一子网(甚至是同一网络)上。你确定你的设备已连接到你的wifi吗?

  2. 您的Windows框有防火墙阻止端口8084.如果您从Windows框运行模拟器,它仍然可以工作。

  3. 您可以尝试在Windows框中查看netstat -ab输出,并确保在正确的端口上看到它正在侦听。