FileUtils.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.fdkk.fdkkmeta.util;
  2. import cn.hutool.core.io.IoUtil;
  3. import com.alibaba.fastjson.util.IOUtils;
  4. import org.springframework.util.MimeTypeUtils;
  5. import org.yaml.snakeyaml.util.ArrayUtils;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.*;
  9. import java.net.URLEncoder;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /**
  14. * 文件处理工具类
  15. *
  16. * @author fdkk
  17. */
  18. public class FileUtils {
  19. private static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
  20. /**
  21. * 输出指定文件的byte数组
  22. *
  23. * @param filePath 文件路径
  24. * @param os 输出流
  25. * @return
  26. */
  27. public static void writeBytes(String filePath, OutputStream os) throws IOException {
  28. FileInputStream fis = null;
  29. try {
  30. File file = new File(filePath);
  31. if (!file.exists()) {
  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. os.write(b, 0, length);
  39. }
  40. } catch (IOException e) {
  41. throw e;
  42. } finally {
  43. IOUtils.close(os);
  44. IOUtils.close(fis);
  45. }
  46. }
  47. /**
  48. * 删除文件
  49. *
  50. * @param filePath 文件
  51. * @return
  52. */
  53. public static boolean deleteFile(String filePath) {
  54. boolean flag = false;
  55. File file = new File(filePath);
  56. // 路径为文件且不为空则进行删除
  57. if (file.isFile() && file.exists()) {
  58. file.delete();
  59. flag = true;
  60. }
  61. return flag;
  62. }
  63. /**
  64. * 文件名称验证
  65. *
  66. * @param filename 文件名称
  67. * @return true 正常 false 非法
  68. */
  69. public static boolean isValidFilename(String filename) {
  70. return filename.matches(FileUtils.FILENAME_PATTERN);
  71. }
  72. public static List<String> readFileByLines2(String path) {
  73. File f = new File(path);
  74. if (!f.exists()) {
  75. return null;
  76. }
  77. FileReader reader = null;
  78. BufferedReader br = null;
  79. try {
  80. // read file content from file
  81. reader = new FileReader(path);
  82. br = new BufferedReader(reader);
  83. String str = null;
  84. List<String> result = new ArrayList<String>();
  85. while ((str = br.readLine()) != null) {
  86. result.add(str);
  87. }
  88. return result;
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. } finally {
  92. IoUtil.close(reader);
  93. IoUtil.close(br);
  94. }
  95. return null;
  96. }
  97. /**
  98. * 下载文件名重新编码
  99. *
  100. * @param response 响应对象
  101. * @param realFileName 真实文件名
  102. * @return
  103. */
  104. public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
  105. String percentEncodedFileName = FileUtils.percentEncode(realFileName);
  106. StringBuilder contentDispositionValue = new StringBuilder();
  107. contentDispositionValue.append("attachment; filename=")
  108. .append(percentEncodedFileName)
  109. .append(";")
  110. .append("filename*=")
  111. .append("utf-8''")
  112. .append(percentEncodedFileName);
  113. response.setHeader("Content-disposition", contentDispositionValue.toString());
  114. response.setHeader("download-filename", percentEncodedFileName);
  115. }
  116. /**
  117. * 百分号编码工具方法
  118. *
  119. * @param s 需要百分号编码的字符串
  120. * @return 百分号编码后的字符串
  121. */
  122. private static String percentEncode(String s) throws UnsupportedEncodingException {
  123. String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
  124. return encode.replaceAll("\\+", "%20");
  125. }
  126. /**
  127. * 获取图像后缀
  128. *
  129. * @param photoByte 图像数据
  130. * @return 后缀名
  131. */
  132. private static String getFileExtendName(byte[] photoByte) {
  133. String strFileExtendName = "jpg";
  134. if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
  135. && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
  136. strFileExtendName = "gif";
  137. } else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
  138. strFileExtendName = "jpg";
  139. } else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
  140. strFileExtendName = "bmp";
  141. } else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
  142. strFileExtendName = "png";
  143. }
  144. return strFileExtendName;
  145. }
  146. }