中扬CRM客户关系管理系统
#
LSH
2023-12-01 09ee5added9d59e90310a2586e846137ea597b19
src/main/java/com/zy/crm/common/utils/FileSaveExampleUtil.java
@@ -1,14 +1,25 @@
package com.zy.crm.common.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import com.zy.crm.manager.entity.PlanUrl;
import org.springframework.core.io.Resource;
import java.net.URLEncoder;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import lombok.Data;
public class FileSaveExampleUtil {
@@ -52,19 +63,19 @@
//        }
//    }
    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 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);
@@ -78,6 +89,128 @@
                    }
                }
            }
        }
    }
//    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;
        }
    }
@@ -106,6 +239,9 @@
        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;
@@ -113,12 +249,32 @@
            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(PlanUrl planUrl) {
            this.name = planUrl.getName();
            this.size = planUrl.getFileSize();
            this.path = planUrl.getUrl();
            this.userName = planUrl.getUserName();
        }
        public FileDTO(boolean success, String name, String errorMessage) {
            this.success = success;
            this.name = name;
            this.errorMessage = errorMessage;
        }
        // getters and setters
    }