Java Web上传的图像没有显示

时间:2015-09-11 21:58:48

标签: java image jsp tomcat servlets

我使用tomcat 8并通过以下代码将图片上传到服务器:

public static String uploadImage(String description, HttpServletRequest request) throws ServletException {
        Part filePart;
        try {
            filePart = request.getPart(PARAM_NAME_IMAGE);
            try(OutputStream out = new FileOutputStream(new File(PATH + description + ".jpg"));
                InputStream fileContent = filePart.getInputStream()){
                int read;
                byte[] bytes = new byte[1024];
                while ((read = fileContent.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
            }
        } catch (IOException e) {
            LOG.error("IOException", e);
        }
        return PATH_TO_IMAGE + description + ".jpg";
    }

然后我将图像路径保存到我的项目的DB。然后我想通过使用:

在页面上看到它
<img src="${item.imagePath}" width="350" height="320" />

But here is what I see

And then if I restart server everything is ok and its shown

所以,我需要看看我上传的内容而不重启服务器

1 个答案:

答案 0 :(得分:-1)

您的HTTP WEB服务器和Java服务器容器(Servlet或Enterprise容器)是否已配置为您的请求是多部分请求? multipart / form-data规范(RFC-2388)。我会在请求上的getParts之后遍历表单数据。