其实不使用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. 剖析ASP.NET Core MVC(Part 1)- AddMvcCore(译)

    原文:https://www.stevejgordon.co.uk/asp-net-core-mvc-anatomy-addmvccore发布于:2017年3月环境:ASP.NET Core 1.1 ...

  2. [转]SSIS中的脚本—脚本任务

    本文转自:http://www.cnblogs.com/tylerdonet/archive/2011/09/16/2179123.html 脚本任务主要用来控制数据流,当现有的控制流 任务不能满足复 ...

  3. OTL调用存储过程/函数及注意事项

    OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个 C++ 编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库. OTL  ...

  4. RMAN 还原与恢复

    一. RMAN 还原与恢复基础 在RMAN 用于中,还原与恢复是两个不同的概念.还原(restore):指访问先前生成的备份,从中得到一个或多个对象,然后在磁盘上的某个位置还原这些对象.恢复(reco ...

  5. SQL注入深入剖析

    SQL注入是一门很深的学问,也是一门很有技巧性的学问 1.  运算符的优先级介绍 2.  SQL语句执行函数介绍 mysql_query() 仅对 SELECT,SHOW,EXPLAIN 或 DESC ...

  6. 动态加入改动删除html表格内容

    1.需求 须要实现收银台上加入改动删除商品信息时顾显能够实时看到其变化 2.解决 收银台和顾显通过tcp传输进行数据通信,顾显通过操作html中的表格进行数据加入改动和删除操作 3.代码 mytest ...

  7. (转)PV、UV、IP之间的区别与联系

    PV是网站分析的一个术语,用以衡量网站用户访问的网页的数量.对于广告主,PV值可预期它可以带来多少广告收入.一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的真实来访者数量,如同一个来访者 ...

  8. 算法笔记_158:算法提高 逆序排列(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 问题描述 编写一个程序,读入一组整数(不超过20个),并把它们保存在一个整型数组中.当用户输入0时,表示输入结束.然后程序将把这个数组中的值按逆序重新存 ...

  9. 算法笔记_103:蓝桥杯练习 算法提高 金明的预算方案(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些 ...

  10. 小程序 的 textarea 组件 层级问题如何解决

        像这样的   既然是定位(脱离文档流),为啥不加层级关系,层级关系不明确,很容易出现显示异常,给遮罩加z-index:10,弹出框加z-index:11 即可 解决方案参考: https:// ...