FileUtils.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.shs.official.common.utils.file;
  2. import javax.servlet.http.HttpServletRequest;
  3. import java.io.*;
  4. import java.net.URLEncoder;
  5. /**
  6. * 文件处理工具类
  7. *
  8. * @author shs
  9. */
  10. public class FileUtils
  11. {
  12. public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
  13. /**
  14. * 输出指定文件的byte数组
  15. *
  16. * @param filePath 文件路径
  17. * @param os 输出流
  18. * @return
  19. */
  20. public static void writeBytes(String filePath, OutputStream os) throws IOException
  21. {
  22. FileInputStream fis = null;
  23. try
  24. {
  25. File file = new File(filePath);
  26. if (!file.exists())
  27. {
  28. throw new FileNotFoundException(filePath);
  29. }
  30. fis = new FileInputStream(file);
  31. byte[] b = new byte[1024];
  32. int length;
  33. while ((length = fis.read(b)) > 0)
  34. {
  35. os.write(b, 0, length);
  36. }
  37. }
  38. catch (IOException e)
  39. {
  40. throw e;
  41. }
  42. finally
  43. {
  44. if (os != null)
  45. {
  46. try
  47. {
  48. os.close();
  49. }
  50. catch (IOException e1)
  51. {
  52. e1.printStackTrace();
  53. }
  54. }
  55. if (fis != null)
  56. {
  57. try
  58. {
  59. fis.close();
  60. }
  61. catch (IOException e1)
  62. {
  63. e1.printStackTrace();
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * 删除文件
  70. *
  71. * @param filePath 文件
  72. * @return
  73. */
  74. public static boolean deleteFile(String filePath)
  75. {
  76. boolean flag = false;
  77. File file = new File(filePath);
  78. // 路径为文件且不为空则进行删除
  79. if (file.isFile() && file.exists())
  80. {
  81. file.delete();
  82. flag = true;
  83. }
  84. return flag;
  85. }
  86. /**
  87. * 文件名称验证
  88. *
  89. * @param filename 文件名称
  90. * @return true 正常 false 非法
  91. */
  92. public static boolean isValidFilename(String filename)
  93. {
  94. return filename.matches(FILENAME_PATTERN);
  95. }
  96. /**
  97. * 下载文件名重新编码
  98. *
  99. * @param request 请求对象
  100. * @param fileName 文件名
  101. * @return 编码后的文件名
  102. */
  103. public static String setFileDownloadHeader(HttpServletRequest request, String fileName)
  104. throws UnsupportedEncodingException
  105. {
  106. final String agent = request.getHeader("USER-AGENT");
  107. String filename = fileName;
  108. if (agent.contains("MSIE"))
  109. {
  110. // IE浏览器
  111. filename = URLEncoder.encode(filename, "utf-8");
  112. filename = filename.replace("+", " ");
  113. }
  114. else if (agent.contains("Firefox"))
  115. {
  116. // 火狐浏览器
  117. filename = new String(fileName.getBytes(), "ISO8859-1");
  118. }
  119. else if (agent.contains("Chrome"))
  120. {
  121. // google浏览器
  122. filename = URLEncoder.encode(filename, "utf-8");
  123. }
  124. else
  125. {
  126. // 其它浏览器
  127. filename = URLEncoder.encode(filename, "utf-8");
  128. }
  129. return filename;
  130. }
  131. }