【问题标题】:getting image path in jspjsp中获取图片路径
【发布时间】:2014-04-23 16:28:25
【问题描述】:

在我的项目中,我必须上传图像并将其显示在用户个人资料视图中。现在我成功存储了,我在显示图像时遇到了问题。图像存储路径D:/uploads所以我在jsp中的图像检索代码是,

<c:set var="fileanme2" value="${teacherId.getPhoto()}"></c:set>
<%
String uploadFilePath2 = "D:" + "/" + "uploads";
%>
<c:set var="shlash2" value="/"></c:set>
<c:set var="pathValue2" value="<%=uploadFilePath2%>"></c:set>
<c:set var="string4" value="${pathValue2}${shlash2}${fileanme2}" />

<img alt="Image" src="${string4}" width="160" height="160"
class="img-thumbnail">

但是图像没有显示,当我在 src 属性中检查浏览器中的图像元素时,它显示路径为D:/uploads/img,当我将鼠标悬停在它上面时,它会显示路径以及项目路径我怎样才能获得显示的确切路径图片。

【问题讨论】:

  • "D:" + "/" + "uploads""D:/uploads"有区别吗?

标签: image jsp jstl


【解决方案1】:

不需要任何字符串连接和任何额外的工作,你可以在一行中完成:

<c:set var="filePath" value="D:/uploads/${teacherId.getPhoto()}" />

您的问题的解决方案:

1)如果您要显示来自本地文件系统的图像,请执行以下操作:

<img src="file:///D|/uploads/image_name.jpg"
     width="200"
     height="200"
     alt="Image"/>

警告:您发布网站时可能无法访问图片。


2) 创建一个 servlet 来处理所有图像的 GET 请求,方法是在 url 中传递图像名称,例如:

@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("getting photo...");
        String imageName = request.getParameter("imageName");
        System.out.println("imageName: "+imageName);
        //set your upload path
        File imageFile = new File("D:\\uploads\\"+imageName);
        System.out.println("file exists: "+imageFile.exists());

        // Get content type by filename.
        String contentType = getServletContext().getMimeType(imageFile.getName());

        // Init servlet response.
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType(contentType);
        response.setHeader("Content-Length", String.valueOf(imageFile.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"" + imageFile.getName() + "\"");

        // Prepare streams.
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            // Open streams.
            input = new BufferedInputStream(new FileInputStream(imageFile), DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

            // Write file contents to response.
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            // Gently close streams.
            close(output);
            close(input);
        }
        // Check if file is actually an image (avoid download of other files by hackers!).
        // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
        if (contentType == null || !contentType.startsWith("image")) {
            // Do your thing if the file appears not being a real image.
            // Throw an exception, or send 404, or show default/warning image, or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

    }

    // Helpers (can be refactored to public utility class) 
    private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                // Do your thing with the exception. Print it, log it or mail it.
                e.printStackTrace();
            }
        }
    }
}

然后在jsp中使用img标签:

<img alt="Image"
     src="<c:url value="/ImageServlet?imageName=${teacherId.getPhoto()}"/>" />

来源:BalusC image servlet

【讨论】:

  • @Raghavendra 2nd 也应该可以工作,并且比第一个解决方案更喜欢第二个解决方案。
  • @Raghavendra 遇到任何问题的第二个解决方案怎么样?让我知道。
  • @Raghavendra 如果此答案有帮助,则标记为答案。
猜你喜欢
  • 2012-06-05
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 2017-03-10
相关资源
最近更新 更多