From 30114e1f4986da9e3ca0bfd44ded4cb75da34eb1 Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Mon, 23 Aug 2021 16:27:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/gaea/business/util/FileUtil.java | 367 ++++++++++++++++++ report-ui/src/api/bigscreen.js | 18 + 2 files changed, 385 insertions(+) create mode 100644 report-core/src/main/java/com/anjiplus/template/gaea/business/util/FileUtil.java diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/util/FileUtil.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/util/FileUtil.java new file mode 100644 index 00000000..22161395 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/util/FileUtil.java @@ -0,0 +1,367 @@ +package com.anjiplus.template.gaea.business.util; + +import com.anji.plus.gaea.code.ResponseCode; +import com.anji.plus.gaea.exception.BusinessExceptionBuilder; +import lombok.extern.slf4j.Slf4j; +import sun.misc.BASE64Encoder; + +import java.io.*; +import java.net.URL; +import java.nio.channels.FileChannel; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Enumeration; +import java.util.zip.*; + +/** + * Created by raodeming on 2021/8/23. + */ +@Slf4j +public class FileUtil { + + //链接url下载图片 + public static void downloadPicture(String urlPath, String path) { + URL url = null; + try { + url = new URL(urlPath); + DataInputStream dataInputStream = new DataInputStream(url.openStream()); + + FileOutputStream fileOutputStream = new FileOutputStream(path); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + + byte[] buffer = new byte[1024]; + int length; + + while ((length = dataInputStream.read(buffer)) > 0) { + output.write(buffer, 0, length); + } + fileOutputStream.write(output.toByteArray()); + dataInputStream.close(); + fileOutputStream.close(); + log.info("链接下载图片:{},临时路径:{}", urlPath, path); + } catch (IOException e) { + log.error("根据链接下载失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + + + /** + * 复制文件 + * + * @param source + * @param dest + * @throws IOException + */ + public static void copyFileUsingFileChannels(File source, File dest) { + FileChannel inputChannel = null; + FileChannel outputChannel = null; + try { + if (!dest.getParentFile().exists()) { + dest.getParentFile().mkdirs(); + } + inputChannel = new FileInputStream(source).getChannel(); + outputChannel = new FileOutputStream(dest).getChannel(); + outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); + } catch (IOException e) { + log.error("复制文件失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } finally { + try { + inputChannel.close(); + outputChannel.close(); + } catch (IOException e) { + log.error("", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + } + + /** + * 复制文件 + * + * @param source + * @param dest + * @throws IOException + */ + public static void copyFileUsingFileChannels(String source, String dest) { + copyFileUsingFileChannels(new File(source), new File(dest)); + } + + + public static void WriteStringToFile(String filePath, String content) { + try { + FileWriter fw = new FileWriter(filePath); + BufferedWriter bw = new BufferedWriter(fw); + bw.write(content); + bw.close(); + fw.close(); + } catch (Exception e) { + log.error("写入文件失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + + + /** + * 根据文件读取文本文件内容 + * + * @param file + * @return + */ + public static String readFile(File file) { + BufferedReader reader = null; + StringBuilder sbf = new StringBuilder(); + try { + reader = new BufferedReader(new FileReader(file)); + String tempStr; + while ((tempStr = reader.readLine()) != null) { + sbf.append(tempStr); + } + reader.close(); + return sbf.toString(); + } catch (IOException e) { + log.error("读文件失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e1.getMessage()); + } + } + } + } + + /** + * 根据文件路径读取文本文件内容 + * + * @param filePath + * @return + */ + public static String readFile(String filePath) { + File file = new File(filePath); + return readFile(file); + } + + static final int BUFFER = 8192; + + /** + * 将文件夹压缩zip包 + * + * @param srcPath + * @param dstPath + * @throws IOException + */ + public static void compress(String srcPath, String dstPath) { + File srcFile = new File(srcPath); + File dstFile = new File(dstPath); + + FileOutputStream out = null; + ZipOutputStream zipOut = null; + try { + out = new FileOutputStream(dstFile); + CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32()); + zipOut = new ZipOutputStream(cos); + String baseDir = ""; + compress(srcFile, zipOut, baseDir); + } catch (IOException e) { + log.error("压缩文件夹失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } finally { + if (null != zipOut) { + try { + zipOut.close(); + } catch (IOException e) { + log.error("", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + out = null; + } + if (null != out) { + try { + out.close(); + } catch (IOException e) { + log.error("", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + } + } + + private static void compress(File file, ZipOutputStream zipOut, String baseDir) { + if (file.isDirectory()) { + compressDirectory(file, zipOut, baseDir); + } else { + compressFile(file, zipOut, baseDir); + } + } + + /** + * 压缩一个目录 + */ + private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) { + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) { + compress(files[i], zipOut, baseDir + dir.getName() + "/"); + } + } + + /** + * 压缩一个文件 + */ + private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) { + if (!file.exists()) { + return; + } + + BufferedInputStream bis = null; + try { + bis = new BufferedInputStream(new FileInputStream(file)); + ZipEntry entry = new ZipEntry(baseDir + file.getName()); + zipOut.putNextEntry(entry); + int count; + byte data[] = new byte[BUFFER]; + while ((count = bis.read(data, 0, BUFFER)) != -1) { + zipOut.write(data, 0, count); + } + + } catch (IOException e) { + log.error("压缩文件夹失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } finally { + if (null != bis) { + try { + bis.close(); + } catch (IOException e) { + log.error("", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + } + } + + + /** + * 解压zip + * + * @param zipFile + * @param dstPath + * @throws IOException + */ + public static void decompress(String zipFile, String dstPath) { + log.info("解压zip:{},临时目录:{}", zipFile, dstPath); + File pathFile = new File(dstPath); + if (!pathFile.exists()) { + pathFile.mkdirs(); + } + ZipFile zip = null; + try { + zip = new ZipFile(zipFile); + + for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) { + ZipEntry entry = (ZipEntry) entries.nextElement(); + String zipEntryName = entry.getName(); + InputStream in = null; + OutputStream out = null; + try { + in = zip.getInputStream(entry); + String outPath = (dstPath + "/" + zipEntryName).replaceAll("\\*", "/"); + ; + //判断路径是否存在,不存在则创建文件路径 + File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); + if (!file.exists()) { + file.mkdirs(); + } + //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 + if (new File(outPath).isDirectory()) { + continue; + } + + out = new FileOutputStream(outPath); + byte[] buf1 = new byte[1024]; + int len; + while ((len = in.read(buf1)) > 0) { + out.write(buf1, 0, len); + } + } catch (IOException e) { + log.error("解压失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } finally { + if (null != in) { + try { + in.close(); + } catch (IOException e) { + log.error("", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + + if (null != out) { + try { + out.close(); + } catch (IOException e) { + log.error("", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + } + } + zip.close(); + } catch (IOException e) { + log.error("解压失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + + /** + * 删除文件夹 + * @param path + */ + public static void delete(String path) { + + Path directory = Paths.get(path); + try { + Files.walkFileTree(directory, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { + Files.delete(file); // this will work because it's always a File + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); //this will work because Files in the directory are already deleted + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + log.error("删除文件失败", e); + throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage()); + } + } + + + public static void main(String[] args) throws Exception { +// String targetFolderPath = "D:\\aa"; +// String rawZipFilePath = "D:\\aa.zip"; +// String newZipFilePath = "D:\\aa.zip"; +// +// +// //将目标目录的文件压缩成Zip文件 +// FileUtil.compress(targetFolderPath, newZipFilePath); +// +// //将Zip文件解压缩到目标目录 +// FileUtil.decompress(rawZipFilePath, targetFolderPath); + +// FileUtil.downloadPicture("http://10.108.26.197:9095/file/download/fd20d563-00aa-45e2-b5db-aff951f814ec", "D:\\abc.png"); + + + +// delete("D:\\aa"); + + } + + +} diff --git a/report-ui/src/api/bigscreen.js b/report-ui/src/api/bigscreen.js index 79217efb..8155503f 100644 --- a/report-ui/src/api/bigscreen.js +++ b/report-ui/src/api/bigscreen.js @@ -45,3 +45,21 @@ export function getData(data) { data, }) } + +// 导出大屏 +export function exportDashboard(data) { + return request({ + url: 'reportDashboard/export/' + data, + method: 'get', + }) +} + + +// 导入大屏 +export function importDashboard(data) { + return request({ + url: 'reportDashboard/import', + method: 'post', + data, + }) +}