123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package com.sw.appcloudv1.service;
- import lombok.extern.slf4j.Slf4j;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- @Service
- @Slf4j
- public class AppDownloadServiceImpl implements AppDownloadService {
- private static final Logger logger = LoggerFactory.getLogger(AppDownloadServiceImpl.class);
- /**
- * app完整包地址
- */
- @Value("${app.file.path}")
- private String filePath;
- /**
- * app更新包地址
- */
- @Value("${app.update.path}")
- private String updatePath;
- /**
- * @Description: 下载文件
- * @Param: [response, version:版本号, projectId:项目id]
- * @Return:
- */
- @Override
- public void download(HttpServletResponse response, Integer projectId) {
- projectId = projectId == null ? 1 : projectId;
- // 获取目录下的全部文件名
- List<String> listFileNames = getFileNames(filePath);
- if (listFileNames.size() == 0) {
- return;
- }
- // 获取最新版本文件名
- String newFile = getNewFileName(listFileNames, projectId);
- if (newFile == null) {
- return;
- }
- // 下载文件
- download(response, newFile, filePath);
- }
- /**
- * @Description: 获取最新的版本号
- * @Param: [projectId]
- * @Return:
- */
- @Override
- public String getNewVersion(Integer projectId) {
- projectId = projectId == null ? 1 : projectId;
- // 获取目录下的全部文件名
- List<String> listFileNames = getFileNames(filePath);
- if (listFileNames.size() == 0) {
- return null;
- }
- // 获取最新版本文件名
- String newFile = getNewFileName(listFileNames, projectId);
- if (newFile == null) {
- return null;
- }
- // 返回
- return newFile.substring(0, newFile.lastIndexOf("."));
- }
- /**
- * @Description: 流文件下载
- * @Param: [response, newFile:最新版本的文件名, path:文件目录]
- * @Return:
- */
- private void download(HttpServletResponse response, String newFile, String path) {
- InputStream fis = null;
- try {
- path = path + "/" + newFile;
- File file = new File(path);
- // 取得文件的后缀名。
- String ext = newFile.substring(newFile.lastIndexOf(".") + 1).toUpperCase();
- // 以流的形式下载文件。
- fis = new BufferedInputStream(new FileInputStream(path));
- byte[] buffer = new byte[fis.available()];
- fis.read(buffer);
- fis.close();
- // 清空response
- response.reset();
- // 设置response的Header
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(newFile.getBytes()));
- response.addHeader("Content-Length", "" + file.length());
- OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
- response.setContentType("application/octet-stream");
- toClient.write(buffer);
- toClient.flush();
- toClient.close();
- logger.info("下载app文件成功!");
- } catch (IOException ex) {
- ex.printStackTrace();
- logger.info("下载app文件失败!");
- } finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- logger.info("关闭文件流失败!");
- }
- }
- }
- }
- /**
- * @Description: 获取最新版本的文件名
- * @Return:
- */
- private String getNewFileName(List<String> listFileNames, Integer projectId) {
- String newFile = null;
- Integer maxVersionId = 1;
- for (String fileName : listFileNames) {
- Integer pid = Integer.parseInt(fileName.substring(9, 11));
- // 判断项目id
- if (pid.equals(projectId)) {
- if (newFile == null) {
- newFile = fileName;
- } else {
- // 获取最新版本号
- Integer versionId = Integer.parseInt(fileName.substring(12, 14));
- if (versionId > maxVersionId) {
- maxVersionId = versionId;
- newFile = fileName;
- }
- }
- }
- }
- return newFile;
- }
- /**
- * @Description: 获取目录下的全部文件名
- * @Return:
- */
- private List<String> getFileNames(String path) {
- // 目录下的文件名
- List<String> listFileNames = new ArrayList<>();
- // 判断是否存在目录
- File dir = new File(path);
- if (!dir.exists() || !dir.isDirectory()) {
- return new ArrayList<>();
- }
- // 读取目录下的所有目录文件信息
- String[] files = dir.list();
- // 循环,添加文件名或回调自身
- for (int i = 0; i < files.length; i++) {
- File file = new File(dir, files[i]);
- // 如果文件
- if (file.isFile()) {
- // 添加文件全路径名
- // listFileNames.add(dir + "\\" + file.getName());
- // listFileNames.add(file.getName().substring(0, file.getName().lastIndexOf(".")));
- listFileNames.add(file.getName());
- }
- }
- return listFileNames;
- }
- }
|