Jsp 根据绝对路径加载本地图片

版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/w18756901575 https://blog.csdn.net/w18756901575/article/details/72818079

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: wkk
  Date: 2017/5/24/024
  Time: 16:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<img src="showImg.jsp?path=D:/img/test.jpg"/>

</body>
</html>

showImg.jsp

<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %><%--
  Created by IntelliJ IDEA.
  User: wkk
  Date: 2017/5/31/031
  Time: 9:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String path = request.getParameter("path");
    if (path == null || path.isEmpty()) {
        return;
    }
    File file = new File(path);
    if (!file.exists() || file.isDirectory()) {
        return;
    }
    FileInputStream fileInputStream = new FileInputStream(file);
    ServletOutputStream outputStream = response.getOutputStream();
    byte bs[] = new byte[1024];
    int l;
    while ((l = fileInputStream.read(bs)) != -1) {
        outputStream.write(bs, 0, l);
    }
    outputStream.flush();
    outputStream.close();
    fileInputStream.close();
%>
</body>
</html>

效果图:

猜你喜欢

转载自blog.csdn.net/w18756901575/article/details/72818079