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>

 

你可能感兴趣的:(image)