FileUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.ruoyi.common.utils.file;
  2. import com.ruoyi.common.config.RuoYiConfig;
  3. import org.springframework.web.multipart.MultipartFile;
  4. import java.io.*;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import javax.net.ssl.HttpsURLConnection;
  8. import javax.servlet.http.HttpServletRequest;
  9. /**
  10. * 文件处理工具类
  11. *
  12. * @author ruoyi
  13. */
  14. public class FileUtils extends org.apache.commons.io.FileUtils {
  15. public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
  16. /**
  17. * 输出指定文件的byte数组
  18. *
  19. * @param filePath 文件路径
  20. * @param os 输出流
  21. * @return
  22. */
  23. public static void writeBytes(String filePath, OutputStream os) throws IOException {
  24. FileInputStream fis = null;
  25. try {
  26. File file = new File(filePath);
  27. if (!file.exists()) {
  28. throw new FileNotFoundException(filePath);
  29. }
  30. fis = new FileInputStream(file);
  31. byte[] b = new byte[1024];
  32. int length;
  33. while ((length = fis.read(b)) > 0) {
  34. os.write(b, 0, length);
  35. }
  36. } catch (IOException e) {
  37. throw e;
  38. } finally {
  39. if (os != null) {
  40. try {
  41. os.close();
  42. } catch (IOException e1) {
  43. e1.printStackTrace();
  44. }
  45. }
  46. if (fis != null) {
  47. try {
  48. fis.close();
  49. } catch (IOException e1) {
  50. e1.printStackTrace();
  51. }
  52. }
  53. }
  54. }
  55. /**
  56. * MultipartFile转File
  57. *
  58. * @param param
  59. * @return
  60. */
  61. public static File transfer(MultipartFile param) {
  62. if (!param.isEmpty()) {
  63. File file = null;
  64. try {
  65. InputStream in = param.getInputStream();
  66. file = new File(param.getOriginalFilename());
  67. OutputStream out = new FileOutputStream(file);
  68. int bytesRead = 0;
  69. byte[] buffer = new byte[8192];
  70. while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
  71. out.write(buffer, 0, bytesRead);
  72. }
  73. in.close();
  74. out.close();
  75. return file;
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. return file;
  79. }
  80. }
  81. return null;
  82. }
  83. /**
  84. * 删除文件
  85. *
  86. * @param filePath 文件
  87. * @return
  88. */
  89. public static boolean deleteFile(String filePath) {
  90. boolean flag = false;
  91. File file = new File(filePath);
  92. // 路径为文件且不为空则进行删除
  93. if (file.isFile() && file.exists()) {
  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. return filename.matches(FILENAME_PATTERN);
  107. }
  108. /**
  109. * 下载文件名重新编码
  110. *
  111. * @param request 请求对象
  112. * @param fileName 文件名
  113. * @return 编码后的文件名
  114. */
  115. public static String setFileDownloadHeader(HttpServletRequest request, String fileName)
  116. throws UnsupportedEncodingException {
  117. final String agent = request.getHeader("USER-AGENT");
  118. String filename = fileName;
  119. if (agent.contains("MSIE")) {
  120. // IE浏览器
  121. filename = URLEncoder.encode(filename, "utf-8");
  122. filename = filename.replace("+", " ");
  123. } else if (agent.contains("Firefox")) {
  124. // 火狐浏览器
  125. filename = new String(fileName.getBytes(), "ISO8859-1");
  126. } else if (agent.contains("Chrome")) {
  127. // google浏览器
  128. filename = URLEncoder.encode(filename, "utf-8");
  129. } else {
  130. // 其它浏览器
  131. filename = URLEncoder.encode(filename, "utf-8");
  132. }
  133. return filename;
  134. }
  135. /**
  136. * 获取指定文件的输入流
  137. *
  138. * @param logoPath 文件的路径
  139. * @return
  140. */
  141. public static InputStream getResourceAsStream(String logoPath) {
  142. return FileUtils.class.getResourceAsStream(logoPath);
  143. }
  144. /**
  145. * 下载微信头像到本地
  146. *
  147. * @param imageUrl
  148. * @return
  149. * @throws IOException
  150. */
  151. public static String avatarUrl(String imageUrl) throws IOException {
  152. URL url = new URL(imageUrl);
  153. HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  154. // 得到URL的输入流
  155. InputStream input = con.getInputStream();
  156. // 设置数据缓冲
  157. byte[] bs = new byte[1024 * 2];
  158. // 读取到的数据长度
  159. int len = 0;
  160. // 输出的文件流保存图片至本地
  161. String fileName = System.currentTimeMillis() + ".jpg";
  162. OutputStream os = new FileOutputStream(RuoYiConfig.getUploadPath() + "/" + fileName);
  163. while ((len = input.read(bs)) != -1) {
  164. os.write(bs, 0, len);
  165. }
  166. os.close();
  167. input.close();
  168. return fileName;
  169. }
  170. }