您的位置:首页 > 其它

使用 img标签引用项目外的 绝对路径的 图片

2017-08-31 10:11 429 查看
这里有2中方式

1.使用tomcat文件夹下的conf里面的server.xml里面的host标签下 添加一个

<Context path="/file" docBase="E:\test" debug="0" reloadable="true"/>


然后在 img 的src= “ /file/red.png” 就是引用了 E:\test\red.png图片

2.使用io流的方式 显示图片

先使用action写一个输出文件流的action

例如

public void show(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();

String url = request.getParameter("url");
String path = url.substring(0,url.lastIndexOf("/"));
String realFileName = url.substring(url.lastIndexOf("/")+1,url.length());
String fileName = request.getParameter("fileName");
String suffix = request.getParameter("suffix");
//       path = ServletActionContext.getServletContext().getRealPath(path);

try {
fileName = java.net.URLEncoder.encode(fileName+"."+suffix, "utf-8"); //ie 中文不兼容问题
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);

OutputStream os = null;
FileInputStream fis = null;
byte[] buffer = new byte[1024];
path = path+"\\"+realFileName+"."+suffix;
int len = 0;
try {

fis = new FileInputStream(new File(path));
os = response.getOutputStream();
while ((len = fis.read(buffer)) > 0) {
os.write(buffer, 0, len);
}

} catch (Exception e) {
e.printStackTrace();

} finally {
try {
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}

}

return ;
}


然后在然后在 img 的src= “ ImgPathActionDownLoad.action?url=E:\test\red.png &fileName=red&suffix=png” 就是引用了 E:\test\red.png图片 这里面的除了参数 url其他你自己可以在action里面改
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片