From 71404114337f70f3d3134d2e272af6e5738f7bad Mon Sep 17 00:00:00 2001 From: pang.jiabao <pang_jiabao@163.com> Date: 星期六, 19 十月 2024 15:27:25 +0800 Subject: [PATCH] 理货出库拆垛完成修改 --- src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java | 142 +++------------------------------------------- 1 files changed, 11 insertions(+), 131 deletions(-) diff --git a/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java b/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java index 82efa3b..cad8419 100644 --- a/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java +++ b/src/main/java/com/zy/asrs/utils/FileSaveExampleUtil.java @@ -5,6 +5,7 @@ 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; @@ -18,59 +19,20 @@ 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); @@ -86,82 +48,6 @@ } } } - -// 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 { @@ -254,7 +140,6 @@ public FileDTO(String name, long size) { this.name = name; this.size = size; -// this.path = path; } public FileDTO(boolean success, String name, String errorMessage) { @@ -263,11 +148,6 @@ this.errorMessage = errorMessage; } - // getters and setters } -// public static void main(String[] args) { -// String directoryPath = "/path/to/directory"; -// deleteFilesInDirectory(directoryPath); -// } } -- Gitblit v1.9.1