【问题标题】:How to display image using <img src=" "> from system directory in JSP如何使用 JSP 系统目录中的 <img src=" "> 显示图像
【发布时间】:2016-06-06 11:07:25
【问题描述】:

我正在尝试获取存储在我的本地系统目录中的图像,我将图像路径存储在 MySQl 数据库中,图像的路径是

 H:\IVS-FEB 2016\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\IVS\uploads\share1.png

我正在尝试使用

获取此路径
<img src="<%String pathup =rs2.getString("pathup");out.print(pathup);%>" width="200" height="200" alt="Uploaded by user">

但这不会在我的网页上显示图像? :( 在浏览器中点击检查元素选项时出现以下错误

不允许加载本地资源:file:///H:/IVS-FEB%202016/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/IVS/uploads/share1。 png

【问题讨论】:

  • 不,它不工作,它仍然不显示图像

标签: html mysql image jsp


【解决方案1】:

编写一个 Java Servlet。见example tutorial

例如:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    ServletOutputStream oStream;
    String fileName = "your file";
    try (FileInputStream iStream = new FileInputStream(new File(fileName))) 
    {
        response.setContentType("image/png");
        oStream = response.getOutputStream();

        byte[] buffer = new byte[1024];
        int len;
        while ((len = iStream.read(buffer)) != -1) 
        { oStream.write(buffer, 0, len); }

    }

    oStream.flush();
    oStream.close();
}

然后在您的 HTML/JSP 页面中使用:

<img src="ImageServlet"/>

如果您有多个基于任何条件的图像,您可以传递参数,并在 Servlet 类中执行您的逻辑选择。

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多