servlet请求输入流的java线程问题

fjaof16o  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(288)

我对javaweb应用程序中的多线程有一些简单的了解。然而,我在开发android应用程序时遇到了一个问题,这个应用程序通过rest与服务器进行通信。
我们的web应用程序基于ApacheWicket 8.6,并包含额外的httpservlet端点。通过post从应用程序上载图像时应用一个端点。只要我一次只上传一张图片,这就可以了。如果我在android应用程序中连续快速执行多个上传请求(几毫秒),那么只有最后一个上传成功执行(当我在两次上传之间放置第二次中断时,它工作正常)。似乎除了最后一个请求之外的所有请求都缺少从servlet请求的输入流读取的图像内容。因此,我猜我的servlet有线程问题。如果有人能指导我朝着正确的方向解决这个问题,我将不胜感激。

public class MyServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean proceed =true;

        Map<String,String[]> parameters = req.getParameterMap();
        if(parameters!=null){
            //read some parameters which specify the request
        }
        if(proceed) {
            InputStream is = req.getInputStream();
            if(is!=null) {
                //The result of this output is 0 for the first requests only for the last request it is 1
                System.err.println("Stream size: "+is.available());

                //do something
            }
        }
        //do something....
    }
}

当然,我可以使用multipart在一个请求中编写android应用程序中的图像,但是我仍然希望servlet线程在同时收到两个请求的情况下是安全的。我感谢你的帮助。

smdnsysy

smdnsysy1#

因此,经过一些额外的研究,我发现输入流甚至不是空的 is.available() 返回0
我的问题不一样。我将上传的图像保存在modeshape存储库中。存储库会话存储在wicket应用程序示例中。因此,只有一个modeshape存储库会话存在。在写入图像时,modeshape会话似乎有问题。因此,我将modeshape会话放在一个同步块中,现在一切正常。

public class MyServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean proceed =true;

        Map<String,String[]> parameters = req.getParameterMap();
        if(parameters!=null){
            //read some parameters which specify the request
        }
        if(proceed) {
            String fileName = req.getHeader("fileName");
            if(!StringUtils.isEmpty(fileName)) {
                InputStream is = req.getInputStream();
                if(is!=null) {
                    //The result of this output is 0 for the first requests only for the last request it is 1
                    System.err.println("Stream size: "+is.available());
                    WicketApplication app=null;
                    try {
                        app = (WicketApplication)Application.get("wicket");
                    } catch (Exception e) {
                        LOG.error("Error while generating WicketApplication object for project "+project.getName(),e);
                    }
                    if(app!=null) {
                        final Session repoSession = app.getRepoSession();
                        synchronized (repoSession) {
                            //write image to repository
                        }
                    }
                }
            }
        }
        //do something....
    }
}

相关问题