文件工具类,一些常用的文件方法

[java] view plain copy
  1. public class FileUtil {  
  2.   
  3.     /** 
  4.      * 将文件输出到客户端,一般用于预览 
  5.      * @param file 
  6.      * @param contentType   图片    image/jpeg 
  7.      *                      视频   audio/mpeg 
  8.      *                      应用程序   application/octet-stream 
  9.      */  
  10.     public static void renderFileToClient(File file, String contentType) {  
  11.         OutputStream toClient = null;  
  12.         InputStream fis = null;  
  13.         byte[] buffer = null;  
  14.         HttpServletResponse response = SpringMVCUtil.getResponse();  
  15.         try {  
  16.             fis = new BufferedInputStream(new FileInputStream(file));  
  17.             buffer = new byte[1024];  
  18.             response.reset();  
  19.             response.setContentLength(Long.valueOf(file.length()).intValue());  
  20.             response.setContentType(contentType);  
  21.             response.setHeader("Pragma""No-cache");  
  22.             response.setHeader("Cache-Control""no-cache");  
  23.             toClient = new BufferedOutputStream(response.getOutputStream());  
  24.             int bytesRead;  
  25.             while (-1 != (bytesRead = fis.read(buffer, 0, buffer.length))) {  
  26.                 toClient.write(buffer, 0, bytesRead);  
  27.                 toClient.flush();  
  28.             }  
  29.         } catch (Exception ex) {  
  30.             ex.printStackTrace();  
  31.         } finally {  
  32.             if (fis != null) {  
  33.                 try {  
  34.                     fis.close();  
  35.                 } catch (IOException e) {  
  36.                     e.printStackTrace();  
  37.                 }  
  38.             }  
  39.             if (toClient != null) {  
  40.                 try {  
  41.                     toClient.close();  
  42.                 } catch (IOException e) {  
  43.                     e.printStackTrace();  
  44.                 }  
  45.             }  
  46.         }  
  47.     }  
  48.       
  49.     /** 
  50.      * 获取附件存储相对路径 
  51.      *  
  52.      * @param attachmentConfig 
  53.      * @return 
  54.      */  
  55.     public static String getRelativePath() {  
  56.         StringBuilder sBuilder=new StringBuilder();  
  57.         // 模块名未定  
  58.         String dateStr = DateUtil.getCurrDateString().replaceAll("-""");  
  59.         sBuilder.append(dateStr.substring(04) + "/");  
  60.         sBuilder.append(dateStr.substring(46) + "/");  
  61.         sBuilder.append(dateStr.substring(68) + "/");  
  62.   
  63.         return sBuilder.toString();  
  64.     }  
  65.       
  66.     /** 
  67.      * 获取临时目录 
  68.      *  
  69.      * @return 
  70.      */  
  71.     public static String getTempDir() {  
  72.         String tempPath = System.getProperty("java.io.tmpdir");  
  73.         File tempFile = new File(tempPath);  
  74.         if (!tempFile.exists()) {  
  75.             tempFile.mkdirs();  
  76.         }  
  77.         return tempFile.getAbsolutePath() + "/";  
  78.     }  
  79.       
  80.     /** 
  81.      * 下载文件 
  82.      *  
  83.      * @date 2015-8-25 下午9:13:13 
  84.      * @param srcPath 
  85.      */  
  86.     public static void download(String downLoadPath) {  
  87.         HttpServletResponse response = SpringMVCUtil.getResponse();  
  88.         response.setContentType("text/html;charset=utf-8");     
  89.         response.setCharacterEncoding("UTF-8");     
  90.         BufferedInputStream bis = null;     
  91.         BufferedOutputStream bos = null;  
  92.         File file = new File(downLoadPath);  
  93.         try {     
  94.             response.setContentType("application/x-msdownload;");     
  95.             response.setHeader("Content-disposition""attachment; filename="new String(file.getName().getBytes("utf-8"), "ISO8859-1"));     
  96.             response.setHeader("Content-Length", String.valueOf(file.length()));     
  97.             bis = new BufferedInputStream(new FileInputStream(downLoadPath));     
  98.             bos = new BufferedOutputStream(response.getOutputStream());     
  99.             byte[] buff = new byte[2048];     
  100.             int bytesRead;     
  101.             while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {     
  102.                 bos.write(buff, 0, bytesRead);     
  103.             }     
  104.         } catch (Exception e) {     
  105.             e.printStackTrace();     
  106.         } finally {     
  107.             if (bis != null)  
  108.                 try {  
  109.                     bis.close();  
  110.                 } catch (IOException e) {  
  111.                     e.printStackTrace();  
  112.                 }     
  113.             if (bos != null)  
  114.                 try {  
  115.                     bos.close();  
  116.                 } catch (IOException e) {  
  117.                     e.printStackTrace();  
  118.                 }     
  119.         }     
  120.     }  
  121.       
  122.     /** 
  123.      * 向客户端输出浏览内容 
  124.      * 
  125.      * @date 2015-8-31 下午8:10:48 
  126.      * @param file  视频文件 
  127.      * @param contentType  "audio/mpeg" 
  128.      */  
  129.     public static void renderMediaToClient(File file, String contentType) {  
  130.         OutputStream toClient = null;  
  131.         InputStream fis = null;  
  132.         byte[] buffer = null;  
  133.         HttpServletResponse response = SpringMVCUtil.getResponse();  
  134.         try {  
  135.             fis = new BufferedInputStream(new FileInputStream(file));  
  136.             buffer = new byte[1024];  
  137.             response.reset();  
  138.             response.setContentLength(Long.valueOf(file.length()).intValue());  
  139.             response.setContentType(contentType);  
  140.             response.setHeader("Pragma""No-cache");  
  141.             response.setHeader("Cache-Control""no-cache");  
  142.             toClient = new BufferedOutputStream(response.getOutputStream());  
  143.             int bytesRead;  
  144.             while (-1 != (bytesRead = fis.read(buffer, 0, buffer.length))) {  
  145.                 toClient.write(buffer, 0, bytesRead);  
  146.                 toClient.flush();  
  147.             }  
  148.         } catch (Exception ex) {  
  149.             ex.printStackTrace();  
  150.         } finally {  
  151.             if (fis != null) {  
  152.                 try {  
  153.                     fis.close();  
  154.                 } catch (IOException e) {  
  155.                     e.printStackTrace();  
  156.                 }  
  157.             }  
  158.             if (toClient != null) {  
  159.                 try {  
  160.                     toClient.close();  
  161.                 } catch (IOException e) {  
  162.                     e.printStackTrace();  
  163.                 }  
  164.             }  
  165.         }  
  166.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值