0


我正在开发一个 Android 打印应用程序。我的应用程序的一部分是从用户接收文件的本地服务器。
我在 Tomcat Java servlet 中实现了服务器。

我的问题是,当两台设备同时向服务器发送2个文件时,有两种可能的结果:
1.一个客户端收到一个好的响应,第二个客户端收到一个空响应。
2. 一个客户端收到第二个客户端的响应,反之亦然。

这是我的servlet代码:

    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        new Runnable() {

            @Override
            public void run() {
                try {
                    request.setCharacterEncoding("UTF-8");
                } catch (UnsupportedEncodingException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                response.setCharacterEncoding("UTF-8");
                try {
                    writer = response.getWriter();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    // get access to file that is uploaded from client
                    Part p1 = request.getPart("File");
                    InputStream is = p1.getInputStream();

                    // read filename which is sent as a part
                    Part p2  = request.getPart("MetaData");
                    Scanner s = new Scanner(p2.getInputStream());
                    String stringJson = s.nextLine();    // read filename from stream
                    s.close();

                    json = new JSONObject(stringJson);
                    fileName = new String(json.getString("FileName").getBytes("UTF-8"));
                    fileDirectory = BASE + request.getSession().getId();
                    File dir = new File(fileDirectory);
                    dir.mkdir();
                    // get filename to use on the server
                    String outputfile = BASE + dir.getName() + "/" + fileName;  // get path on the server
                    FileOutputStream os = new FileOutputStream (outputfile);

                    // write bytes taken from uploaded file to target file
                    byte[] buffer = new byte[1024];
                    int ch = is.read(buffer);
                    while (ch != -1) { 
                        os.write(buffer);
                        ch = is.read(buffer);
                    }
                    os.close();
                    is.close();
                }
                catch(Exception ex) {
                    writer.println("Exception -->" + ex.getMessage());
                }
                finally { 
                    try {
                        myRequest = request;
                        try {
                            printFile(request.getSession().getId());
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            writer.println("Exception -->" + e.getMessage());
                        }
                        writer.close();
                    } catch (InterruptedException e) {
                        writer.println("Exception -->" + e.getMessage());
                    }
                }               
            }
        }.run();

    }

tomcat 服务器作为虚拟机在 Ubuntu 13.04 上运行。
任何的想法?

4

3 回答 3

0

您不需要实现 Runnable。默认情况下,Servlet 是线程安全的,这意味着为每个请求创建一个新线程。如果您使用一些静态变量,请确保以线程安全的方式使用它们。我认为您的线程创建可能会困扰 tomcat/container 以不正确的方式发送响应。简而言之,我相信您所做的超出了要求,容器可以为您提供救援。

于 2013-05-03T10:50:24.027 回答
0

任何 Web 容器都管理 servlet 的多线程。您无需实现自己的线程。从您的代码中删除多线程,它将完美运行。

于 2013-05-03T10:51:58.883 回答
0

我不认为使用 Runnable 有什么不同,但它有点毫无意义。我看不到你在哪里声明了你的作家。如果这是 servlet 的一个实例变量(即不在 post 方法中),那么当两个会话同时使用时,它很容易发生会话交换。尝试在 post 方法中声明它。

于 2013-05-03T11:00:24.423 回答