FileUtils.java 5.6 KB

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