其实不使用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. 用phonegap和jquery-mobile写android应用

    今天纪录的是学习用phonegap和jquery-moblie来写android的过程.环境搭建. 我的习惯是直接上官网看文档,看get-started guide之类的文档.然后在看参考手册.然后就 ...

  2. [译]ES读写文档时shard-replication模型

    官网页面:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-replication.html 本文是对官网页面的 ...

  3. [React] Detect user activity with a custom useIdle React Hook

    If the user hasn't used your application for a few minutes, you may want to log them out of the appl ...

  4. 今天科普一下 iOS马甲包审核以及常见审核问题

    一.什么是马甲包 马甲包是利用App store 规则漏洞,通过技术手段,多次上架同一款产品的方法.马甲包和主产品包拥有同样的内容和功能,除了icon和应用名称不能完全一致,其他基本一致.    二. ...

  5. VS2013编译boost1.55库

    1. 官网下载最新的Boost库,我的是1.55 2. 在使用vs2013编译boost-1.55.0之前,先要给boost做下修改: boost_1_55_0\boost\intrusive\det ...

  6. C/C++内存管理

    1. 静态内存 静态内存是指在程序开始运行时由编译器分配的内存,它的分配是在程序开始编译时完成的,不占用CPU资源.程序中的各种变量,在编译时系统已经为其分配了所需的内存空间,当该变量在作用域内使用完 ...

  7. vue - config(index.js)

    描述:我想,这是调用最多的一个文件了吧(无论是dev,还是prod) 'use strict' // Template version: 1.3.1 // see http://vuejs-templ ...

  8. Chrome扩展之css used 获取网页样式

    地址栏输入: chrome://extensions/ 然后获取更多扩展程序,得到css used 复制html节点 最后点击 "css used" 把样式全部复制下来即可 (记住 ...

  9. IO/序列化/JSON

    一.读写文件 1.open:打开文件 open(path, mode, encoding='xxx', errors='ignore') mode取值:rU 或 Ua 以读方式打开, 同时提供通用换行 ...

  10. 如何让你的webapp也能跳窗口搜索

    目前很多手机app或者一些webapp,搜索栏基本采用跳窗口的搜索方式 怎么做 实现方式: 1.在触发外层的input的时候打开个modal层,默认打开该modal层的时候就触发了moda里面的inp ...