FileUtil.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package com.jiayue.biz.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.io.FileUtils;
  4. import org.springframework.util.ResourceUtils;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.*;
  8. import java.net.URLDecoder;
  9. import java.net.URLEncoder;
  10. import java.nio.charset.StandardCharsets;
  11. import java.nio.file.Files;
  12. import java.nio.file.LinkOption;
  13. import java.nio.file.Path;
  14. import java.nio.file.Paths;
  15. import java.nio.file.attribute.BasicFileAttributeView;
  16. import java.nio.file.attribute.BasicFileAttributes;
  17. import java.security.MessageDigest;
  18. import java.util.Date;
  19. /**
  20. * 文件工具类
  21. *
  22. * @author xsl
  23. * @version 3.0
  24. */
  25. @Slf4j
  26. public class FileUtil {
  27. /**
  28. * 移动本地文件
  29. *
  30. * @param remoteAbsoluteFile 远程文件名(包括完整路径)
  31. * @param localAbsoluteFile 本地文件名(包括完整路径)
  32. * @return 成功时,返回true,失败返回false
  33. * @throws Exception
  34. */
  35. public static void move(String localAbsoluteFile, String remoteAbsoluteFile) throws Exception {
  36. File srcFile = new File(localAbsoluteFile);
  37. File destDir = new File(remoteAbsoluteFile);
  38. try {
  39. if (!destDir.exists()) {// 如果目录不存在则创建目录
  40. boolean b = destDir.mkdirs();
  41. if (!b) // 如果创建失败则抛出异常
  42. throw new RuntimeException(destDir + " 目录创建失败");
  43. }
  44. File file = new File(remoteAbsoluteFile + File.separator + srcFile.getName());
  45. if(file.exists()){
  46. file.delete();
  47. }
  48. FileUtils.moveFile(srcFile, new File(remoteAbsoluteFile + File.separator + srcFile.getName()));
  49. } catch (IOException e) {
  50. throw new Exception("文件:" + srcFile.getName() + "移动到" + destDir.getPath() + "失败。", e);
  51. }
  52. }
  53. //文件内容md5加密
  54. public static String getMD5(byte[] fileByte) {
  55. String md5 = new String();
  56. try {
  57. MessageDigest md = MessageDigest.getInstance("MD5");
  58. md.update(fileByte);
  59. byte b[] = md.digest();
  60. int i;
  61. StringBuffer buf = new StringBuffer("");
  62. for (int offset = 0; offset < b.length; offset++) {
  63. i = b[offset];
  64. if (i < 0)
  65. i += 256;
  66. if (i < 16)
  67. buf.append("0");
  68. buf.append(Integer.toHexString(i));
  69. }
  70. md5 = buf.toString();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. return md5;
  75. }
  76. /**
  77. * 获取文件创建时间
  78. *
  79. * @param fullFileName
  80. * @return
  81. */
  82. public static Long getFileCreateTime(String fullFileName) {
  83. Path path = Paths.get(fullFileName);
  84. BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
  85. BasicFileAttributes attr;
  86. try {
  87. attr = basicview.readAttributes();
  88. Date createDate = new Date(attr.creationTime().toMillis());
  89. return createDate.getTime();
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. return new Date().getTime();
  94. }
  95. /**
  96. * 获取项目根路径
  97. *
  98. * @return
  99. */
  100. public static String getResourceBasePath() {
  101. // 获取跟目录
  102. File path = null;
  103. try {
  104. path = new File(ResourceUtils.getURL("classpath:").getPath());
  105. } catch (FileNotFoundException e) {
  106. // nothing to do
  107. }
  108. if (path == null || !path.exists()) {
  109. path = new File("");
  110. }
  111. String pathStr = path.getAbsolutePath();
  112. try {
  113. pathStr = URLDecoder.decode(pathStr, "UTF-8");
  114. } catch (UnsupportedEncodingException e) {
  115. e.printStackTrace();
  116. }
  117. // 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
  118. // pathStr = pathStr.replace("\\target\\classes", "");
  119. return pathStr;
  120. }
  121. //下载文件
  122. public static void downloadFile(File file, HttpServletResponse response) {
  123. String name = file.getName();
  124. FileInputStream fileInputStream = null;
  125. ServletOutputStream outputStream = null;
  126. try {
  127. fileInputStream = new FileInputStream(file);
  128. String encode = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
  129. response.setContentType("application/octet-stream");
  130. String percentEncodedFileName = encode.replaceAll("\\+", "%20");
  131. String contentDispositionValue =
  132. "attachment; filename=" + percentEncodedFileName;
  133. response.setHeader("Content-disposition", contentDispositionValue);
  134. int len = 0;
  135. byte[] buffer = new byte[1024];
  136. outputStream = response.getOutputStream();
  137. while ((len = fileInputStream.read(buffer)) > 0) {
  138. outputStream.write(buffer, 0, len);
  139. }
  140. fileInputStream.close();
  141. outputStream.flush();
  142. outputStream.close();
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. }
  146. }
  147. /**
  148. * 获取上报文件目录相对路径
  149. *
  150. * @return
  151. */
  152. public static String getFileUploadPath() {
  153. return createUploadAllDir("uploadFile");
  154. }
  155. /**
  156. * 获取上传测风塔附件相对路径
  157. *
  158. * @return
  159. */
  160. public static String getEnclosurePath() {
  161. return createUploadAllDir("enclosure");
  162. }
  163. /**
  164. * 获取下载文件目录相对路径
  165. *
  166. * @return
  167. */
  168. public static String getDownloadFilePath() {
  169. return createUploadAllDir("downloadFile");
  170. }
  171. /**
  172. * 获取下载文件目录相对路径
  173. *
  174. * @return
  175. */
  176. public static String getZxglFilePath() {
  177. return createUploadAllDir("zxgl");
  178. }
  179. /**
  180. * 获取日志目录相对路径
  181. *
  182. * @return
  183. */
  184. public static String getLogsPath() {
  185. return createUploadAllDir("logs");
  186. }
  187. /**
  188. * 获取系统脚本相对路径
  189. *
  190. * @return
  191. */
  192. public static String getBinPath() {
  193. return createUploadAllDir("bin");
  194. }
  195. /**
  196. * 程序备份相对路径
  197. *
  198. * @return
  199. */
  200. public static String getOriginalPath() {
  201. return createUploadAllDir("original");
  202. }
  203. /**
  204. * 系统资料相对路径
  205. *
  206. * @return
  207. */
  208. public static String getSystemFilePath() {
  209. return createUploadAllDir("systemFile");
  210. }
  211. /**
  212. * 获取被删除文件目录
  213. *
  214. * @return 路径
  215. */
  216. public static String getDeleteFilePath() {
  217. return createUploadAllDir("deleteFile");
  218. }
  219. /**
  220. * 程序运行相对路径
  221. *
  222. * @return
  223. */
  224. public static String getProducePath() {
  225. return createUploadAllDir("produce");
  226. }
  227. /**
  228. * 报表相对路径
  229. *
  230. * @return
  231. */
  232. public static String getreportFormPath() {
  233. return createUploadAllDir("reportForm");
  234. }
  235. /**
  236. * 获取临时文件目录相对路径
  237. *
  238. * @return
  239. */
  240. public static String getTempFilePath() {
  241. return createUploadAllDir("tempFile");
  242. }
  243. public static String getE46Path() {
  244. return createUploadAllDir("HN");
  245. }
  246. private static String createUploadAllDir(String dir) {
  247. String path = "";
  248. /*if (System.getProperties().getProperty("file.separator").equals("\\")) {
  249. path = new File(getResourceBasePath()).getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + dir;
  250. try {
  251. path = URLDecoder.decode(path, "UTF-8");
  252. } catch (UnsupportedEncodingException e) {
  253. e.printStackTrace();
  254. }
  255. File file = new File(path);
  256. if (!file.exists()) {
  257. boolean b = file.mkdirs();
  258. if (!b)
  259. log.error("目录创建失败" + path);
  260. }
  261. } else {
  262. path = "D:\\program\\" + dir;
  263. }*/
  264. path = "D:\\program\\" + dir;
  265. return path;
  266. }
  267. /**
  268. * 获取环境数据导出文件目录相对路径
  269. *
  270. * @return
  271. */
  272. public static String getEnvironmentalDataFile() {
  273. return createUploadAllDir("environmentalDataFile");
  274. }
  275. }