其实不使用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

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0"> <description>Tag extensions, my customized tag library.</description>
<display-name>xxx ext tags</display-name>
<tlib-version>1.0</tlib-version>
<short-name>ext</short-name>
<uri>http://tags.xxx.com/ext</uri> <tag>
<description>
Load the specified image file and display it on the page.
</description>
<name>image</name>
<tag-class>com.v1.ex120.ImageTag</tag-class>
<body-content>empty</body-content> <attribute>
<description>
The file extension of the image file.
</description>
<name>imageType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute> <attribute>
<description>
The URI of the image file on the disk.
</description>
<name>fileName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute> <attribute>
<description>
The HttpServletResponse of the display page.
Normally you'd use the response object of the page
in which this tag is used.
</description>
<name>response</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag> </taglib>

imagetag.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib uri="http://tags.xxx.com/ext" prefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<ext:image response="${pageContext.response }" fileName="D:\qq.png" imageType="PNG"/>
</body>
</html>

ImageTag小案例的更多相关文章

  1. 机械表小案例之transform的应用

    这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...

  2. shell讲解-小案例

    shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...

  3. [jQuery学习系列六]6-jQuery实际操作小案例

    前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...

  4. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  5. React.js入门小案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  6. SqlDependency缓存数据库表小案例

    SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...

  7. JavaScript apply函数小案例

    //回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...

  8. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...

  9. ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)

    1 hello world 引入vue.min.js 代码: ----2.0+版本 <div id="test"> {{str}} </div> <s ...

随机推荐

  1. List of regional organizations by population

    https://baike.baidu.com/item/国际组织/261053?fr=aladdin 经济文化类组织(非政府组织) 创行 狮子会 全球青年领导力联盟 乐施会 政治类组织 欧洲联盟(欧 ...

  2. 在 Android Studio 2.2 中愉快地使用 C/C++

    转载请注明出处:http://blog.csdn.net/wl9739/article/details/52607010 注:官网上面的技术文章也在不断地汉化中,只是进度有点慢.在我翻译本篇文章的时候 ...

  3. H5+CSS3简单动画 知识点 汇总

    乱入几个:  1.h5的一个语义化标签   figure :用于规定独立的流内容(图像 图表 照片 代码等)   figcapition:与figure配套使用,用于标签定义figure元素标题 2. ...

  4. [Functional Programming ADT] Debug a Functional JavaScript composeK Flow

    When using ADTs in our code base, it can be difficult to use common debugging tools like watches and ...

  5. ffmpeg与TS

    http://blog.csdn.net/shuyong1999/article/details/7176329 一个不错的音视频博客 0. 简介 FFmpeg是一个集录制.转换.音/视频编码解码功能 ...

  6. Apple 远程推送APNS 服务

    APP注册APNS流程 http://mobiforge.com/design-development/programming-apple-push-notification-services 服务器 ...

  7. vue - node_modules

    详情见:node_modules导包机制 在打包或者结束项目时,这个文件夹(node_modules)不应该被打包. 你应该打包其它的文件,如果要运行(直接用以下命令安装即可,它会根据package. ...

  8. 在retrofit+Rxjava中如何取得状态码非200(出现错误)时的response里的body

    一个典型的retrofit+Rxjava的网络请求如下 Subscription subscription = videoChartService.login(newBody) .observeOn( ...

  9. html 里面的 role 属性是什么意思

    role="button" role是什么意思? html 里面的 role 属性是什么意义和用途 使用role属性告诉辅助设备(如屏幕阅读器)这个元素所扮演的角色,属于WAI-A ...

  10. CentOS 6.4 编译安装 gcc 4.8.1(转)

    今天在isocpp上看到“GCC 4.8.1 released, C++11 feature complete”这个消息,非常兴奋.终于有一个全面支持C++11语言特性的编译器了! 当然了,gcc仅仅 ...