ImageTag小案例
其实不使用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小案例的更多相关文章
- 机械表小案例之transform的应用
这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...
- shell讲解-小案例
shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...
- [jQuery学习系列六]6-jQuery实际操作小案例
前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...
- 02SpringMvc_springmvc快速入门小案例(XML版本)
这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:
- React.js入门小案例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- SqlDependency缓存数据库表小案例
SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...
- JavaScript apply函数小案例
//回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...
- Session小案例------完成用户登录
Session小案例------完成用户登录 在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...
- 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 ...
随机推荐
- redigo简单理解
package main import ( "fmt" "github.com/gomodule/redigo/redis") func main() { // ...
- pgfplots画二维图真的很方便,多例比较
%直接PDFLATEX编译即可\documentclass[border=1mm]{standalone}\usepackage{tkz-euclide,pgfplots}\begin{documen ...
- 对Storm ETL的初步思考
ETL简介 ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过萃取(extract).转置(transform).加载(load)至目的端的过程. ETL ...
- Shell学习:read的选项及用法
转摘: http://kb.cnblogs.com/a/2255702/ 1. Read的一些选项 Read可以带有-a, -d, -e, -n, -p, -r, -t, 和 -s八个选项. -a : ...
- java缓存适合使用的情况
并非所有的情况都适合于使用二级缓存,需要根据具体情况来决定.同时可以针对某一个持久化对象配置其具体的缓存策略. 适合于使用二级缓存的情况: 1.数据不会被第三方修改 一般情况下,会被hibernate ...
- http://m.blog.csdn.net/article/details?id=2630620
http://m.blog.csdn.net/article/details?id=2630620
- Java笔记14:泛型初探
一.泛型简介 泛型是从Java SE 1.5开始出现的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛 ...
- (转)一个基于vue2的天气js应用
基于vue.js 2.0的百度天气应用 vue-weather 基于vue.js 2.0的百度天气应用. 说明 初学vue,在看完一个简单的视频教程和走两遍完官方文档之后仍然感觉云里雾里,知其然不知其 ...
- Google Maps API v2密钥申请
1. 进入到Google APIs Console页面 https://code.google.com/apis/console/ 点击左边导航栏的Seivices进入 在All services 的 ...
- ZK框架笔记3、窗体组件
<window title="My First window" border="normal" width="200px" closa ...