博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ImageTag小案例
阅读量:7219 次
发布时间:2019-06-29

本文共 3188 字,大约阅读时间需要 10 分钟。

其实不使用ImageIO,就是用一般的BufferedOutputStream+byte[] buffer也可以

关键在于通过response设置页面的MIME Type,自行Google~~~

源代码直接帖了。。。

ImageTag.java

public class ImageTag extends SimpleTagSupport {    private String fileName;    private HttpServletResponse response;    private String imageType;    public void setImageType(String imageType) {        this.imageType = imageType;    }    public void setFileName(String fileName) {        this.fileName = fileName;    }    public void setResponse(HttpServletResponse response) {        this.response = response;    }    @Override    public void doTag() throws JspException, IOException {        response.setContentType("image/" + imageType); // This is necessary!!!        BufferedImage image = null;        BufferedOutputStream outputStream = null;        try {            File imageFile = new File(fileName);            Image src = ImageIO.read(imageFile);            int width = src.getWidth(null);            int height = src.getHeight(null);            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);            image.getGraphics().drawImage(src, 0, 0, width, height, null);            outputStream = new BufferedOutputStream(response.getOutputStream());            ImageIO.write(image, imageType, outputStream);            outputStream.flush(); // write the content from the buffer to the page        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (outputStream != null)                outputStream.close();        }    }}

tagext.tld

Tag extensions, my customized tag library.
xxx ext tags
1.0
ext
http://tags.xxx.com/ext
Load the specified image file and display it on the page.
image
com.v1.ex120.ImageTag
empty
The file extension of the image file.
imageType
true
true
The URI of the image file on the disk.
fileName
true
true
The HttpServletResponse of the display page. Normally you'd use the response object of the page in which this tag is used.
response
true
true

imagetag.jsp

<%@ page language="java" contentType="text/html; charset=GBK"    pageEncoding="GBK"%><%@ taglib uri="http://tags.xxx.com/ext" prefix="ext" %>
Insert title here

 

转载地址:http://dvtym.baihongyu.com/

你可能感兴趣的文章
[转]NHibernate之旅(6):探索NHibernate中的事务
查看>>
Kafka实现细节(三)
查看>>
屏幕分辨率(QQVGA、QVGA、VGA、XGA、WXGA、WUXGA和WSXGA+)
查看>>
IOS 音频播放
查看>>
观察者模式
查看>>
java entry
查看>>
Sublime Text
查看>>
html的下拉框的几个基本使用方法
查看>>
WinForm动态添加控件及其事件(转)
查看>>
Oracle数据库入门——目录结构
查看>>
makeBackronym
查看>>
应付发票审批 Hold and Release Names
查看>>
基于struct2完整的用户登录
查看>>
Socket的连接问题
查看>>
Codeforces 429 A. Xor-tree
查看>>
Spring+iBatis+Atomikos实现JTA事务
查看>>
lufylegend库 鼠标事件 循环事件 键盘事件
查看>>
通过浏览器调用Android要么iOS应用
查看>>
在线支付接口开发总结
查看>>
mysqldump导出部分数据的方法: 加入--where参数
查看>>