在jsp中获取图像路径

时间:2014-04-23 16:28:26

标签: image jsp jstl

在我的项目中,我必须上传图片并在用户个人资料视图中显示。现在我成功存储,我在显示图像时遇到问题。图像存储在路径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显示路径以及项目路径我如何获得确切的路径用于显示图像。

1 个答案:

答案 0 :(得分:0)

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

<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个请求:

@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