FileUtils.java 3.6 KB

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