Servlet從本地文件中讀取圖片,並顯示在頁面中

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;//sun公司僅提供了jpg圖片文件的編碼api
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 
 * 顯示文件的Servlet
 * 負責從本地文件中讀取圖片,
 * 
 */
public class ShowPic extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {

    private static final String GIF = "image/gif;charset=GB2312";// 設定輸出的類型

    private static final String JPG = "image/jpeg;charset=GB2312";

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public ShowPic() {
        super();
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String spec = request.getParameter("spec");// 輸出圖片的類型的標誌

        String imagePath = "D:\\temp\\1.jpg";

        response.reset();

        OutputStream output = response.getOutputStream();// 獲得輸出流
        if (imagePath.toLowerCase().endsWith(".jpg"))// 使用編碼處理文件流的狀況:
        {
            response.setContentType(JPG);// 設定輸出的類型
            // 獲得圖片的真實路徑

            // 獲得圖片的文件流
            InputStream imageIn = new FileInputStream(new File(imagePath));
            // 獲得輸入的編碼器,將文件流進行jpg格式編碼
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
            // 獲得編碼後的圖片對象
            BufferedImage image = decoder.decodeAsBufferedImage();
            // 獲得輸出的編碼器
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
            encoder.encode(image);// 對圖片進行輸出編碼
            imageIn.close();// 關閉文件流
        }
        if (imagePath.toLowerCase().endsWith(".gif"))// 不使用編碼處理文件流的狀況:
        {
            response.setContentType(GIF);
            ServletContext context = getServletContext();// 獲得背景對象
            InputStream imageIn = context.getResourceAsStream(imagePath);// 文件流
            BufferedInputStream bis = new BufferedInputStream(imageIn);// 輸入緩衝流
            BufferedOutputStream bos = new BufferedOutputStream(output);// 輸出緩衝流
            byte data[] = new byte[4096];// 緩衝字節數
            int size = 0;
            size = bis.read(data);
            while (size != -1) {
                bos.write(data, 0, size);
                size = bis.read(data);
            }
            bis.close();
            bos.flush();// 清空輸出緩衝流
            bos.close();
        }
        output.close();
    }
}

   想要調用的話,你能夠簡單的映射一下servelt,我就將servet的名映射爲html

<servlet>
      <servlet-name>ShowPic</servlet-name>
      <display-name>ShowPic</display-name>
      <description></description>
      <servlet-class>
      cn.ccb.zlgz.web.servlet.ShowPic</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>ShowPic</servlet-name>
      <url-pattern>/ShowPic</url-pattern>
  </servlet-mapping>

      使用HTML調用:java

<html>
<body>
<img src="ShowPic?spec=2"></a>
</body>
</html>

      這樣圖片便顯示在htm上了web

 

 轉載於:http://tntxia.iteye.com/blog/826193
 

JSP中顯示圖片的主要代碼就是:api

  <img src="http://localhost:8080/imgShow/ImageShowServlet" />app

  其中"imgShow"是項目名稱, "ImageShowServlet"是url-pattern.編碼

相關文章
相關標籤/搜索