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 org.springframework.web.multipart.MultipartFile; 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 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 ResponseEntity 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 viewFileList(String directoryPath) { List 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; } public FileDTO(boolean success, String name, String errorMessage) { this.success = success; this.name = name; this.errorMessage = errorMessage; } } }