分享

JSP页面实现图片、PDF字节流的显示,Word、Excel、Zip字节流的下载功能的实现

 WindySky 2017-03-14

项目中需要把存储在数据库Blob字段中字节流进行以下相关的操作:

1.图片文件直接在页面中显示;

2.Doc,PDF等文档提示用户下载。

这个需求需要解决2个问题,第一个问题,从数据库中读取Blob字段;第二个问题,根据文件的类型,图片文件直接显示,其他文件提供下载功能。

在这里读取BLob字段的数据不是什么难点,我们知道用Blob字段是保存的二进制流文件,用Byte[]来保存即可。代码如下:

  1. //获得数据库连接     
  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是读出并需要返回的数据,类型是byte[]     
  11.          data = new byte[input.available()];     
  12.          inStream.read(data);     
  13.          inStream.close();     
  14.      }     
  15.      inStream.close();     
  16.      con.commit();     
  17.      con.close();   

对于如何按需求处理二进制流文件,我的实现方式如下:

  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.  * 对字节流进行处理,图片显示,其他提供下载  
  21.  * @param fileName 文件名称  
  22.  * @param bytes[] 文件二进制流  
  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.     }  


在前台的JSP页面中直接调用StreamOper方法即可。图片和PDF二进制流都可以在JSP中显示,Doc,Excel等流直接提示用户下载。

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

这样对于图片,直接在JSP中显示,对于其他问题提示用户下载。

最后附上一段如何读取文件转换成二进制流程代码,供大家参考:

  1. ///读取文件字节流  
  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. }  








    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多