FileUtils.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.kede.common.utils.excel;
  2. import com.kede.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 ruoyi
  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. public static final String[] DEFAULT_ALLOWED_EXTENSION = {
  18. // 图片
  19. "bmp", "gif", "jpg", "jpeg", "png",
  20. // word excel powerpoint
  21. "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
  22. // 压缩文件
  23. "rar", "zip", "gz", "bz2",
  24. // pdf
  25. "pdf" };
  26. /**
  27. * 输出指定文件的byte数组
  28. *
  29. * @param filePath 文件路径
  30. * @param os 输出流
  31. * @return
  32. */
  33. public static void writeBytes(String filePath, OutputStream os) throws IOException
  34. {
  35. FileInputStream fis = null;
  36. try
  37. {
  38. File file = new File(filePath);
  39. if (!file.exists())
  40. {
  41. throw new FileNotFoundException(filePath);
  42. }
  43. fis = new FileInputStream(file);
  44. byte[] b = new byte[1024];
  45. int length;
  46. while ((length = fis.read(b)) > 0)
  47. {
  48. os.write(b, 0, length);
  49. }
  50. }
  51. catch (IOException e)
  52. {
  53. throw e;
  54. }
  55. finally
  56. {
  57. if (os != null)
  58. {
  59. try
  60. {
  61. os.close();
  62. }
  63. catch (IOException e1)
  64. {
  65. e1.printStackTrace();
  66. }
  67. }
  68. if (fis != null)
  69. {
  70. try
  71. {
  72. fis.close();
  73. }
  74. catch (IOException e1)
  75. {
  76. e1.printStackTrace();
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * 删除文件
  83. *
  84. * @param filePath 文件
  85. * @return
  86. */
  87. public static boolean deleteFile(String filePath)
  88. {
  89. boolean flag = false;
  90. File file = new File(filePath);
  91. // 路径为文件且不为空则进行删除
  92. if (file.isFile() && file.exists())
  93. {
  94. file.delete();
  95. flag = true;
  96. }
  97. return flag;
  98. }
  99. /**
  100. * 文件名称验证
  101. *
  102. * @param filename 文件名称
  103. * @return true 正常 false 非法
  104. */
  105. public static boolean isValidFilename(String filename)
  106. {
  107. return filename.matches(FILENAME_PATTERN);
  108. }
  109. /**
  110. * 检查文件是否可下载
  111. *
  112. * @param resource 需要下载的文件
  113. * @return true 正常 false 非法
  114. */
  115. public static boolean checkAllowDownload(String resource)
  116. {
  117. // 禁止目录上跳级别
  118. if (StringUtils.contains(resource, ".."))
  119. {
  120. return false;
  121. }
  122. // 检查允许下载的文件规则
  123. if (ArrayUtils.contains(DEFAULT_ALLOWED_EXTENSION, getFileType(resource)))
  124. {
  125. return true;
  126. }
  127. // 不在允许下载的文件规则
  128. return false;
  129. }
  130. /**
  131. * 下载文件名重新编码
  132. *
  133. * @param request 请求对象
  134. * @param fileName 文件名
  135. * @return 编码后的文件名
  136. */
  137. public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
  138. {
  139. final String agent = request.getHeader("USER-AGENT");
  140. String filename = fileName;
  141. if (agent.contains("MSIE"))
  142. {
  143. // IE浏览器
  144. filename = URLEncoder.encode(filename, "utf-8");
  145. filename = filename.replace("+", " ");
  146. }
  147. else if (agent.contains("Firefox"))
  148. {
  149. // 火狐浏览器
  150. filename = new String(fileName.getBytes(), "ISO8859-1");
  151. }
  152. else if (agent.contains("Chrome"))
  153. {
  154. // google浏览器
  155. filename = URLEncoder.encode(filename, "utf-8");
  156. }
  157. else
  158. {
  159. // 其它浏览器
  160. filename = URLEncoder.encode(filename, "utf-8");
  161. }
  162. return filename;
  163. }
  164. /**
  165. * 下载文件名重新编码
  166. *
  167. * @param response 响应对象
  168. * @param realFileName 真实文件名
  169. * @return
  170. */
  171. public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName,String attName) throws UnsupportedEncodingException
  172. {
  173. String percentEncodedFileName=null;
  174. if (attName!=null){
  175. percentEncodedFileName=percentEncode(attName);
  176. } else{
  177. percentEncodedFileName=percentEncode(realFileName);
  178. }
  179. StringBuilder contentDispositionValue = new StringBuilder();
  180. contentDispositionValue.append("attachment; filename=")
  181. .append(percentEncodedFileName)
  182. .append(";")
  183. .append("filename*=")
  184. .append("utf-8''")
  185. .append(percentEncodedFileName);
  186. response.setHeader("Content-disposition", contentDispositionValue.toString());
  187. }
  188. /**
  189. * 百分号编码工具方法
  190. *
  191. * @param s 需要百分号编码的字符串
  192. * @return 百分号编码后的字符串
  193. */
  194. public static String percentEncode(String s) throws UnsupportedEncodingException
  195. {
  196. String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
  197. return encode.replaceAll("\\+", "%20");
  198. }
  199. /**
  200. * 获取文件类型
  201. * <p>
  202. * 例如: ruoyi.txt, 返回: txt
  203. *
  204. * @param fileName 文件名
  205. * @return 后缀(不含".")
  206. */
  207. public static String getFileType(String fileName)
  208. {
  209. int separatorIndex = fileName.lastIndexOf(".");
  210. if (separatorIndex < 0)
  211. {
  212. return "";
  213. }
  214. return fileName.substring(separatorIndex + 1).toLowerCase();
  215. }
  216. }