|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | public static void saveFile(MultipartFile file, String savePath) throws IOException { | 
|---|
|  |  |  | // 创建保存文件的目录(如果不存在) | 
|---|
|  |  |  | File directory = new File(savePath); | 
|---|
|  |  |  | if (!directory.exists()) { | 
|---|
|  |  |  | directory.mkdirs(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 将内容写入文件 | 
|---|
|  |  |  | try (OutputStream outputStream = new FileOutputStream(file)) { | 
|---|
|  |  |  | byte[] bytes = content.getBytes(); | 
|---|
|  |  |  | outputStream.write(bytes); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 保存文件 | 
|---|
|  |  |  | String fileName = file.getOriginalFilename(); | 
|---|
|  |  |  | String filePath = savePath + fileName; | 
|---|
|  |  |  | File dest = new File(filePath); | 
|---|
|  |  |  | file.transferTo(dest); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    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); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    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 { | 
|---|
|  |  |  | 
|---|
|  |  |  | public FileDTO(String name, long size) { | 
|---|
|  |  |  | this.name = name; | 
|---|
|  |  |  | this.size = size; | 
|---|
|  |  |  | //            this.path = path; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public FileDTO(boolean success, String name, String errorMessage) { | 
|---|
|  |  |  | 
|---|
|  |  |  | this.errorMessage = errorMessage; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // getters and setters | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    public static void main(String[] args) { | 
|---|
|  |  |  | //        String directoryPath = "/path/to/directory"; | 
|---|
|  |  |  | //        deleteFilesInDirectory(directoryPath); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | } | 
|---|