Spring MVC可使用的AbstractJpegView
2007-06-27 16:13
225 查看
这和AbstractExcelView,AbstractPdfView不同,此类是实现View接口,实现自定义的视图解析
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.View;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import sun.awt.image.codec.JPEGImageEncoderImpl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.sun.image.codec.jpeg.JPEGImageEncoder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public abstract class AbstractJpegView implements View ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getContentType() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO 自动生成方法存根
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int getImageWidth()...{return 100;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int getImageHeght()...{return 100;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
protected int getImageType()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return BufferedImage.TYPE_INT_RGB;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void render(Map map, HttpServletRequest request,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
HttpServletResponse response) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
response.setContentType("image/jpeg");//设置内容类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedImage image=new BufferedImage(this.getImageWidth(),this.getImageHeght(),this.getImageType());//缓存图像
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
buildImage(map,image,request,response);//绘制图像
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//编码图像
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletOutputStream out=response.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPEGImageEncoder encoder=new JPEGImageEncoderImpl(out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
encoder.encode(image);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected abstract void buildImage(Map model,BufferedImage image,HttpServletRequest request,HttpServletResponse response) throws Exception;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我们只要继承这个类,实现buildImage方法,就可以输出人以jpeg图像了,下面是一个画圆的简单实现
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Graphics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class CircleJpegView extends AbstractJpegView ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void buildImage(Map model, BufferedImage image,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpServletRequest request, HttpServletResponse response)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//画圆
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Graphics g=image.getGraphics();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawOval(0,0, this.getImageWidth(), this.getImageHeght());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.View;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import sun.awt.image.codec.JPEGImageEncoderImpl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.sun.image.codec.jpeg.JPEGImageEncoder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public abstract class AbstractJpegView implements View ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getContentType() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO 自动生成方法存根
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int getImageWidth()...{return 100;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int getImageHeght()...{return 100;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
protected int getImageType()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return BufferedImage.TYPE_INT_RGB;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void render(Map map, HttpServletRequest request,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
HttpServletResponse response) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
response.setContentType("image/jpeg");//设置内容类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedImage image=new BufferedImage(this.getImageWidth(),this.getImageHeght(),this.getImageType());//缓存图像
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
buildImage(map,image,request,response);//绘制图像
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//编码图像
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletOutputStream out=response.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPEGImageEncoder encoder=new JPEGImageEncoderImpl(out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
encoder.encode(image);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected abstract void buildImage(Map model,BufferedImage image,HttpServletRequest request,HttpServletResponse response) throws Exception;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我们只要继承这个类,实现buildImage方法,就可以输出人以jpeg图像了,下面是一个画圆的简单实现
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Graphics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class CircleJpegView extends AbstractJpegView ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void buildImage(Map model, BufferedImage image,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpServletRequest request, HttpServletResponse response)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//画圆
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Graphics g=image.getGraphics();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawOval(0,0, this.getImageWidth(), this.getImageHeght());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- Spring MVC使用ModelAndView进行重定向
- Springmvc 使用 AbstractExcelView 导出excel
- Spring MVC @JsonView使用详解
- Spring MVC使用ModelAndView进行重定向
- Spring MVC 4.1 新增 @JsonView注解使用
- Spring MVC使用ModelAndView进行重定向(转)
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- Springmvc 使用 AbstractExcelView 导出excel
- Spring MVC使用ModelAndView进行重定向
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- 使用Spring MVC不通过View直接生成一个页面
- Spring MVC @JsonView注解使用
- Spring MVC如何配置OpenSessionInViewInterceptor并结合Hibernate使用
- spring mvc view使用方法示例
- Springmvc 使用 AbstractExcelView 导出excel
- Spring MVC使用ModelAndView进行重定向
- spring mvc中mvc:view-controller的使用
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- Spring MVC使用ModelAndView进行重定向
- Spring MVC 使用ModelAndView的相对路径与绝对路径跳转的问题