Spring MVC可使用的AbstractJpegView
系统
1611 0
这和AbstractExcelView,AbstractPdfView不同,此类是实现View接口,实现自定义的视图解析
package
Action;
import
java.awt.image.BufferedImage;
import
java.util.Map;
import
javax.servlet.ServletOutputStream;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.web.servlet.View;
import
sun.awt.image.codec.JPEGImageEncoderImpl;
import
com.sun.image.codec.jpeg.JPEGImageEncoder;
public
abstract
class
AbstractJpegView
implements
View
...
{
public
String getContentType()
...
{
//
TODO 自动生成方法存根
return
null
;
}
public
int
getImageWidth()
...
{
return
100
;}
public
int
getImageHeght()
...
{
return
100
;}
protected
int
getImageType()
...
{
return
BufferedImage.TYPE_INT_RGB;
}
public
void
render(Map map, HttpServletRequest request,
HttpServletResponse response)
throws
Exception
...
{
response.setContentType(
"
image/jpeg
"
);
//
设置内容类型
BufferedImage image
=
new
BufferedImage(
this
.getImageWidth(),
this
.getImageHeght(),
this
.getImageType());
//
缓存图像
buildImage(map,image,request,response);
//
绘制图像
//
编码图像
ServletOutputStream out
=
response.getOutputStream();
JPEGImageEncoder encoder
=
new
JPEGImageEncoderImpl(out);
encoder.encode(image);
out.flush();
}
protected
abstract
void
buildImage(Map model,BufferedImage image,HttpServletRequest request,HttpServletResponse response)
throws
Exception;
}
我们只要继承这个类,实现buildImage方法,就可以输出人以jpeg图像了,下面是一个画圆的简单实现
package
Action;
import
java.awt.Graphics;
import
java.awt.image.BufferedImage;
import
java.util.Map;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
CircleJpegView
extends
AbstractJpegView
...
{
protected
void
buildImage(Map model, BufferedImage image,
HttpServletRequest request, HttpServletResponse response)
throws
Exception
...
{
//
画圆
Graphics g
=
image.getGraphics();
g.drawOval(
0
,
0
,
this
.getImageWidth(),
this
.getImageHeght());
}
}
Spring MVC可使用的AbstractJpegView
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元