小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
          最近,公司用servlet做一個跟蹤圖片點擊技術的模塊,我個人略有心得想和大家分享,
           不知大家愿意審視。
           這個模塊挺大,我僅說說用servlet顯示圖片部分。我先說說用servlet顯示圖片的一個流程:
           1. 設置response的輸出類型:
              對應的語句--response.setContentType("image/gif;charset=GB2312") ,response  
              便能輸出gif圖片,"image/gif;charset=GB2312"便是輸出類型,當然你可以輸出
              "image/jpg;charset=GB2312"類型文件。
           2. 得到文件流:
              servlet是以流的形式件圖片文件從服務器讀出,通過response將流發到瀏覽器的。
           3. 得到輸出流:
              對應的語句--OutputStream output = response.getOutputStream();
              當然,處理圖片文件需要以二進制形式的流。
           4. 文件流的編碼(但也不一定必須編碼的,如果不是文件流,則必須編碼)
              所以我給大家一個用編碼的代碼和不用編碼的代碼.
          順便說一句,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";//設定輸出的類型
               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");//輸出圖片的類型的標志
           int int_spec=Integer.parseInt(spec);
                  if(spec==1)    
                  {
                      String imagePath="/jfgg/b1.jpg";//圖片相對web應用的位置
                  }
                  else
                  {
                        String imagePath="/jfgg/b2.gif";//圖片相對web應用的位置
                  }    
                  
                 OutputStream output = response.getOutputStream();//得到輸出流
                 if(imagePath.toLowerCase().endsWith(".jpg"))//使用編碼處理文件流的情況:
                {
               response.setContentType(JPG);//設定輸出的類型
                    //得到圖片的真實路徑      
                    imagePath = getServletContext().getRealPath(imagePath);
                   //得到圖片的文件流
                    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的名映射為
                 ShowPic,  于是下代碼調用
          <html>
          <body>
          <img src="ShowPic?spec=2"></a>
          </body>
          </html>
                  這樣圖片便顯示在htm上了,本人研究java的時間不長,如有問題,盡情指正。
          posted on 2010-09-25 13:43 小菜毛毛 閱讀(4298) 評論(0)  編輯  收藏 所屬分類: 面試
          主站蜘蛛池模板: 通化市| 石泉县| 武清区| 岳池县| 临泉县| 吴堡县| 连城县| 邵阳县| 阿克| 将乐县| 禹城市| 云浮市| 罗甸县| 双辽市| 万年县| 沭阳县| 桦甸市| 台安县| 乌拉特后旗| 革吉县| 吐鲁番市| 黔西| 肥乡县| 乐都县| 内乡县| 漠河县| 和政县| 灵川县| 庆安县| 富民县| 故城县| 双流县| 延边| 桃江县| 玉溪市| 若羌县| 乌审旗| 合肥市| 会昌县| 博爱县| 潮安县|