代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
在jsp页面中如何访问绝对路径中的图片
作者:
404nofound405
/ 发布于
2014/12/30
/
542
/** * 加载图片 * @return * @throws Exception */ @RequestMapping("/getImage.html") public ModelAndView getImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="newFlag", required=false) String newFlag, @RequestParam(value="fileName", required=false) String fileName){ if(StringUtil.isNotEmpty(fileName)){ String basePath = null; if(StringUtil.isNotEmpty(newFlag)){ basePath = System.getProperty("java.io.tmpdir"); }else{ basePath = this.getFileUploadPath(); } String templatePath = basePath+"/"+fileName; File temp = new File(templatePath); FileInputStream steam = null; if(temp.exists()){ try { steam = new FileInputStream(templatePath); String fileNames = new String(fileName.getBytes("UTF-8"),"ISO_8859_1"); response.setCharacterEncoding("UTF-8"); String contentType = null; if(fileName.endsWith(".emf")){ contentType = "application/x-emf"; }else if(fileName.endsWith(".wmf")){ contentType = "application/x-wmf"; }else if(fileName.endsWith(".jpeg") || fileName.endsWith(".jpg")){ contentType = "image/jpeg"; }else if(fileName.endsWith(".png")){ contentType = "image/png"; }else if(fileName.endsWith(".dib")){ contentType = "application/x-dib"; }else if(fileName.endsWith(".gif")){ contentType = "image/gif"; }else if(fileName.endsWith(".tiff")){ contentType = "image/tiff"; }else if(fileName.endsWith(".eps")){ contentType = "application/x-ps"; }else if(fileName.endsWith(".bmp")){ contentType = "application/x-bmp"; }else if(fileName.endsWith(".wpg")){ contentType = "application/x-wpg"; } if(contentType != null){ response.setContentType(contentType); } response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.addHeader("content-disposition","attachment; filename=" +fileNames); OutputStream out = response.getOutputStream(); byte[] bt = new byte[1024]; while(steam.read(bt) > -1){ out.write(bt); } out.flush(); out.close(); }catch (Exception e) { logger.error(e); }finally{ try{ if(steam != null){ steam.close(); } }catch (IOException e) { } } }else{ logger.error("[" + templatePath + "]图片未找到。"); try { steam = new FileInputStream(request.getSession().getServletContext().getRealPath("/images/getPhotoFailed.jpg")); String fileNames = new String(fileName.getBytes("UTF-8"),"ISO_8859_1"); response.setCharacterEncoding("UTF-8"); response.setContentType("image/jpeg"); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.addHeader("content-disposition","attachment; filename=" +fileNames); OutputStream out = response.getOutputStream(); byte[] bt = new byte[1024]; while(steam.read(bt) > -1){ out.write(bt); } out.flush(); out.close(); }catch (Exception e) { logger.error(e); }finally{ try{ if(steam != null){ steam.close(); } }catch (IOException e) { } } } } else {//fileName为空时 FileInputStream steam = null; try { steam = new FileInputStream(request.getSession().getServletContext().getRealPath("/images/noPhoto.jpg")); String fileNames = new String(fileName.getBytes("UTF-8"),"ISO_8859_1"); response.setCharacterEncoding("UTF-8"); response.setContentType("image/jpeg"); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.addHeader("content-disposition","attachment; filename=" +fileNames); OutputStream out = response.getOutputStream(); byte[] bt = new byte[1024]; while(steam.read(bt) > -1){ out.write(bt); } out.flush(); out.close(); }catch (Exception e) { logger.error(e); }finally{ try{ if(steam != null){ steam.close(); } }catch (IOException e) { } } } return null; }
试试其它关键字
绝对路径
图片
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
404nofound405
贡献的其它代码
(
1
)
.
在jsp页面中如何访问绝对路径中的图片
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3