显示图像,其中“图像"标签的SRC属性为“相对路径" [英] Display image where SRC attribute of Image tag is Relative Path

查看:254
本文介绍了显示图像,其中“图像"标签的SRC属性为“相对路径"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将图像上传到

C:\Users\MyComputerName\Desktop\MyWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyWebApp\data

图像名称示例lala.jpg保存在数据库中.

And the image name example lala.jpg is saved in database.

现在我正在尝试在我的jsp中显示图像.

Now I am trying to display the image in my jsp.

我发现我们需要创建一个servlet,该servlet可以从您的Web容器外部加载文件,然后将文件写入/流式传输到您的响应中. 其中一个示例是BalusC的文件Servlet .

I found that we need to create a servlet that can load file from outside of your web container and then write/stream file to your response. One of the example is File Servlet by BalusC.

我尝试关注,但无法显示图片.

I tried to follow but I did not able to display the image.

有人可以指出我的错误吗?帮助将不胜感激.谢谢! :)

Can anyone point out my mistakes? Help will be appreciate. Thanks! :)

下面是我的代码.

jsp

<table>
<thead>
<tr>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<tbody>

<c:forEach items="${staff}" var="staff">
<tr>
<td>${staff.staffName}</td>
<td><img src="FileServlet?path=C:\Users\MyComputerName\Desktop\MyWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyWebApp\data\${staff.staffImage}"></td>
</tr>
</c:forEach>
</tbody>
</table>

文件Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Get requested file by path info.
        String requestedFile = request.getParameter("path");

        System.out.println(requestedFile);

        // Decode the file name (might contain spaces and on) and prepare file object.
        File file = new File(requestedFile);

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

        // If content type is unknown, then set the default value.
        // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
        // To add new content types, add new mime-mapping entry in web.xml.
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

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

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

        try {
            // Open streams.
            input = new BufferedInputStream(new FileInputStream(file), 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);
        }
    }

    // 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();
            }
        }
    }

web.xml

  <servlet>
    <display-name>FileServlet</display-name>
    <servlet-name>FileServlet</servlet-name>
    <servlet-class>servlet.FileServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FileServlet</servlet-name>
    <url-pattern>/FileServlet</url-pattern>
  </servlet-mapping>

推荐答案

正确的URL是:

<img src="FileServlet?path=C:\Users\MyComputerName\Desktop\MyWorkspace\.metadata\.plug‌​ins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyWebApp\data\\<c:out value="${staff.staffImage}"/>">

这篇关于显示图像,其中“图像"标签的SRC属性为“相对路径"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆