FileUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.future.common.utils.file;
  2. import com.future.common.utils.StringUtils;
  3. import org.apache.commons.lang3.ArrayUtils;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.*;
  7. import java.net.URLEncoder;
  8. import java.nio.charset.StandardCharsets;
  9. /**
  10. * 文件处理工具类
  11. *
  12. * @author future
  13. */
  14. public class FileUtils extends org.apache.commons.io.FileUtils
  15. {
  16. public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
  17. /**
  18. * 输出指定文件的byte数组
  19. *
  20. * @param filePath 文件路径
  21. * @param os 输出流
  22. * @return
  23. */
  24. public static void writeBytes(String filePath, OutputStream os) throws IOException
  25. {
  26. FileInputStream fis = null;
  27. try
  28. {
  29. File file = new File(filePath);
  30. if (!file.exists())
  31. {
  32. throw new FileNotFoundException(filePath);
  33. }
  34. fis = new FileInputStream(file);
  35. byte[] b = new byte[1024];
  36. int length;
  37. while ((length = fis.read(b)) > 0)
  38. {
  39. os.write(b, 0, length);
  40. }
  41. }
  42. catch (IOException e)
  43. {
  44. throw e;
  45. }
  46. finally
  47. {
  48. if (os != null)
  49. {
  50. try
  51. {
  52. os.close();
  53. }
  54. catch (IOException e1)
  55. {
  56. e1.printStackTrace();
  57. }
  58. }
  59. if (fis != null)
  60. {
  61. try
  62. {
  63. fis.close();
  64. }
  65. catch (IOException e1)
  66. {
  67. e1.printStackTrace();
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * 删除文件
  74. *
  75. * @param filePath 文件
  76. * @return
  77. */
  78. public static boolean deleteFile(String filePath)
  79. {
  80. boolean flag = false;
  81. File file = new File(filePath);
  82. // 路径为文件且不为空则进行删除
  83. if (file.isFile() && file.exists())
  84. {
  85. file.delete();
  86. flag = true;
  87. }
  88. return flag;
  89. }
  90. /**
  91. * 文件名称验证
  92. *
  93. * @param filename 文件名称
  94. * @return true 正常 false 非法
  95. */
  96. public static boolean isValidFilename(String filename)
  97. {
  98. return filename.matches(FILENAME_PATTERN);
  99. }
  100. /**
  101. * 检查文件是否可下载
  102. *
  103. * @param resource 需要下载的文件
  104. * @return true 正常 false 非法
  105. */
  106. public static boolean checkAllowDownload(String resource)
  107. {
  108. // 禁止目录上跳级别
  109. if (StringUtils.contains(resource, ".."))
  110. {
  111. return false;
  112. }
  113. // 检查允许下载的文件规则
  114. if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
  115. {
  116. return true;
  117. }
  118. // 不在允许下载的文件规则
  119. return false;
  120. }
  121. /**
  122. * 下载文件名重新编码
  123. *
  124. * @param request 请求对象
  125. * @param fileName 文件名
  126. * @return 编码后的文件名
  127. */
  128. public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
  129. {
  130. final String agent = request.getHeader("USER-AGENT");
  131. String filename = fileName;
  132. if (agent.contains("MSIE"))
  133. {
  134. // IE浏览器
  135. filename = URLEncoder.encode(filename, "utf-8");
  136. filename = filename.replace("+", " ");
  137. }
  138. else if (agent.contains("Firefox"))
  139. {
  140. // 火狐浏览器
  141. filename = new String(fileName.getBytes(), "ISO8859-1");
  142. }
  143. else if (agent.contains("Chrome"))
  144. {
  145. // google浏览器
  146. filename = URLEncoder.encode(filename, "utf-8");
  147. }
  148. else
  149. {
  150. // 其它浏览器
  151. filename = URLEncoder.encode(filename, "utf-8");
  152. }
  153. return filename;
  154. }
  155. /**
  156. * 下载文件名重新编码
  157. *
  158. * @param response 响应对象
  159. * @param realFileName 真实文件名
  160. * @return
  161. */
  162. public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
  163. {
  164. String percentEncodedFileName = percentEncode(realFileName);
  165. StringBuilder contentDispositionValue = new StringBuilder();
  166. contentDispositionValue.append("attachment; filename=")
  167. .append(percentEncodedFileName)
  168. .append(";")
  169. .append("filename*=")
  170. .append("utf-8''")
  171. .append(percentEncodedFileName);
  172. response.setHeader("Content-disposition", contentDispositionValue.toString());
  173. }
  174. /**
  175. * 百分号编码工具方法
  176. *
  177. * @param s 需要百分号编码的字符串
  178. * @return 百分号编码后的字符串
  179. */
  180. public static String percentEncode(String s) throws UnsupportedEncodingException
  181. {
  182. String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
  183. return encode.replaceAll("\\+", "%20");
  184. }
  185. }