Skip to content

Commit 057ecfd

Browse files
committed
fix:修复文件上传功能
1 parent b38f1e1 commit 057ecfd

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
<artifactId>thumbnailator</artifactId>
147147
<version>[0.4, 0.5)</version>
148148
</dependency>
149+
<dependency>
150+
<groupId>commons-io</groupId>
151+
<artifactId>commons-io</artifactId>
152+
<version>2.11.0</version>
153+
</dependency>
149154
<!-- Edge TTS -->
150155
<dependency>
151156
<groupId>io.github.whitemagic2014</groupId>

src/main/java/com/xiaozhi/utils/FileUploadUtils.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import org.springframework.web.multipart.MultipartFile;
1111

1212
import java.io.File;
13+
import java.io.FileOutputStream;
1314
import java.io.IOException;
1415
import java.io.InputStream;
1516
import java.nio.file.Files;
1617
import java.nio.file.Path;
1718
import java.nio.file.Paths;
1819
import java.util.Properties;
1920
import java.util.UUID;
21+
import org.apache.commons.io.IOUtils;
2022

2123
/**
2224
* 文件上传工具类
@@ -51,17 +53,28 @@ public static String uploadFile(String baseDir, String relativePath, String file
5153
assertAllowed(file);
5254

5355
// 创建目录
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+
}
5868
}
5969

6070
// 保存文件
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+
}
6376

64-
return filePath.toString();
77+
return destFile.getAbsolutePath();
6578
}
6679

6780
/**
@@ -90,7 +103,7 @@ public static String smartUpload(String baseDir, String relativePath, String fil
90103
// 检查配置文件中是否有腾讯云COS的配置
91104
Properties properties = new Properties();
92105
try (InputStream inputStream = FileUploadUtils.class.getClassLoader()
93-
.getResourceAsStream("application.properties")) {
106+
.getResourceAsStream("application-prod.properties")) {
94107
if (inputStream != null) {
95108
properties.load(inputStream);
96109

@@ -160,6 +173,8 @@ private static String determineContentType(String fileName) {
160173
return "audio/mpeg";
161174
case "mp4":
162175
return "video/mp4";
176+
case "wav":
177+
return "audio/wav";
163178
default:
164179
return "application/octet-stream";
165180
}

src/main/resources/application-dev.properties

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,5 @@ logging.level.com.xiaozhi.websocket=DEBUG
1414
email.smtp.username=xxx
1515
email.smtp.password=xxx
1616

17-
# 开发环境腾讯云COS配置
18-
tencent.cos.secret-id=xxx
19-
tencent.cos.secret-key=xxx
20-
tencent.cos.region=xxx
21-
tencent.cos.bucket-name=xxx
22-
tencent.cos.path-prefix=uploads
17+
# 使用 Simple 缓存
18+
spring.cache.type=simple

src/main/resources/application.properties

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,5 @@ spring.session.cookie.path=/
4242
# 启用虚拟线程
4343
spring.threads.virtual.enabled=true
4444

45-
# 缓存
46-
spring.cache.type=simple
47-
4845
# 激活的配置文件(默认为开发环境)
4946
spring.profiles.active=dev

0 commit comments

Comments
 (0)