refactor(file): 优化文件上传和访问路径处理
- 修改 BannerServiceImpl 中图片 URL 的处理逻辑,确保使用相对路径 - 更新 FileServiceImpl 中文件上传方法,返回文件的绝对物理路径 - 在 MusicDTO 中为日期时间字段添加 JsonFormat 注解,统一日期格式
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.test.musichouduan.dto;
|
package com.test.musichouduan.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
@@ -99,6 +100,7 @@ public class MusicDTO {
|
|||||||
/**
|
/**
|
||||||
* 发行日期
|
* 发行日期
|
||||||
*/
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
private LocalDateTime releaseDate;
|
private LocalDateTime releaseDate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,11 +116,13 @@ public class MusicDTO {
|
|||||||
/**
|
/**
|
||||||
* 创建时间
|
* 创建时间
|
||||||
*/
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
private LocalDateTime createTime;
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 收藏时间
|
* 收藏时间
|
||||||
*/
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
private LocalDateTime collectTime;
|
private LocalDateTime collectTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -70,11 +71,12 @@ public class BannerServiceImpl implements BannerService {
|
|||||||
public BannerDTO addBanner(BannerDTO bannerDTO, MultipartFile image) {
|
public BannerDTO addBanner(BannerDTO bannerDTO, MultipartFile image) {
|
||||||
// 上传图片
|
// 上传图片
|
||||||
String imageUrl = fileService.uploadImage(image);
|
String imageUrl = fileService.uploadImage(image);
|
||||||
|
String Url = imageUrl.replace(System.getProperty("user.dir") + File.separator + "music-houduan", "").replace(File.separator, "/");
|
||||||
|
|
||||||
// 创建轮播图实体
|
// 创建轮播图实体
|
||||||
Banner banner = new Banner();
|
Banner banner = new Banner();
|
||||||
BeanUtils.copyProperties(bannerDTO, banner);
|
BeanUtils.copyProperties(bannerDTO, banner);
|
||||||
banner.setImageUrl(imageUrl); // imageUrl已经是 /upload/images/... 格式
|
banner.setImageUrl(Url); // imageUrl已经是 /upload/images/... 格式
|
||||||
banner.setStatus(banner.getStatus() != null ? banner.getStatus() : 1);
|
banner.setStatus(banner.getStatus() != null ? banner.getStatus() : 1);
|
||||||
|
|
||||||
// 保存轮播图
|
// 保存轮播图
|
||||||
|
|||||||
@@ -194,7 +194,8 @@ public class FileServiceImpl implements FileService {
|
|||||||
try {
|
try {
|
||||||
file.transferTo(targetFile);
|
file.transferTo(targetFile);
|
||||||
// 返回文件的相对访问URL,并确保使用正斜杠
|
// 返回文件的相对访问URL,并确保使用正斜杠
|
||||||
return accessPath + directory + "/" + fileName;
|
// 返回文件的绝对物理路径
|
||||||
|
return targetFile.getAbsolutePath();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("上传文件失败", e);
|
logger.error("上传文件失败", e);
|
||||||
throw new BusinessException("上传文件失败");
|
throw new BusinessException("上传文件失败");
|
||||||
|
|||||||
Reference in New Issue
Block a user