为上传的文件分配目录动态的图片
动态图片存放在post/下的/yyyy/MM目录中
This commit is contained in:
@@ -11,6 +11,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -30,7 +32,6 @@ public class AliOssUtil {
|
|||||||
* @return 完整的文件访问URL
|
* @return 完整的文件访问URL
|
||||||
*/
|
*/
|
||||||
public String upload(byte[] bytes, String objectName) {
|
public String upload(byte[] bytes, String objectName) {
|
||||||
|
|
||||||
// 创建OSSClient实例。
|
// 创建OSSClient实例。
|
||||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -55,8 +57,11 @@ public class PostServiceImpl implements PostService {
|
|||||||
try {
|
try {
|
||||||
// 根据文件扩展名判断文件类型
|
// 根据文件扩展名判断文件类型
|
||||||
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
||||||
|
// 创建目录
|
||||||
|
String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
|
||||||
// 生成唯一文件名
|
// 生成唯一文件名
|
||||||
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
String newFileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||||
|
String fileName = "post/" + dir + "/" + newFileName;
|
||||||
// 获取文件字节数据
|
// 获取文件字节数据
|
||||||
byte[] fileBytes = file.getBytes();
|
byte[] fileBytes = file.getBytes();
|
||||||
// 根据文件类型处理
|
// 根据文件类型处理
|
||||||
|
|||||||
Reference in New Issue
Block a user