这和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;
}
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());
}
}
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());
}
}