FileUtils.java 4.3 KB

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