AppDownloadServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.sw.appcloudv1.service;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Service;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.*;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. @Service
  12. @Slf4j
  13. public class AppDownloadServiceImpl implements AppDownloadService {
  14. private static final Logger logger = LoggerFactory.getLogger(AppDownloadServiceImpl.class);
  15. /**
  16. * app完整包地址
  17. */
  18. @Value("${app.file.path}")
  19. private String filePath;
  20. /**
  21. * app更新包地址
  22. */
  23. @Value("${app.update.path}")
  24. private String updatePath;
  25. /**
  26. * @Description: 下载文件
  27. * @Param: [response, version:版本号, projectId:项目id]
  28. * @Return:
  29. */
  30. @Override
  31. public void download(HttpServletResponse response, Integer projectId) {
  32. projectId = projectId == null ? 1 : projectId;
  33. // 获取目录下的全部文件名
  34. List<String> listFileNames = getFileNames(filePath);
  35. if (listFileNames.size() == 0) {
  36. return;
  37. }
  38. // 获取最新版本文件名
  39. String newFile = getNewFileName(listFileNames, projectId);
  40. if (newFile == null) {
  41. return;
  42. }
  43. // 下载文件
  44. download(response, newFile, filePath);
  45. }
  46. /**
  47. * @Description: 获取最新的版本号
  48. * @Param: [projectId]
  49. * @Return:
  50. */
  51. @Override
  52. public String getNewVersion(Integer projectId) {
  53. projectId = projectId == null ? 1 : projectId;
  54. // 获取目录下的全部文件名
  55. List<String> listFileNames = getFileNames(filePath);
  56. if (listFileNames.size() == 0) {
  57. return null;
  58. }
  59. // 获取最新版本文件名
  60. String newFile = getNewFileName(listFileNames, projectId);
  61. if (newFile == null) {
  62. return null;
  63. }
  64. // 返回
  65. return newFile.substring(0, newFile.lastIndexOf("."));
  66. }
  67. /**
  68. * @Description: 流文件下载
  69. * @Param: [response, newFile:最新版本的文件名, path:文件目录]
  70. * @Return:
  71. */
  72. private void download(HttpServletResponse response, String newFile, String path) {
  73. InputStream fis = null;
  74. try {
  75. path = path + "/" + newFile;
  76. File file = new File(path);
  77. // 取得文件的后缀名。
  78. String ext = newFile.substring(newFile.lastIndexOf(".") + 1).toUpperCase();
  79. // 以流的形式下载文件。
  80. fis = new BufferedInputStream(new FileInputStream(path));
  81. byte[] buffer = new byte[fis.available()];
  82. fis.read(buffer);
  83. fis.close();
  84. // 清空response
  85. response.reset();
  86. // 设置response的Header
  87. response.addHeader("Content-Disposition", "attachment;filename=" + new String(newFile.getBytes()));
  88. response.addHeader("Content-Length", "" + file.length());
  89. OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  90. response.setContentType("application/octet-stream");
  91. toClient.write(buffer);
  92. toClient.flush();
  93. toClient.close();
  94. logger.info("下载app文件成功!");
  95. } catch (IOException ex) {
  96. ex.printStackTrace();
  97. logger.info("下载app文件失败!");
  98. } finally {
  99. if (fis != null) {
  100. try {
  101. fis.close();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. logger.info("关闭文件流失败!");
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * @Description: 获取最新版本的文件名
  111. * @Return:
  112. */
  113. private String getNewFileName(List<String> listFileNames, Integer projectId) {
  114. String newFile = null;
  115. Integer maxVersionId = 1;
  116. for (String fileName : listFileNames) {
  117. Integer pid = Integer.parseInt(fileName.substring(9, 11));
  118. // 判断项目id
  119. if (pid.equals(projectId)) {
  120. if (newFile == null) {
  121. newFile = fileName;
  122. } else {
  123. // 获取最新版本号
  124. Integer versionId = Integer.parseInt(fileName.substring(12, 14));
  125. if (versionId > maxVersionId) {
  126. maxVersionId = versionId;
  127. newFile = fileName;
  128. }
  129. }
  130. }
  131. }
  132. return newFile;
  133. }
  134. /**
  135. * @Description: 获取目录下的全部文件名
  136. * @Return:
  137. */
  138. private List<String> getFileNames(String path) {
  139. // 目录下的文件名
  140. List<String> listFileNames = new ArrayList<>();
  141. // 判断是否存在目录
  142. File dir = new File(path);
  143. if (!dir.exists() || !dir.isDirectory()) {
  144. return new ArrayList<>();
  145. }
  146. // 读取目录下的所有目录文件信息
  147. String[] files = dir.list();
  148. // 循环,添加文件名或回调自身
  149. for (int i = 0; i < files.length; i++) {
  150. File file = new File(dir, files[i]);
  151. // 如果文件
  152. if (file.isFile()) {
  153. // 添加文件全路径名
  154. // listFileNames.add(dir + "\\" + file.getName());
  155. // listFileNames.add(file.getName().substring(0, file.getName().lastIndexOf(".")));
  156. listFileNames.add(file.getName());
  157. }
  158. }
  159. return listFileNames;
  160. }
  161. }