⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pictureservlet.java

📁 此系统为我大家完全开发一个相册管理系统
💻 JAVA
字号:
/* ====================================================================
 * $Id$
 * ====================================================================
 *  文件名    PictureServlet.java
 *  机能名    从WEB-INF目录内读取图片并显示。
 *  履历      2005-1-26 dlxu 创建新文件  
 *           Copyright 2004 东南大学 All Rights Reserved
 * ====================================================================
 */
package cn.edu.seu.album.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Category;
import org.apache.log4j.Logger;

/**
 * <p> [概 要] 从WEB-INF目录内读取图片并显示。</p>
 * <p> [详 细] 从WEB-INF目录内读取图片并显示。</p>
 * <p> [备 考] 无。</p>
 *
 * @author dlxu
 * @version 1.0 2005-1-26
 * @since 1.0
 */
public class PictureServlet extends HttpServlet {

    // Get the logger.
    private static Category log = Logger.getInstance(PictureServlet.class);

    /**
     * <p> [概 要] 从WEB-INF目录内读取图片并显示。</p>
     * <p> [详 细] 从WEB-INF目录内读取图片并显示。</p>
     * <p> [备 考] 无。</p>
     *
     * @param req HttpServletRequest类
     * @param resp HttpServletResponse类
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {

        log.debug("enter the picture loading servlet");

        // The input stream read the picture.
        InputStream in = null;

        // The output stream display the picture.
        OutputStream out = null;

        // Set the content type.
        resp.setContentType("image/gif");
        try {

            // Get the picture name from the parameter.
            String fileName = (String) req.getParameter("id");

            // Get the picture path.
            String path = this.getServletContext().getRealPath("/WEB-INF/");
            String realFileName = path + "\\picture\\" + fileName;
            // log.debug("图片地址:" + realFileName);
            try {
                // Get the inputStream from the file.
                in = new FileInputStream(realFileName);
            } catch (FileNotFoundException e) {
                String errorInfo = "The Image file " + realFileName
                        + " can not be found!";
                log.error(errorInfo);
                return;

            }
            try {
                // Get the outputStream.
                out = resp.getOutputStream();
            } catch (IOException e1) {
                String errorInfo = "There are some errors while reading "
                        + fileName;
                log.error(errorInfo);
                return;
            }
            try {
                // Read the image from the inputStream and then output it.
                byte[] b = new byte[4096];
                int bt = in.read(b);
                while (bt != -1) {
                    out.write(b, 0, bt);
                    bt = in.read(b);
                }
            } catch (IOException e) {
                String errorInfo = "There are some errors while reading "
                        + fileName;
                log.error(errorInfo);
                return;
            }
        } finally {
            // Close the inputStream and the outputStream.
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.warn("can not close the inputStream");
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.warn("Can not close the outputStream");
                }
            }
        }

    }

    /**
     * <p> [概 要] </p>
     * <p> [详 细] </p>
     * <p> [备 考] 无。</p>
     * @param req
     * @param res
     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res) {
        return;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -