refactor(music-houduan): 重构文件上传功能并优化配置
- 更新 sa-token 版本至 1.43.0 - 重构文件上传功能,支持相对路径和绝对路径- 优化文件删除逻辑,处理多种路径情况 - 更新应用配置,使用相对路径 -调整 Redis 配置结构- 移除冗余代码,提高代码可读性
This commit is contained in:
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
<sa-token.version>1.38.0</sa-token.version>
|
<sa-token.version>1.43.0</sa-token.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.test.musichouduan.service.impl;
|
package com.test.musichouduan.service.impl;
|
||||||
|
|
||||||
|
import com.test.musichouduan.dto.Result;
|
||||||
import com.test.musichouduan.exception.BusinessException;
|
import com.test.musichouduan.exception.BusinessException;
|
||||||
import com.test.musichouduan.service.FileService;
|
import com.test.musichouduan.service.FileService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -106,17 +107,47 @@ public class FileServiceImpl implements FileService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从URL中提取文件路径
|
|
||||||
String filePath = url.replace(accessPath, "");
|
|
||||||
Path path = Paths.get(uploadPath, filePath);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 如果是完整URL,则只提取路径部分
|
||||||
|
String filePath = url;
|
||||||
|
if (url.startsWith("http")) {
|
||||||
|
// 找到访问路径在URL中的位置
|
||||||
|
int accessPathIndex = url.indexOf(accessPath);
|
||||||
|
if (accessPathIndex != -1) {
|
||||||
|
filePath = url.substring(accessPathIndex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果不是完整URL,确保使用正确的访问路径
|
||||||
|
filePath = url.replace(accessPath, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保filePath以accessPath开头
|
||||||
|
if (!filePath.startsWith(accessPath)) {
|
||||||
|
filePath = accessPath + filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从URL中提取文件路径部分(去掉访问路径前缀)
|
||||||
|
String fileRelativePath = filePath.replace(accessPath, "");
|
||||||
|
Path path = Paths.get(uploadPath, fileRelativePath);
|
||||||
|
|
||||||
// 删除文件
|
// 删除文件
|
||||||
return Files.deleteIfExists(path);
|
return Files.deleteIfExists(path);
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
logger.error("删除文件失败", e);
|
logger.error("删除文件失败: " + url, e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// // 从URL中提取文件路径
|
||||||
|
// String filePath = url.replace(accessPath, "");
|
||||||
|
// Path path = Paths.get(uploadPath, filePath);
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// // 删除文件
|
||||||
|
// return Files.deleteIfExists(path);
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// logger.error("删除文件失败", e);
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -132,17 +163,30 @@ public class FileServiceImpl implements FileService {
|
|||||||
String extension = getFileExtension(originalFilename);
|
String extension = getFileExtension(originalFilename);
|
||||||
String fileName = UUID.randomUUID().toString() + "." + extension;
|
String fileName = UUID.randomUUID().toString() + "." + extension;
|
||||||
|
|
||||||
// 创建目录
|
// // 创建目录
|
||||||
File dir = new File(uploadPath + directory);
|
// File dir = new File(uploadPath + directory);
|
||||||
if (!dir.exists()) {
|
// if (!dir.exists()) {
|
||||||
dir.mkdirs();
|
// dir.mkdirs();
|
||||||
|
// }
|
||||||
|
|
||||||
|
File root = new File(uploadPath); // /uploads
|
||||||
|
File bizDir = new File(root, directory); // /uploads/bizType
|
||||||
|
if (!bizDir.exists()) {
|
||||||
|
boolean ok = bizDir.mkdirs(); // 递归创建
|
||||||
|
if (!ok) {
|
||||||
|
Result.error(500, "无法创建目录: " + bizDir.getAbsolutePath());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File targetFile = new File(bizDir, fileName);
|
||||||
|
|
||||||
|
|
||||||
// 保存文件
|
// 保存文件
|
||||||
try {
|
try {
|
||||||
Path path = Paths.get(uploadPath, directory, fileName);
|
file.transferTo(targetFile);
|
||||||
Files.write(path, file.getBytes());
|
// Path path = Paths.get(uploadPath, directory, fileName);
|
||||||
return "http://localhost:8085" + accessPath + directory + "/" + fileName;
|
// Files.write(path, file.getBytes());
|
||||||
|
return accessPath+directory + "/" + fileName;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("上传文件失败", e);
|
logger.error("上传文件失败", e);
|
||||||
throw new BusinessException("上传文件失败");
|
throw new BusinessException("上传文件失败");
|
||||||
|
|||||||
@@ -1,25 +1,26 @@
|
|||||||
spring.application.name=music-houduan
|
#spring.application.name=music-houduan
|
||||||
server.port=8085
|
#server.port=8085
|
||||||
|
#
|
||||||
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/music_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/music_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
||||||
spring.datasource.username=root
|
#spring.datasource.username=root
|
||||||
spring.datasource.password=123456
|
#spring.datasource.password=123456
|
||||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
#
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
#spring.jpa.hibernate.ddl-auto=update
|
||||||
spring.jpa.show-sql=true
|
#spring.jpa.show-sql=true
|
||||||
|
#
|
||||||
spring.data.redis.host=127.0.0.1
|
#spring.data.redis.host=127.0.0.1
|
||||||
spring.data.redis.port=6379
|
#spring.data.redis.port=6379
|
||||||
spring.data.redis.password=123456
|
#spring.data.redis.password=123456
|
||||||
|
#
|
||||||
sa-token.token-name=satoken
|
#sa-token.token-name=satoken
|
||||||
sa-token.timeout=2592000
|
#sa-token.timeout=2592000
|
||||||
sa-token.is-concurrent=true
|
#sa-token.is-concurrent=true
|
||||||
sa-token.is-share=true
|
#sa-token.is-share=true
|
||||||
sa-token.token-style=uuid
|
#sa-token.token-style=uuid
|
||||||
|
#
|
||||||
# 文件上传配置
|
## 文件上传配置
|
||||||
file.upload-path=C:/it/music-houduan/upload/
|
#file.upload-path=./upload/
|
||||||
file.access-path=/upload/
|
#project.root-path=.
|
||||||
file.allowed-types=jpg,jpeg,png,gif,mp3,wav,lrc,txt
|
#file.access-path=/upload/
|
||||||
|
#file.allowed-types=jpg,jpeg,png,gif,mp3,wav,lrc,txt
|
||||||
@@ -23,28 +23,27 @@ spring:
|
|||||||
connection-timeout: 30000
|
connection-timeout: 30000
|
||||||
|
|
||||||
# JPA配置
|
# JPA配置
|
||||||
jpa:
|
# jpa:
|
||||||
database-platform: org.hibernate.dialect.MySQL8Dialect
|
# database-platform: org.hibernate.dialect.MySQL8Dialect
|
||||||
hibernate:
|
# hibernate:
|
||||||
ddl-auto: update
|
# ddl-auto: update
|
||||||
show-sql: false
|
# show-sql: false
|
||||||
properties:
|
# properties:
|
||||||
hibernate:
|
# hibernate:
|
||||||
format_sql: false
|
# format_sql: false
|
||||||
|
|
||||||
# Redis配置
|
# Redis配置
|
||||||
redis:
|
data:
|
||||||
host: 127.0.0.1
|
redis:
|
||||||
port: 6379
|
host: 127.0.0.1
|
||||||
password: 123456
|
password: 123456
|
||||||
database: 0
|
timeout: 10000
|
||||||
timeout: 10000
|
lettuce:
|
||||||
lettuce:
|
pool:
|
||||||
pool:
|
max-active: 8
|
||||||
max-active: 8
|
max-wait: -1
|
||||||
max-wait: -1
|
max-idle: 8
|
||||||
max-idle: 8
|
min-idle: 0
|
||||||
min-idle: 0
|
|
||||||
|
|
||||||
# 文件上传配置
|
# 文件上传配置
|
||||||
servlet:
|
servlet:
|
||||||
@@ -60,6 +59,7 @@ spring:
|
|||||||
serialization:
|
serialization:
|
||||||
write-dates-as-timestamps: false
|
write-dates-as-timestamps: false
|
||||||
|
|
||||||
|
|
||||||
# Sa-Token配置
|
# Sa-Token配置
|
||||||
sa-token:
|
sa-token:
|
||||||
# token名称 (同时也是cookie名称)
|
# token名称 (同时也是cookie名称)
|
||||||
@@ -88,7 +88,8 @@ sa-token:
|
|||||||
# 文件上传配置
|
# 文件上传配置
|
||||||
file:
|
file:
|
||||||
# 上传路径
|
# 上传路径
|
||||||
upload-path: upload/
|
# upload-path: C:/it/music-houduan/
|
||||||
|
upload-path: ${user.dir}/music-houduan/upload
|
||||||
# 访问路径
|
# 访问路径
|
||||||
access-path: /upload/
|
access-path: /upload/
|
||||||
# 允许的文件类型
|
# 允许的文件类型
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 978 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 61 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 111 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 6.9 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 61 KiB |
Binary file not shown.
@@ -60,6 +60,7 @@ const fetchSingerList = async () => {
|
|||||||
|
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
singerList.value = res.data.list
|
singerList.value = res.data.list
|
||||||
|
console.log(66666,singerList.value)
|
||||||
pagination.value.total = res.data.total
|
pagination.value.total = res.data.total
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error(res.message || '获取歌手列表失败')
|
ElMessage.error(res.message || '获取歌手列表失败')
|
||||||
|
|||||||
Reference in New Issue
Block a user