其实不使用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. SQLAlchemy会话与事务控制:互斥锁和共享锁

    关于sqlalchemy,可以细度这个网址:http://www.codexiu.cn/python/SQLAlchemy%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/73 ...

  2. 阿里云NAS使用方法

    1.创建文件系统 #在创建文件系统页面,填写各项参数.根据项目需求选择存储类型 2.添加挂载点 文件系统实例创建完成后,您需要为文件系统添加挂载点,用于计算节点(ECS 实例.E-HPC 或容器服务) ...

  3. Android APP内存优化之图片优化

    网上有很多大拿分享的关于Android性能优化的文章,主要是通过各种工具分析,使用合理的技巧优化APP的体验,提升APP的流畅度,但关于内存优化的文章很少有看到.在Android设备内存动不动就上G的 ...

  4. 让网页在ie浏览器下以最高版本解析网页

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta ht ...

  5. Maven Web项目配置Mybatis出现SqlSessionFactory错误的解决方案

    一.错误现象 严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Er ...

  6. HDUOJ A Mathematical Curiosity 1017

     此题不难就是输出格式麻烦 #include<stdio.h>  int main(){        int T;   scanf("%d",&T);   ...

  7. C# SQLite 创建数据库的方法增删查改语法和命令

    SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...

  8. Kolla 4.0.0环境下VIP无法迁移问题排查

    VRRP无法切换VIP的问题分析: Keepalived的配置文件:/etc/kolla/keepalived/keepalived.conf当中,nopreempt选项是影响切换的因素之一,另一个因 ...

  9. python学习准备阶段(环境配置)python解释器的选择

    pycharm提示No Python interpreter configgured for the project ####### 1 点击configure python interpreter ...

  10. PyPy与VirtualEnv的安装问题

    PyPy与VirtualEnv的安装问题 说明:本博客由bitpeach原创撰写,请勿商用.转载免费,请注明出处,谢谢. (零)背景 VirtualEnv工具的详细内容是什么,请自行百度.这里大概简介 ...