From 85c7878f5a7792baecaee357cab58a75d207024b Mon Sep 17 00:00:00 2001
From: LSH
Date: 星期四, 07 十二月 2023 14:12:58 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java |  273 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 273 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java b/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java
new file mode 100644
index 0000000..82efa3b
--- /dev/null
+++ b/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java
@@ -0,0 +1,273 @@
+package com.zy.asrs.utils;
+
+import lombok.Data;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.UrlResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.ResponseEntity;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+public class FileSaveExampleUtil {
+
+//    public static void main(String[] args) {
+//        String filePath = "D:/work/file/file.txt"; // 鎸囧畾淇濆瓨鏂囦欢鐨勮矾寰�
+//
+//        try {
+//            String content = "杩欐槸瑕佷繚瀛樼殑鏂囦欢鍐呭";
+//            saveToFile(filePath, content);
+//            System.out.println("鏂囦欢淇濆瓨鎴愬姛锛�");
+//        } catch (IOException e) {
+//            System.out.println("鏂囦欢淇濆瓨澶辫触锛�" + e.getMessage());
+//        }
+//    }
+
+    public static void saveToFile(String filePath, String content) throws IOException {
+        File file = new File(filePath);
+
+        // 鍒涘缓鐖剁洰褰曪紙濡傛灉涓嶅瓨鍦級
+        File parentDir = file.getParentFile();
+        if (!parentDir.exists()) {
+            parentDir.mkdirs();
+        }
+
+        // 灏嗗唴瀹瑰啓鍏ユ枃浠�
+        try (OutputStream outputStream = new FileOutputStream(file)) {
+            byte[] bytes = content.getBytes();
+            outputStream.write(bytes);
+        }
+    }
+
+//    public static void main(String[] args) {
+//        String savePath = "C:/path/to/save/"; // 鎸囧畾淇濆瓨鏂囦欢鐨勮矾寰�
+//        MultipartFile file = ...; // 鑾峰彇瑕佷繚瀛樼殑MultipartFile鏂囦欢
+//
+//        try {
+//            saveFile(file, savePath);
+//            System.out.println("鏂囦欢淇濆瓨鎴愬姛锛�");
+//        } catch (IOException e) {
+//            System.out.println("鏂囦欢淇濆瓨澶辫触锛�" + e.getMessage());
+//        }
+//    }
+
+//    public static void saveFile(MultipartFile file, String savePath) throws IOException {
+//        // 鍒涘缓淇濆瓨鏂囦欢鐨勭洰褰曪紙濡傛灉涓嶅瓨鍦級
+//        File directory = new File(savePath);
+//        if (!directory.exists()) {
+//            directory.mkdirs();
+//        }
+//
+//        // 淇濆瓨鏂囦欢
+//        String fileName = file.getOriginalFilename();
+//        String filePath = savePath + fileName;
+//        File dest = new File(filePath);
+//        file.transferTo(dest);
+//    }
+
+    public static void deleteFilesInDirectory(String directoryPath) {
+        File directory = new File(directoryPath);
+        if (directory.exists() && directory.isDirectory()) {
+            File[] files = directory.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    if (file.isFile()) {
+                        file.delete();
+                        System.out.println("Deleted file: " + file.getAbsolutePath());
+                    }
+                }
+            }
+        }
+    }
+
+//    public static FileDTO downloadFile(String directoryPath,HttpServletResponse response) {
+//        File file = new File(directoryPath);
+//        if (file.exists()) {
+//            try {
+//                response.setContentType("application/vnd.ms-excel");
+//                response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
+//
+//                FileInputStream fileInputStream = new FileInputStream(file);
+//                OutputStream outputStream = response.getOutputStream();
+//                byte[] buffer = new byte[1024];
+//                int length;
+//                while ((length = fileInputStream.read(buffer)) != -1) {
+//                    outputStream.write(buffer, 0, length);
+//                }
+//                outputStream.flush();
+//                outputStream.close();
+//                fileInputStream.close();
+//
+//                return new FileDTO(true, file.getName(), null);
+//            } catch (IOException e) {
+//                e.printStackTrace();
+//            }
+//        }
+//
+//        return new FileDTO(false, null, "鏂囦欢涓嶅瓨鍦�");
+//
+//
+//    }
+
+//    public static void downloadFile(String directoryPath,HttpServletResponse response) {
+//        try {
+////            File file = new File(directoryPath);
+////            InputStream inputStream = new FileInputStream(file);
+////            //杈撳嚭鏂囦欢
+////            InputStream fis = new BufferedInputStream(inputStream);
+////            byte[] buffer = new byte[fis.available()];
+////            fis.read(buffer);
+////            fis.close();
+////            response.reset();
+////
+////            //鑾峰彇鏂囦欢鐨勫悕瀛楀啀娴忚鍣ㄤ笅杞介〉闈�
+////            String name = file.getName();
+////            response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "iso-8859-1"));
+////            response.addHeader("Content-Length", "" + file.length());
+////            OutputStream out = new BufferedOutputStream(response.getOutputStream());
+////            response.setContentType("application/octet-stream");
+////            out.write(buffer);
+////            out.flush();
+////            out.close();
+//            File file = new File(directoryPath);
+//            if (!file.exists()) {
+//                // 鏂囦欢涓嶅瓨鍦紝鍙互鏍规嵁瀹為檯鎯呭喌杩涜澶勭悊
+//                return;
+//            }
+//
+//            response.reset();
+//            response.setContentType("application/octet-stream");
+//            response.setContentLength((int) file.length());
+//            response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
+//
+//            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
+//            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
+//            byte[] buffer = new byte[4096];
+//            int bytesRead;
+//            while ((bytesRead = bis.read(buffer)) != -1) {
+//                bos.write(buffer, 0, bytesRead);
+//            }
+//            bos.flush();
+//
+//            bis.close();
+//            bos.close();
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        }
+//    }
+
+    public static ResponseEntity<Resource> downloadFile(String filename, HttpServletResponse response) {
+        try {
+            File file = new File(filename);
+            //鑾峰彇鏂囦欢鐨勫悕瀛楀啀娴忚鍣ㄤ笅杞介〉闈�
+            String name = file.getName();
+
+            // 鏋勫缓鏂囦欢璺緞
+            Path filePath = Paths.get(filename);
+            Resource resource = new UrlResource(filePath.toUri());
+
+            if (resource.exists()) {
+                // 璁剧疆鍝嶅簲澶�
+                HttpHeaders headers = new HttpHeaders();
+//                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
+                response.setContentType("application/octet-stream");
+                headers.setContentDispositionFormData("attachment", filename);
+                response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "UTF-8"));
+//                headers.setContentDispositionFormData("attachment", URLEncoder.encode(name, "UTF-8"));
+//                response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
+
+                // 杩斿洖鏂囦欢鍝嶅簲
+                return ResponseEntity.ok()
+                        .headers(headers)
+                        .body(resource);
+
+//                // 璁剧疆鍝嶅簲澶�
+//                HttpHeaders headers = new HttpHeaders();
+//                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
+//                headers.setContentDispositionFormData("attachment", URLEncoder.encode(name, "UTF-8"));
+//                response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
+//
+//                // 杩斿洖鏂囦欢鍝嶅簲
+//                return ResponseEntity.ok()
+//                        .headers(headers)
+//                        .body(resource);
+            } else {
+                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                return null;
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+            return null;
+        }
+    }
+
+    public static List<FileDTO> viewFileList(String directoryPath) {
+        List<FileDTO> fileList = new ArrayList<>();
+
+        File directory = new File(directoryPath);
+        if (directory.exists() && directory.isDirectory()) {
+            File[] files = directory.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    if (file.isFile()) {
+                        FileDTO fileDTO = new FileDTO(file.getName(), file.length(), file.getAbsolutePath());
+//                        FileDTO fileDTO = new FileDTO(file.getName(), file.length());
+                        fileList.add(fileDTO);
+                    }
+                }
+            }
+        }
+
+        return fileList;
+    }
+
+    @Data
+    public static class FileDTO {
+        private String name;
+        private long size;
+        private String path;
+        private String userName;
+        private boolean success;
+        private String errorMessage;
+
+        public FileDTO(String name, long size, String path) {
+            this.name = name;
+            this.size = size;
+            this.path = path;
+        }
+
+        public FileDTO(String name, long size, String path,String userName) {
+            this.name = name;
+            this.size = size;
+            this.path = path;
+            this.userName = userName;
+        }
+
+        public FileDTO(String name, long size) {
+            this.name = name;
+            this.size = size;
+//            this.path = path;
+        }
+
+        public FileDTO(boolean success, String name, String errorMessage) {
+            this.success = success;
+            this.name = name;
+            this.errorMessage = errorMessage;
+        }
+
+        // getters and setters
+    }
+
+//    public static void main(String[] args) {
+//        String directoryPath = "/path/to/directory";
+//        deleteFilesInDirectory(directoryPath);
+//    }
+}

--
Gitblit v1.9.1