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">
来直接显示图片
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。