FileService.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.its.bis.webapp.service;
  2. import com.its.app.utils.OS;
  3. import com.its.bis.webapp.vo.FileInfoVo;
  4. import lombok.extern.slf4j.Slf4j;
  5. import net.sf.json.JSONObject;
  6. import org.springframework.core.io.InputStreamResource;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.http.ContentDisposition;
  9. import org.springframework.http.HttpHeaders;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.stereotype.Service;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.io.RandomAccessFile;
  18. import java.nio.file.Files;
  19. import java.nio.file.Path;
  20. import java.nio.file.Paths;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. @Slf4j
  24. @Service
  25. public class FileService {
  26. private final String logDir= "/logs/";
  27. private final String sysDir= System.getProperty("user.dir");
  28. private final String[] exceptDir ={"backup"};
  29. private int id = 1;
  30. public String getView(HttpServletRequest request, String fileName, String filePath) {
  31. long preEndPoint = request.getParameter("preEndPoint") == null ? 0 : Long.parseLong(request.getParameter("preEndPoint") + "");
  32. StringBuilder sb = new StringBuilder();
  33. long startPoint, endPoint;
  34. RandomAccessFile file = null;
  35. String errMsg = "";
  36. try {
  37. file = new RandomAccessFile(System.getProperty("user.dir") + filePath + fileName, "r");
  38. endPoint = file.length();
  39. startPoint = preEndPoint > 0 ? preEndPoint : endPoint < 2000 ? 0 : endPoint - 2000;
  40. file.seek(startPoint);
  41. String str;
  42. while ((str = file.readLine()) != null) {
  43. byte[] b = str.getBytes("iso-8859-1");
  44. str = new String(b, "UTF-8");
  45. sb.append(str);
  46. sb.append("<br>");
  47. endPoint = file.getFilePointer();
  48. file.seek(endPoint);
  49. }
  50. JSONObject json = new JSONObject();
  51. json.put("endPoint", endPoint);
  52. json.put("log", sb.toString());
  53. return json.toString();
  54. }
  55. catch(FileNotFoundException fnf) {
  56. log.error("FileService.getView: Exception: {}", fnf.toString());
  57. errMsg += fnf.toString();
  58. }
  59. catch (IOException e) {
  60. log.error(e.getMessage());
  61. errMsg += e.toString();
  62. }
  63. finally {
  64. try {
  65. if (file != null) file.close();
  66. }
  67. catch (Exception e) {
  68. log.error("FileService.getView: Exception: {}", e.toString());
  69. errMsg += e.toString();
  70. }
  71. }
  72. if (!errMsg.equals("")) {
  73. sb.append(errMsg);
  74. sb.append("<br>");
  75. JSONObject json = new JSONObject();
  76. json.put("endPoint", 0);
  77. json.put("log", sb.toString());
  78. return json.toString();
  79. }
  80. return null;
  81. }
  82. public ResponseEntity<Resource> fileDownload(String fileName, String filePath){
  83. try {
  84. Path path = Paths.get(System.getProperty("user.dir")+filePath+ fileName);
  85. String contentType = "application/download";
  86. HttpHeaders headers = new HttpHeaders();
  87. headers.add(HttpHeaders.CONTENT_TYPE, contentType);
  88. headers.setContentDisposition(ContentDisposition.parse("attachment;" + " filename=\"" + fileName + "\";"));
  89. Resource resource = new InputStreamResource(Files.newInputStream(path));
  90. return new ResponseEntity<>(resource, headers, HttpStatus.OK);
  91. }
  92. catch (IOException e) {
  93. log.error("FileService.fileDownload: Exception: {}", e.toString());
  94. }
  95. return null;
  96. }
  97. public void fileDelete(String fileName,String filePath) {
  98. File file = new File(this.sysDir+filePath+fileName);
  99. if (file.exists()) {
  100. if (file.delete()) {
  101. log.info("FileService.fileDelete: Delete Success: {}", filePath+fileName);
  102. }
  103. else {
  104. log.error("FileService.fileDelete: Delete Fail: {}", filePath+fileName);
  105. }
  106. }
  107. else {
  108. log.warn("FileService.fileDelete: Not exists: {}", filePath+fileName);
  109. }
  110. }
  111. public List<FileInfoVo> getLogFiles() {
  112. this.id = 1;
  113. FileInfoVo rootFile = new FileInfoVo();
  114. rootFile.setId(id);
  115. rootFile.setType("dir");
  116. this.id++;
  117. rootFile.setFileName("logs");
  118. rootFile.setFileInfos(getFiles(this.sysDir, this.logDir));
  119. List<FileInfoVo> fileInfos = new ArrayList<>();
  120. fileInfos.add(rootFile);
  121. return fileInfos;
  122. }
  123. private List<FileInfoVo> getFiles(String sysDir, String logDir) {
  124. int rootId = id - 1;
  125. //log.debug("FileService.getFiles: id: {}, sysDir: {}, logDir: {}", id, sysDir, logDir);
  126. List<FileInfoVo> subArr = new ArrayList<>();
  127. File dirFile = new File(sysDir, logDir);
  128. File[] fileList = dirFile.listFiles();
  129. for (File file: fileList) {
  130. FileInfoVo info = new FileInfoVo();
  131. //log.debug("FileService.getFiles: getName: {}, isDir: {}, getPath: {}", file.getName(), file.isDirectory(), file.getPath());
  132. if (file.isDirectory()) {
  133. for (String dir : exceptDir) {
  134. if (dir.equals(file.getName())) {
  135. break;
  136. }
  137. else {
  138. String subDir = file.getPath().substring(file.getPath().indexOf(logDir.replaceAll("/", "")), file.getPath().length());
  139. info.setId(id);
  140. id++;
  141. String sFileSeparator = "/";
  142. info.setFilePath(sFileSeparator + subDir);
  143. info.setFileName(file.getName());
  144. info.setFileSize(file.length());
  145. info.setType("dir");
  146. info.setFileInfos(getFiles(sysDir, subDir));
  147. info.setParentId(rootId);
  148. //log.info(info.toString());
  149. subArr.add(info);
  150. }
  151. }
  152. }
  153. }
  154. //log.debug("x: {}", fileList.toString());
  155. for (File file: fileList) {
  156. FileInfoVo info = new FileInfoVo();
  157. if (file.isFile() && file.getName().contains(".log")) {
  158. info.setId(id);
  159. id++;
  160. info.setFileName(file.getName());
  161. info.setFileSize(file.length());
  162. String subDir;
  163. if (OS.isWindows()) {
  164. subDir = file.getPath().substring(file.getPath().indexOf(logDir.replaceAll("/","")), file.getPath().length() - file.getName().length()).replaceAll("\\\\", "/");
  165. } else {
  166. subDir = file.getPath().substring(file.getPath().indexOf(logDir), file.getPath().length() - file.getName().length()).replaceAll("\\\\", "/");
  167. }
  168. String sFileSeparator = "/";
  169. info.setFilePath(sFileSeparator + subDir);
  170. info.setType("log");
  171. info.setParentId(rootId);
  172. //log.info(info.toString());
  173. subArr.add(info);
  174. }
  175. }
  176. //log.debug("y: {}", subArr.toString());
  177. return subArr;
  178. }
  179. }