From fd55f7cfae9bfe450f6fdd78103667ec9bd3b666 Mon Sep 17 00:00:00 2001 From: beeajax <1105173470@qq.com> Date: Fri, 28 Jul 2023 16:48:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=99=9A=E6=8B=9F=E6=9C=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=90=8C=E6=AD=A5=E5=88=B0=E8=B0=83=E5=BA=A6?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/FileController.java | 55 +++--- .../controller/SchVirtualLogController.java | 8 +- .../server/service/IFileUploadService.java | 2 - .../service/impl/FileUploadServiceImpl.java | 166 ------------------ .../src/main/resources/application.properties | 4 +- .../src/main/resources/bootstrap.yml | 8 +- .../com/docus/server/TerminalBootstrap.java | 2 +- .../server/common/SchVirtualLogTask.java | 52 ++++++ .../src/main/resources/bootstrap.yml | 10 +- .../java/com/docus/server/FileController.java | 16 +- .../test/java/com/docus/server/FileUtils.java | 162 +++++++++++++++++ .../api/scheduling.management/FileApi.java | 87 ++++----- .../SchVirtualLogApi.java | 4 +- .../schvirtuallog/AddSchVirtualLogDTO.java | 4 +- .../schvirtuallog/EditSchVirtualLogDTO.java | 21 +-- .../scheduling.management/SchVirtualLog.java | 4 +- pom.xml | 6 +- 17 files changed, 319 insertions(+), 292 deletions(-) create mode 100644 collector-terminal-management/src/main/java/com/docus/server/common/SchVirtualLogTask.java create mode 100644 collector-terminal-management/src/test/java/com/docus/server/FileUtils.java diff --git a/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java b/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java index 6d15adf..fff4c6a 100644 --- a/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java +++ b/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java @@ -1,21 +1,18 @@ package com.docus.server.controller; -import com.docus.server.api.scheduling.management.FileApi; import com.docus.server.service.IFileUploadService; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestParam; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; /** * 文件上传下载 API @@ -23,37 +20,27 @@ import java.io.IOException; * @author AutoGenerator * @since 2023-07-15 */ +@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口") @RestController -public class FileController implements FileApi { +@RequestMapping("/sch/file") +public class FileController { @Resource private IFileUploadService iFileUploadService; - @Resource - private RestTemplate restTemplate; - @Override + @ApiOperation("文件下载") + @GetMapping("/download") +// @Override public void downloadFile(String filePath, HttpServletResponse response) throws Exception { iFileUploadService.downloadFile(filePath, response); } - @Override - public void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception { - iFileUploadService.uploadFile(multipartFiles, pathKey); - } - - @Override - public void downLoadFromUrl(String urlStr, String fileName, String savePath) throws Exception { - iFileUploadService.downLoadFromUrl(urlStr, fileName, savePath); - } - - @Override - public ResponseEntity downloadFile(@RequestParam String url) throws IOException { - HttpEntity request = new HttpEntity<>(new HttpHeaders()); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, request, byte[].class); - ByteArrayResource resource = new ByteArrayResource(response.getBody()); - return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "your_file_name") - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .contentLength(resource.contentLength()) - .body(resource); + @ApiOperation("文件上传") + @PostMapping("/upload") + @ApiImplicitParams({ + @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class) + }) +// @Override + public void uploadFile(MultipartFile[] files, String pathKey) throws Exception { + iFileUploadService.uploadFile(files, pathKey); } } diff --git a/collector-scheduling-management/src/main/java/com/docus/server/controller/SchVirtualLogController.java b/collector-scheduling-management/src/main/java/com/docus/server/controller/SchVirtualLogController.java index 181b93e..e7d7b29 100644 --- a/collector-scheduling-management/src/main/java/com/docus/server/controller/SchVirtualLogController.java +++ b/collector-scheduling-management/src/main/java/com/docus/server/controller/SchVirtualLogController.java @@ -52,8 +52,8 @@ public class SchVirtualLogController implements SchVirtualLogApi { * @return 成功或失败 */ @Override - public boolean add(AddSchVirtualLogDTO addSchVirtualLogDTO) { - return iSchVirtualLogService.add(addSchVirtualLogDTO); + public void add(AddSchVirtualLogDTO addSchVirtualLogDTO) { + iSchVirtualLogService.add(addSchVirtualLogDTO); } /** @@ -63,8 +63,8 @@ public class SchVirtualLogController implements SchVirtualLogApi { * @return 成功或失败 */ @Override - public boolean edit(EditSchVirtualLogDTO editSchVirtualLogDTO) { - return iSchVirtualLogService.edit(editSchVirtualLogDTO); + public void edit(EditSchVirtualLogDTO editSchVirtualLogDTO) { + iSchVirtualLogService.edit(editSchVirtualLogDTO); } /** diff --git a/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java b/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java index e46ed31..00e46ec 100644 --- a/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java +++ b/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java @@ -11,6 +11,4 @@ public interface IFileUploadService { List uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception; void downloadFile(String filePath, HttpServletResponse response); - - void downLoadFromUrl(String urlStr, String fileName, String savePath) throws Exception; } diff --git a/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java b/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java index eb92f4e..647a2bb 100644 --- a/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java +++ b/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java @@ -7,32 +7,17 @@ import com.docus.infrastructure.web.exception.ExceptionCode; import com.docus.server.service.IFileUploadService; import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.httpclient.util.URIUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; import java.net.URLEncoder; -import java.security.cert.CertificateException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -50,8 +35,6 @@ public class FileUploadServiceImpl implements IFileUploadService { private static DateTimeFormatter ymdDtf = DateTimeFormatter.ofPattern("yyyyMMdd"); - private String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"; - @Override public List uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception { @@ -137,155 +120,6 @@ public class FileUploadServiceImpl implements IFileUploadService { } } - /** - * 设置不验证主机 - */ - private final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; - } - }; - - @Override - public void downLoadFromUrl(String urlStr, String fileName, String savePath) throws Exception { - long start = System.currentTimeMillis(); - urlStr = urlStr.replace("\\", "/"); - urlStr = URIUtil.encodePathQuery(urlStr); - URL url = new URL(urlStr); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - try { - boolean useHttps = urlStr.toLowerCase().startsWith("https"); - if (useHttps) { - HttpsURLConnection https = (HttpsURLConnection) conn; - trustAllHosts(https); - https.setHostnameVerifier(DO_NOT_VERIFY); - } - //设置超时间为3秒 - //防止屏蔽程序抓取而返回403错误 - conn.setRequestProperty("User-Agent", userAgent); - conn.setRequestProperty("Accept-Encoding", "identity"); - conn.setConnectTimeout(8 * 1000); - conn.setReadTimeout(8 * 1000); - conn = reload(conn); - long length = conn.getContentLength(); - if (length < 0) { - String values = conn.getHeaderField("Content-Length"); - if (values != null && !values.isEmpty()) { - length = Long.parseLong(values); - } - } - InputStream inputStream = null; - if (conn.getResponseCode() >= 400) { - throw new Exception("文件不存在"); - } else { - inputStream = conn.getInputStream(); - } - //获取自己数组 - byte[] getData = readInputStream(inputStream); - - //文件保存位置 - File saveDir = new File(savePath); - if (!saveDir.exists()) { - saveDir.mkdir(); - } - File file = new File(saveDir + File.separator + fileName); - FileOutputStream fos = new FileOutputStream(file); - fos.write(getData); - if (fos != null) { - fos.close(); - } - if (inputStream != null) { - inputStream.close(); - } - long end = System.currentTimeMillis(); - log.info("info:" + url + " download success;用时:" + (end - start) + "ms"); - } catch (Exception ex) { - throw ex; - } finally { - // 断开连接,释放资源 - conn.disconnect(); - } - } - - /** - * 处理多级302等跳转 - * - * @param uc - * @return - * @throws Exception - */ - private HttpURLConnection reload(HttpURLConnection uc) throws Exception { - - HttpURLConnection huc = uc; - - if (huc.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP - || huc.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM) {// 302, 301 - String url = huc.getHeaderField("Location"); - url = url.replace("\\", "/"); - return reload((HttpURLConnection) new URL(url).openConnection()); - } - return uc; - } - - /** - * 覆盖java默认的证书验证 - */ - private final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { - @Override - public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException { - - } - - @Override - public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException { - - } - - @Override - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return new java.security.cert.X509Certificate[]{}; - } - }}; - - /** - * 信任所有 - * - * @param connection - * @return - */ - private SSLSocketFactory trustAllHosts(HttpsURLConnection connection) { - SSLSocketFactory oldFactory = connection.getSSLSocketFactory(); - try { - SSLContext sc = SSLContext.getInstance("TLS"); - sc.init(null, trustAllCerts, new java.security.SecureRandom()); - SSLSocketFactory newFactory = sc.getSocketFactory(); - connection.setSSLSocketFactory(newFactory); - } catch (Exception e) { - e.printStackTrace(); - } - return oldFactory; - } - - /** - * 从输入流中获取字节数组 - * - * @param inputStream - * @return - * @throws IOException - */ - public byte[] readInputStream(InputStream inputStream) throws IOException { - byte[] buffer = new byte[1024]; - int len = 0; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - while ((len = inputStream.read(buffer)) != -1) { - bos.write(buffer, 0, len); - } - bos.close(); - return bos.toByteArray(); - } - - private void valid(MultipartFile multipartFile) { if (multipartFile.isEmpty()) { throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "上传失败,请选择文件!"); diff --git a/collector-scheduling-management/src/main/resources/application.properties b/collector-scheduling-management/src/main/resources/application.properties index bde7d71..63a543c 100644 --- a/collector-scheduling-management/src/main/resources/application.properties +++ b/collector-scheduling-management/src/main/resources/application.properties @@ -3,7 +3,7 @@ api.base-package=com.docus.server spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://db.docus.cn:3306/docus-collector-scheduling?autoReconnect=true&allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai -spring.datasource.username=docus -spring.datasource.password=docus702 +spring.datasource.username=root +spring.datasource.password=root mybatis-plus.type-enums-package=com.docus.server.enums diff --git a/collector-scheduling-management/src/main/resources/bootstrap.yml b/collector-scheduling-management/src/main/resources/bootstrap.yml index 5de0b5b..b617803 100644 --- a/collector-scheduling-management/src/main/resources/bootstrap.yml +++ b/collector-scheduling-management/src/main/resources/bootstrap.yml @@ -13,8 +13,8 @@ spring: #公司病案的文件服务数据库 master: url: jdbc:log4jdbc:mysql://db.docus.cn:3306/docus-collector-scheduling?autoReconnect=true&allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai - username: docus - password: docus702 + username: root + password: root driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy type: com.alibaba.druid.pool.DruidDataSource servlet: @@ -23,7 +23,7 @@ spring: max-request-size: 200MB redis: host: redis.docus.cn - password: JSdocus@702 +# password: JSdocus@702 cloud: nacos: discovery: @@ -60,7 +60,7 @@ netty: all-idle-time-seconds: 0 file: - uploadFolder: D://docus/ + uploadFolder: /Users/linruifeng/workspace/ docus: redisKeyExpiration: true diff --git a/collector-terminal-management/src/main/java/com/docus/server/TerminalBootstrap.java b/collector-terminal-management/src/main/java/com/docus/server/TerminalBootstrap.java index 93f3750..1f27f2c 100644 --- a/collector-terminal-management/src/main/java/com/docus/server/TerminalBootstrap.java +++ b/collector-terminal-management/src/main/java/com/docus/server/TerminalBootstrap.java @@ -6,7 +6,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync -@EnableFeignClients(basePackages = {"com.docus.core.excel.feign", "com.docus.server.api.taskdistribute"}) +@EnableFeignClients(basePackages = {"com.docus.core.excel.feign", "com.docus.server.api"}) @SpringBootApplication(scanBasePackages = {"com.docus"}) public class TerminalBootstrap { public static void main(String[] args) { diff --git a/collector-terminal-management/src/main/java/com/docus/server/common/SchVirtualLogTask.java b/collector-terminal-management/src/main/java/com/docus/server/common/SchVirtualLogTask.java new file mode 100644 index 0000000..fccaf57 --- /dev/null +++ b/collector-terminal-management/src/main/java/com/docus/server/common/SchVirtualLogTask.java @@ -0,0 +1,52 @@ +package com.docus.server.common; + +import com.alibaba.fastjson.JSONObject; +import com.docus.server.api.scheduling.management.SchVirtualLogApi; +import com.docus.server.common.utils.SystemInfoUtils; +import com.docus.server.dto.scheduling.management.schvirtuallog.AddSchVirtualLogDTO; +import com.xxl.job.core.util.IpUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; + +@Component +@Slf4j +public class SchVirtualLogTask { + + @Resource + private SchVirtualLogApi schVirtualLogApi; + + //定时任务 + // 5 * * * * ? 在每分钟的5秒执行 + @Scheduled(cron = "${docus.vm-task-cron}") + public void runTask() { + try { + log.info("收集虚拟机信息定时任务: 开始执行"); + + JSONObject info = SystemInfoUtils.getInfo(); + + AddSchVirtualLogDTO addSchVirtualLogDTO = new AddSchVirtualLogDTO(); + addSchVirtualLogDTO.setCpuUtilization((String) ((JSONObject) info.get("cpuInfo")).get("cSys")); + addSchVirtualLogDTO.setMemoryTotal((String) ((JSONObject) info.get("memInfo")).get("total")); + addSchVirtualLogDTO.setMemoryAllowance((String) ((JSONObject) info.get("memInfo")).get("free")); + addSchVirtualLogDTO.setUplinkRate((String) ((JSONObject) info.get("networkInfo")).get("txPercent")); + addSchVirtualLogDTO.setDescendingRate((String) ((JSONObject) info.get("networkInfo")).get("rxPercent")); + addSchVirtualLogDTO.setIp(IpUtil.getIp()); + addSchVirtualLogDTO.setClientTime(new Date()); + addSchVirtualLogDTO.setDiskJson(info.get("sysFileInfo").toString()); + schVirtualLogApi.add(addSchVirtualLogDTO); + System.out.println(info); + + log.info("收集虚拟机信息定时任务: 执行完毕"); + } catch (Exception e) { + log.error("收集虚拟机信息定时任务执行出错", e); + } + + } + + +} + diff --git a/collector-terminal-management/src/main/resources/bootstrap.yml b/collector-terminal-management/src/main/resources/bootstrap.yml index 38ba207..a9036de 100644 --- a/collector-terminal-management/src/main/resources/bootstrap.yml +++ b/collector-terminal-management/src/main/resources/bootstrap.yml @@ -13,8 +13,8 @@ spring: #公司病案的文件服务数据库 master: url: jdbc:log4jdbc:mysql://db.docus.cn:3306/docus-collector-scheduling?autoReconnect=true&allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai - username: docus - password: docus702 + username: root + password: root driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy type: com.alibaba.druid.pool.DruidDataSource servlet: @@ -23,7 +23,7 @@ spring: max-request-size: 200MB redis: host: redis.docus.cn - password: JSdocus@702 + # password: JSdocus@702 cloud: nacos: discovery: @@ -67,7 +67,7 @@ netty: all-idle-time-seconds: 0 file: - uploadFolder: D://docus/ + uploadFolder: /Users/linruifeng/workspace/ docus: - redisKeyExpiration: true + vm-task-cron: 0/15 * * * * ? diff --git a/collector-terminal-management/src/test/java/com/docus/server/FileController.java b/collector-terminal-management/src/test/java/com/docus/server/FileController.java index 29f3512..23c085c 100644 --- a/collector-terminal-management/src/test/java/com/docus/server/FileController.java +++ b/collector-terminal-management/src/test/java/com/docus/server/FileController.java @@ -1,5 +1,6 @@ package com.docus.server; +import cn.hutool.core.util.ZipUtil; import com.docus.server.common.download.downLoader.HttpDownloader; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -16,10 +17,19 @@ public class FileController { @Test void test() throws Exception { HttpDownloader httpDownloader = new HttpDownloader(null); - String url = "http://192.168.16.110:9113/sch/file/download2?filePath=collector_packages/20230718/64cdaf49-f45e-4435-b6a3-3466165e7e3b/docus-collector-scheduling.tar.gz"; - String fileName = "test.tar.gz"; - String savePath = "h:\\test"; + String url = "http://127.0.0.1:9113/sch/file/download?filePath=collector_packages/20230727/78c002bc-7674-4dfe-9247-ae594d03ccdf/docus-collector-scheduling.tar.gz"; + String fileName = "collector.tar.gz"; + String savePath = "/Users/linruifeng/Desktop/collector_packages"; httpDownloader.downLoadFromUrl(url, fileName, savePath); } + public static void main(String[] args) throws Exception { + test1(); + } + + public static void test1() throws Exception { + ZipUtil.unzip("/Users/linruifeng/Desktop/collector_packages/collector.zip", "/Users/linruifeng/Desktop/collector_packages"); +// ZipUtil.unGzip(new GZIPInputStream(new FileInputStream(new File("/Users/linruifeng/Desktop/collector_packages/index.tar.gz")))); +// FileUtils.unTarGz("/Users/linruifeng/Desktop/collector_packages/", "/Users/linruifeng/Desktop/collector_packages/test"); + } } diff --git a/collector-terminal-management/src/test/java/com/docus/server/FileUtils.java b/collector-terminal-management/src/test/java/com/docus/server/FileUtils.java new file mode 100644 index 0000000..2010beb --- /dev/null +++ b/collector-terminal-management/src/test/java/com/docus/server/FileUtils.java @@ -0,0 +1,162 @@ +package com.docus.server; + +import org.apache.tools.tar.TarInputStream; + +import java.io.*; +import java.util.zip.GZIPInputStream; + +/** + * @program: JavaCode + * @ClassName FileUtils + * @description: + * @author: ltcz99 + * @create: 2023-04-16 + * @Version 1.0 + **/ +public class FileUtils { + + + /** + * 解压tar.gz文件到指定目录 + * + * @param sourceDir 源文件夹 + * @param destDir 解压后的目标文件夹 + */ + public static void unTarGz(String sourceDir, String destDir) throws Exception { + File outFile = new File(sourceDir); + File[] files = outFile.listFiles(); + try { + //创建输出目录 + createDirectory(destDir, null); + TarInputStream tarIn; + int index = 1; + for (File file : files) { + if (file.getName().contains("tar.gz")) { + tarIn = new TarInputStream(new GZIPInputStream( + new BufferedInputStream(new FileInputStream(file))), + 1024 * 2); + + String outFileName = destDir + "/" + file.getName(); + OutputStream out = new FileOutputStream(new File(outFileName)); + int length = 0; + byte[] b = new byte[2048]; + while ((length = tarIn.read(b)) != -1) { + out.write(b, 0, length); + } + out.close(); + tarIn.close(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * 解压gz到指定的文件夹下面 + * + * @param sourceDir + * @param destDir + */ + public static void unGzipFile(String sourceDir, String destDir) { + //创建输出目录 + createDirectory(destDir, null); + File sourceFile = new File(sourceDir); + File[] files = sourceFile.listFiles(); + try { + int index = 1; + for (File file : files) { + if (file.getName().contains("gz")) { + FileInputStream fin = new FileInputStream(file); + //建立gzip解压工作流 + GZIPInputStream gzin = new GZIPInputStream(fin); + //建立解压文件输出流 + File tmpFile = new File(destDir + "/" + index++ + ".log"); + FileOutputStream fout = new FileOutputStream(tmpFile); + int length; + byte[] buf = new byte[2048]; + while ((length = gzin.read(buf, 0, buf.length)) != -1) { + fout.write(buf, 0, length); + } + gzin.close(); + fout.close(); + fin.close(); + } + } + } catch (Exception ex) { + System.err.println(ex); + } + } + + /** + * 读取文件到指定的文件夹下面 + * + * @param sourceLogPath + * @param destLogPath + */ + public static void readFileToDestLogPath(String sourceLogPath, String destLogPath) { + File sourceFile = new File(sourceLogPath); + File[] files = sourceFile.listFiles(); + for (File file : files) { + String fileName = destLogPath + "/" + file.getName(); + File destFile = new File(fileName); + if (file.getName().contains("log") && !fileName.contains("gz")) { + try { + if (destFile.exists()) { + destFile.delete(); + } + String logFile = sourceFile + "/" + file.getName(); + FileInputStream fis = new FileInputStream(logFile); + FileOutputStream fos = new FileOutputStream(destFile); + BufferedInputStream bis = new BufferedInputStream(fis); + BufferedOutputStream bos = new BufferedOutputStream(fos); + int len = 0; + while ((len = bis.read()) != -1) { + bos.write(len); + } + bos.flush(); + // 关闭资源 + fis.close(); + bis.close(); + fos.close(); + bos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + } + + /** + * 创建目录 + * + * @param outputDir + * @param subDir + */ + public static void createDirectory(String outputDir, String subDir) { + File file = new File(outputDir); + //子目录不为空 + if (!(subDir == null || subDir.trim().equals(""))) { + file = new File(outputDir + "/" + subDir); + } + if (!file.exists()) { + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + file.mkdirs(); + } + } + + public static void main(String[] args) throws Exception { + String sourceDir = "/Users/ltcz99/Downloads/templog"; + String destDir = "/Users/ltcz99/Downloads/unzip/"; + //解压.gz文件到指定的文件件下面 + unGzipFile(sourceDir, destDir); + // 解压tar.gz文件到指定的文件夹下面 + unTarGz(sourceDir, destDir); + //读取特定的文件到指定的文件夹下面 + readFileToDestLogPath(sourceDir, destDir); + } +} diff --git a/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java b/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java index fb90bcd..d89c314 100644 --- a/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java +++ b/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java @@ -1,51 +1,36 @@ -package com.docus.server.api.scheduling.management; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiOperation; -import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.multipart.MultipartFile; - -import javax.servlet.http.HttpServletResponse; - - -/** - * 文件上传下载 API - * - * @author AutoGenerator - * @since 2023-07-15 - */ -@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口") -@FeignClient(value = "collector-scheduling-management", contextId = "collector-scheduling-management.FileApi") -@RequestMapping("/sch/file") -public interface FileApi { - - @ApiOperation("文件下载") - @GetMapping("/download0") - void downLoadFromUrl(@RequestParam(value = "urlStr") String urlStr, - @RequestParam(value = "fileName") String fileName, - @RequestParam(value = "savePath") String savePath) throws Exception; - - @ApiOperation("文件下载") - @GetMapping("/download1") - ResponseEntity downloadFile(@RequestParam(value = "filePath") String filePath) throws Exception; - - @ApiOperation("文件下载") - @GetMapping("/download2") - void downloadFile(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws Exception; - - @ApiOperation("文件上传") - @PostMapping("/upload") - @ApiImplicitParams({ - @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class) - }) - void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception; - -} +//package com.docus.server.api.scheduling.management; +// +//import io.swagger.annotations.Api; +//import io.swagger.annotations.ApiImplicitParam; +//import io.swagger.annotations.ApiImplicitParams; +//import io.swagger.annotations.ApiOperation; +//import org.springframework.cloud.openfeign.FeignClient; +//import org.springframework.web.bind.annotation.*; +//import org.springframework.web.multipart.MultipartFile; +// +//import javax.servlet.http.HttpServletResponse; +// +// +///** +// * 文件上传下载 API +// * +// * @author AutoGenerator +// * @since 2023-07-15 +// */ +//@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口") +//@FeignClient(value = "collector-scheduling-management", contextId = "collector-scheduling-management.FileApi") +//@RequestMapping("/sch/file") +//public interface FileApi { +// +// @ApiOperation("文件下载") +// @GetMapping("/download") +// void downloadFile(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws Exception; +// +// @ApiOperation("文件上传") +// @PostMapping("/upload") +// @ApiImplicitParams({ +// @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class) +// }) +// void uploadFile(@RequestPart MultipartFile[] files, String pathKey) throws Exception; +// +//} diff --git a/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/SchVirtualLogApi.java b/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/SchVirtualLogApi.java index 761956a..868a7fb 100644 --- a/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/SchVirtualLogApi.java +++ b/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/SchVirtualLogApi.java @@ -57,7 +57,7 @@ public interface SchVirtualLogApi { */ @ApiOperation("新增") @PostMapping("/add") - boolean add(@RequestBody AddSchVirtualLogDTO addSchVirtualLogDTO); + void add(@RequestBody AddSchVirtualLogDTO addSchVirtualLogDTO); /** * 编辑 @@ -67,7 +67,7 @@ public interface SchVirtualLogApi { */ @ApiOperation("编辑") @PutMapping("/edit") - boolean edit(@RequestBody EditSchVirtualLogDTO editSchVirtualLogDTO); + void edit(@RequestBody EditSchVirtualLogDTO editSchVirtualLogDTO); /** * 批量删除 diff --git a/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/AddSchVirtualLogDTO.java b/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/AddSchVirtualLogDTO.java index d6c4b4a..351cae9 100644 --- a/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/AddSchVirtualLogDTO.java +++ b/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/AddSchVirtualLogDTO.java @@ -22,10 +22,10 @@ public class AddSchVirtualLogDTO implements Serializable { private String cpuUtilization; @ApiModelProperty(value = "物理内存总量(单位:MB)") - private Double memoryTotal; + private String memoryTotal; @ApiModelProperty(value = "物理内存余量(单位:MB)") - private Double memoryAllowance; + private String memoryAllowance; @ApiModelProperty(value = "当前网络上行速率(KBPS)") private String uplinkRate; diff --git a/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/EditSchVirtualLogDTO.java b/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/EditSchVirtualLogDTO.java index 0bb2db4..88f1f6a 100644 --- a/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/EditSchVirtualLogDTO.java +++ b/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schvirtuallog/EditSchVirtualLogDTO.java @@ -3,23 +3,18 @@ package com.docus.server.dto.scheduling.management.schvirtuallog; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.Accessors; - -import com.docus.server.enums.*; import java.io.Serializable; import java.time.LocalDateTime; /** -* -* 虚拟机使用情况 EditDTO -* -* @author AutoGenerator -* @since 2023-07-15 -*/ + * 虚拟机使用情况 EditDTO + * + * @author AutoGenerator + * @since 2023-07-15 + */ @Data -@ApiModel(value="EditSchVirtualLogDTO对象", description="虚拟机使用情况") +@ApiModel(value = "EditSchVirtualLogDTO对象", description = "虚拟机使用情况") public class EditSchVirtualLogDTO implements Serializable { @ApiModelProperty(value = "终端id") @@ -29,10 +24,10 @@ public class EditSchVirtualLogDTO implements Serializable { private String cpuUtilization; @ApiModelProperty(value = "物理内存总量(单位:MB)") - private Double memoryTotal; + private String memoryTotal; @ApiModelProperty(value = "物理内存余量(单位:MB)") - private Double memoryAllowance; + private String memoryAllowance; @ApiModelProperty(value = "当前网络上行速率(KBPS)") private String uplinkRate; diff --git a/docus-client-interface/src/main/java/com/docus/server/entity/scheduling.management/SchVirtualLog.java b/docus-client-interface/src/main/java/com/docus/server/entity/scheduling.management/SchVirtualLog.java index cd208fa..27f4a72 100644 --- a/docus-client-interface/src/main/java/com/docus/server/entity/scheduling.management/SchVirtualLog.java +++ b/docus-client-interface/src/main/java/com/docus/server/entity/scheduling.management/SchVirtualLog.java @@ -35,11 +35,11 @@ public class SchVirtualLog implements Serializable { @ApiModelProperty(value = "物理内存总量(单位:MB)") @TableField("memory_total") - private Double memoryTotal; + private String memoryTotal; @ApiModelProperty(value = "物理内存余量(单位:MB)") @TableField("memory_allowance") - private Double memoryAllowance; + private String memoryAllowance; @ApiModelProperty(value = "当前网络上行速率(KBPS)") @TableField("uplink_rate") diff --git a/pom.xml b/pom.xml index 8a5f9b2..6081ef2 100644 --- a/pom.xml +++ b/pom.xml @@ -255,7 +255,11 @@ commons-httpclient 3.1 - + + org.apache.ant + ant + 1.10.5 +