小菜毛毛技術(shù)分享

          與大家共同成長(zhǎng)

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(15)

          我參與的團(tuán)隊(duì)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          最近,公司用servlet做一個(gè)跟蹤圖片點(diǎn)擊技術(shù)的模塊,我個(gè)人略有心得想和大家分享,
           不知大家愿意審視。
           這個(gè)模塊挺大,我僅說說用servlet顯示圖片部分。我先說說用servlet顯示圖片的一個(gè)流程:
           1. 設(shè)置response的輸出類型:
              對(duì)應(yīng)的語句--response.setContentType("image/gif;charset=GB2312") ,response  
              便能輸出gif圖片,"image/gif;charset=GB2312"便是輸出類型,當(dāng)然你可以輸出
              "image/jpg;charset=GB2312"類型文件。
           2. 得到文件流:
              servlet是以流的形式件圖片文件從服務(wù)器讀出,通過response將流發(fā)到瀏覽器的。
           3. 得到輸出流:
              對(duì)應(yīng)的語句--OutputStream output = response.getOutputStream();
              當(dāng)然,處理圖片文件需要以二進(jìn)制形式的流。
           4. 文件流的編碼(但也不一定必須編碼的,如果不是文件流,則必須編碼)
              所以我給大家一個(gè)用編碼的代碼和不用編碼的代碼.
          順便說一句,sun公司僅提供了jpg圖片文件的編碼api。
              
              我想基本流程都講完了,下面我把代碼拿給大家看一下,大家自然一目了然了:
          package xjw.personal.servet;
          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;
          public class ShowPicture  extends HttpServlet
          {
               private static final String GIF="image/gif;charset=GB2312";//設(shè)定輸出的類型
               private static final String JPG="image/jpeg;charset=GB2312";
               public void init()  throws ServletException
               {
               }
               public void doGet(HttpServletRequest request, HttpServletResponse response)
                            throws IOException, ServletException
               {
                  doPost(request, response);
               }

              public void doPost(HttpServletRequest request, HttpServletResponse response)
                           throws IOException, ServletException
             {
                  
                  String spec=request.getParameter("spec");//輸出圖片的類型的標(biāo)志
           int int_spec=Integer.parseInt(spec);
                  if(spec==1)    
                  {
                      String imagePath="/jfgg/b1.jpg";//圖片相對(duì)web應(yīng)用的位置
                  }
                  else
                  {
                        String imagePath="/jfgg/b2.gif";//圖片相對(duì)web應(yīng)用的位置
                  }    
                  
                 OutputStream output = response.getOutputStream();//得到輸出流
                 if(imagePath.toLowerCase().endsWith(".jpg"))//使用編碼處理文件流的情況:
                {
               response.setContentType(JPG);//設(shè)定輸出的類型
                    //得到圖片的真實(shí)路徑      
                    imagePath = getServletContext().getRealPath(imagePath);
                   //得到圖片的文件流
                    InputStream imageIn = new FileInputStream(new File(imagePath));
                   //得到輸入的編碼器,將文件流進(jìn)行jpg格式編碼
               JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
                   //得到編碼后的圖片對(duì)象
                   BufferedImage image = decoder.decodeAsBufferedImage();
                   //得到輸出的編碼器
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
                    encoder.encode(image);//對(duì)圖片進(jìn)行輸出編碼
                    imageIn.close();//關(guān)閉文件流
                 }
                 if(imagePath.toLowerCase().endsWith(".gif"))//不使用編碼處理文件流的情況:
                {
              response.setContentType(GIF);
              ServletContext context = getServletContext();//得到背景對(duì)象
              InputStream imageIn=context.getResourceAsStream(imagePath);//文件流
                   BufferedInputStream bis=new BufferedInputStream(imageIn);//輸入緩沖流
              BufferedOutputStream bos=new BufferedOutputStream(output);//輸出緩沖流
              byte data[]=new byte[4096];//緩沖字節(jié)數(shù)
              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();
                      
               }
          }
          最后是如何調(diào)用,你可以簡(jiǎn)單的映射一下servelt,我就將servet的名映射為
                 ShowPic,  于是下代碼調(diào)用
          <html>
          <body>
          <img src="ShowPic?spec=2"></a>
          </body>
          </html>
                  這樣圖片便顯示在htm上了,本人研究java的時(shí)間不長(zhǎng),如有問題,盡情指正。
          posted on 2010-09-25 13:43 小菜毛毛 閱讀(4300) 評(píng)論(0)  編輯  收藏 所屬分類: 面試
          主站蜘蛛池模板: 东乌珠穆沁旗| 灵川县| 南京市| 普陀区| 涿州市| 隆安县| 武城县| 宜丰县| 扶沟县| 凉城县| 台东市| 图片| 巴彦淖尔市| 博野县| 荔波县| 吉安县| 恩平市| 湖北省| 大同县| 米林县| 晋中市| 定远县| 淮阳县| 江口县| 宁城县| 穆棱市| 屏边| 奈曼旗| 平遥县| 隆化县| 武安市| 通道| 广宁县| 日照市| 漾濞| 大连市| 北碚区| 嘉义市| 黄山市| 泾阳县| 泰州市|