|
10 | 10 | import org.springframework.web.multipart.MultipartFile; |
11 | 11 |
|
12 | 12 | import java.io.File; |
| 13 | +import java.io.FileOutputStream; |
13 | 14 | import java.io.IOException; |
14 | 15 | import java.io.InputStream; |
15 | 16 | import java.nio.file.Files; |
16 | 17 | import java.nio.file.Path; |
17 | 18 | import java.nio.file.Paths; |
18 | 19 | import java.util.Properties; |
19 | 20 | import java.util.UUID; |
| 21 | +import org.apache.commons.io.IOUtils; |
20 | 22 |
|
21 | 23 | /** |
22 | 24 | * 文件上传工具类 |
@@ -51,17 +53,28 @@ public static String uploadFile(String baseDir, String relativePath, String file |
51 | 53 | assertAllowed(file); |
52 | 54 |
|
53 | 55 | // 创建目录 |
54 | | - String fullPath = baseDir + File.separator + relativePath; |
55 | | - Path uploadPath = Paths.get(fullPath); |
56 | | - if (!Files.exists(uploadPath)) { |
57 | | - Files.createDirectories(uploadPath); |
| 56 | + String fullPath = baseDir; |
| 57 | + if (!relativePath.isEmpty()) { |
| 58 | + fullPath = fullPath + File.separator + relativePath; |
| 59 | + } |
| 60 | + |
| 61 | + // 确保目录存在 |
| 62 | + File directory = new File(fullPath); |
| 63 | + if (!directory.exists()) { |
| 64 | + boolean created = directory.mkdirs(); |
| 65 | + if (!created) { |
| 66 | + throw new IOException("无法创建目录: " + fullPath); |
| 67 | + } |
58 | 68 | } |
59 | 69 |
|
60 | 70 | // 保存文件 |
61 | | - Path filePath = uploadPath.resolve(fileName); |
62 | | - file.transferTo(filePath.toFile()); |
| 71 | + File destFile = new File(directory, fileName); |
| 72 | + try (FileOutputStream fos = new FileOutputStream(destFile); |
| 73 | + InputStream inputStream = file.getInputStream()) { |
| 74 | + IOUtils.copy(inputStream, fos); |
| 75 | + } |
63 | 76 |
|
64 | | - return filePath.toString(); |
| 77 | + return destFile.getAbsolutePath(); |
65 | 78 | } |
66 | 79 |
|
67 | 80 | /** |
@@ -90,7 +103,7 @@ public static String smartUpload(String baseDir, String relativePath, String fil |
90 | 103 | // 检查配置文件中是否有腾讯云COS的配置 |
91 | 104 | Properties properties = new Properties(); |
92 | 105 | try (InputStream inputStream = FileUploadUtils.class.getClassLoader() |
93 | | - .getResourceAsStream("application.properties")) { |
| 106 | + .getResourceAsStream("application-prod.properties")) { |
94 | 107 | if (inputStream != null) { |
95 | 108 | properties.load(inputStream); |
96 | 109 |
|
@@ -160,6 +173,8 @@ private static String determineContentType(String fileName) { |
160 | 173 | return "audio/mpeg"; |
161 | 174 | case "mp4": |
162 | 175 | return "video/mp4"; |
| 176 | + case "wav": |
| 177 | + return "audio/wav"; |
163 | 178 | default: |
164 | 179 | return "application/octet-stream"; |
165 | 180 | } |
|
0 commit comments