Java Web 读取本地图片,以流的方式返回在页面

WEB项目读取不在项目路径中放的文件,类似于E:/的某个文件,是访问不到的,除了在Tomcat下配置虚拟路径,下面就是这种方法 
@RequestMapping(value = "/pic/mid-{mediaid:.*}", method = RequestMethod.GET) 
public void getpicByMediaid(HttpServletRequest request, HttpServletResponse response, 
@PathVariable("mediaid") String mediaid, 
@RequestParam(value = "w", defaultValue = "0") int w, 
@RequestParam(value = "h", defaultValue = "0") int h) throws IOException { 
response.setHeader("Cache-Control", "max-age=" + (7 * 24 * 60 * 60)); 
response.setDateHeader("Last-Modified", new Date().getTime()); 
try { 
if (mediaid.length() < 2) { 
response.sendError(404, "exp"); 
return; 

String imgurl = rootpath + mediaid.substring(0, 2) + "/" + mediaid.substring(2); 

if (w != 0 || h != 0) { 
this.diplayImageBySize(request, response, imgurl, mediaid, w, h); 
} else { 
this.diplayImage(request, response, imgurl, mediaid); 

} catch (Exception ex) { 
response.sendError(500, "exp"); 
return; 


这个是Controller中的一个方法,是一个获取图片的 
this.diplayImageBySize(request, response, imgurl, mediaid, w, h); 
这个方法可以设置图片的宽和高 
this.diplayImage(request, response, imgurl, mediaid); 
这个方法直接获得图片 
public void diplayImageBySize(HttpServletRequest request, HttpServletResponse response, String imgurl, String id, int w, int h) throws IOException { 
response.setHeader("Cache-Control", "max-age=" + (7 * 24 * 60 * 60)); 
response.setDateHeader("Last-Modified", new Date().getTime()); 
if (request.getHeader("If-Modified-Since") != null) { 
response.setStatus(304); 
return; 


BufferedImage buffImg = dpservice.diplayImg(w + "," + h, imgurl); 
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); 
ImageWriter writer = (ImageWriter) iter.next(); 
ImageWriteParam iwp = writer.getDefaultWriteParam(); 
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
iwp.setCompressionQuality(0.9f); 
ColorModel colorModel = ColorModel.getRGBdefault(); 
iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, 
colorModel.createCompatibleSampleModel(8, 8))); 
response.setContentType(request.getSession().getServletContext().getMimeType(id)); 
OutputStream os = response.getOutputStream(); 
MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(os); 
writer.setOutput(output); 
IIOImage image = new IIOImage(buffImg, null, null); 
writer.write(null, image, iwp); 
writer.dispose(); 
output.flush(); 
output.close(); 
os.close(); 
buffImg = null; 


public void diplayImage(HttpServletRequest request, HttpServletResponse response, String imgurl, String id) throws IOException { 
response.setHeader("Cache-Control", "max-age=" + (7 * 24 * 60 * 60)); 
response.setDateHeader("Last-Modified", new Date().getTime()); 
if (request.getHeader("If-Modified-Since") != null) { 
response.setStatus(304); 
return; 

//String imgurl = String.format(this.imgpath, id, id); 
BufferedImage buffImg = ImageIO.read(new File(imgurl)); 
response.setContentType(request.getSession().getServletContext().getMimeType(id)); 
OutputStream os = response.getOutputStream(); 
ImageIO.write(buffImg, "JPEG", os); 
os.flush(); 
os.close(); 
buffImg = null; 



而在前台页面,HTML中可以直接以 
<img src="http://项目/pic/mid-1"> 
来直接显示图片

转载于:https://www.cnblogs.com/Secondnn/p/3683314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值