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"> 
來直接顯示圖片


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com