Jsp 根據絕對路徑載入本地圖片
阿新 • • 發佈:2019-02-16
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>
效果圖: