小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JSP頁面實(shí)現(xiàn)圖片、PDF字節(jié)流的顯示,Word、Excel、Zip字節(jié)流的下載功能的實(shí)現(xiàn)

 WindySky 2017-03-14

項(xiàng)目中需要把存儲(chǔ)在數(shù)據(jù)庫Blob字段中字節(jié)流進(jìn)行以下相關(guān)的操作:

1.圖片文件直接在頁面中顯示;

2.Doc,PDF等文檔提示用戶下載。

這個(gè)需求需要解決2個(gè)問題,第一個(gè)問題,從數(shù)據(jù)庫中讀取Blob字段;第二個(gè)問題,根據(jù)文件的類型,圖片文件直接顯示,其他文件提供下載功能。

在這里讀取BLob字段的數(shù)據(jù)不是什么難點(diǎn),我們知道用Blob字段是保存的二進(jìn)制流文件,用Byte[]來保存即可。代碼如下:

  1. //獲得數(shù)據(jù)庫連接     
  2.      Connection con = ConnectionFactory.getConnection();     
  3.      con.setAutoCommit(false);     
  4.      Statement st = con.createStatement();        
  5.      ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");     
  6.      if (rs.next())     
  7.      {     
  8.          java.sql.Blob blob = rs.getBlob("BLOBATTR");     
  9.          InputStream inStream = blob.getBinaryStream();     
  10.          //data是讀出并需要返回的數(shù)據(jù),類型是byte[]     
  11.          data = new byte[input.available()];     
  12.          inStream.read(data);     
  13.          inStream.close();     
  14.      }     
  15.      inStream.close();     
  16.      con.commit();     
  17.      con.close();   

對于如何按需求處理二進(jìn)制流文件,我的實(shí)現(xiàn)方式如下:

  1.     static {  
  2.     MIME = new Hashtable();  
  3.     MIME.put("jpeg", "image/jpeg");  
  4.     MIME.put("jpg", "image/jpeg");  
  5.     MIME.put("jfif", "image/jpeg");  
  6.     MIME.put("jfif-tbnl", "image/jpeg");  
  7.     MIME.put("jpe", "image/jpeg");  
  8.     MIME.put("jfif", "image/jpeg");  
  9.     MIME.put("tiff", "image/tiff");  
  10.     MIME.put("tif", "image/tiff");  
  11.     MIME.put("gif", "image/gif");  
  12.     MIME.put("xls", "application/x-msexcel");  
  13.     MIME.put("doc", "application/msword");  
  14.     MIME.put("ppt", "application/x-mspowerpoint");  
  15.     MIME.put("zip", "application/x-zip-compressed");  
  16.     MIME.put("pdf", "application/pdf");  
  17. }  
  18.   
  19. /**  
  20.  * 對字節(jié)流進(jìn)行處理,圖片顯示,其他提供下載  
  21.  * @param fileName 文件名稱  
  22.  * @param bytes[] 文件二進(jìn)制流  
  23.  * @param down 是否下載  
  24.  *   
  25.  * @return  
  26.  */  
  27. public static void StreamOper(HttpServletResponse response, String fileName, byte bytes[], boolean down)  
  28.     throws IOException {  
  29.         int index = 0;  
  30.         String ext = "";  
  31.         if ((index = fileName.indexOf('.')) > 0)  
  32.             ext = fileName.substring(index + 1);  
  33.         //通過文件名的后綴判斷文件的格式  
  34.         String mime = (String) MIME.get(ext);  
  35.         if (mime == null)  
  36.             mime = "application/x-msdownload";  
  37.   
  38.                        response.setContentType(mime);  
  39.         //是否需要提供下載  
  40.         if (down)  
  41.             response.setHeader("Content-Disposition", "attachment; filename=" + fileName);  
  42.         OutputStream outStream = response.getOutputStream();  
  43.         outStream.write(bytes, 0, bytes.length);  
  44.         outStream.flush();  
  45.         outStream.close();  
  46.     }  


在前臺(tái)的JSP頁面中直接調(diào)用StreamOper方法即可。圖片和PDF二進(jìn)制流都可以在JSP中顯示,Doc,Excel等流直接提示用戶下載。

  1. <%  
  2.   
  3.     FileDownloader.StreamOper(response, filename, pic, false);  
  4.       
  5. %>  

這樣對于圖片,直接在JSP中顯示,對于其他問題提示用戶下載。

最后附上一段如何讀取文件轉(zhuǎn)換成二進(jìn)制流程代碼,供大家參考:

  1. ///讀取文件字節(jié)流  
  2. public static byte[] readerFileStream(String fileName)  
  3.         throws IOException {  
  4.     File f = new File(fileName);  
  5.     int length = (int)f.length();  
  6.       
  7.     byte[] buff = new byte[length];  
  8.       
  9.     BufferedInputStream bis = null;  
  10.       
  11.   
  12.     bis = new BufferedInputStream(new FileInputStream(fileName));  
  13.     int bytesRead;  
  14.     while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
  15.     }  
  16.       
  17.     return buff;  
  18. }  








    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多