自动化立体仓库 - WMS系统
#
LSH
2023-12-07 85c7878f5a7792baecaee357cab58a75d207024b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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);
//    }
}