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

来源:互联网 发布:俄军战斗力 知乎 编辑:程序博客网 时间:2024/06/18 12:48

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>

效果图:

原创粉丝点击