文件上传接口修改

pull/440/head
CHENYUQING 2025-11-28 17:04:43 +08:00
parent 08111ba1c5
commit 2652943aea
4 changed files with 43 additions and 26 deletions

View File

@ -3,6 +3,7 @@ package com.ruoyi.file.controller;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -24,6 +25,7 @@ public class SysFileController
private static final Logger log = LoggerFactory.getLogger(SysFileController.class); private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
@Autowired @Autowired
@Qualifier("minioSysFileService")
private ISysFileService sysFileService; private ISysFileService sysFileService;
/** /**

View File

@ -14,7 +14,7 @@ import com.ruoyi.file.utils.FileUploadUtils;
* @author ruoyi * @author ruoyi
*/ */
@Primary @Primary
@Service @Service("localSysFileService")
public class LocalSysFileServiceImpl implements ISysFileService public class LocalSysFileServiceImpl implements ISysFileService
{ {
/** /**

View File

@ -17,7 +17,7 @@ import io.minio.RemoveObjectArgs;
* *
* @author ruoyi * @author ruoyi
*/ */
@Service @Service("minioSysFileService")
public class MinioSysFileServiceImpl implements ISysFileService public class MinioSysFileServiceImpl implements ISysFileService
{ {
@Autowired @Autowired
@ -36,28 +36,39 @@ public class MinioSysFileServiceImpl implements ISysFileService
@Override @Override
public String uploadFile(MultipartFile file) throws Exception public String uploadFile(MultipartFile file) throws Exception
{ {
InputStream inputStream = null; // InputStream inputStream = null;
try // try
{ // {
String fileName = FileUploadUtils.extractFilename(file); // String fileName = FileUploadUtils.extractFilename(file);
inputStream = file.getInputStream(); // inputStream = file.getInputStream();
PutObjectArgs args = PutObjectArgs.builder() // PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioConfig.getBucketName()) // .bucket(minioConfig.getBucketName())
.object(fileName) // .object(fileName)
.stream(inputStream, file.getSize(), -1) // .stream(inputStream, file.getSize(), -1)
.contentType(file.getContentType()) // .contentType(file.getContentType())
.build(); // .build();
client.putObject(args); // client.putObject(args);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; // return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
} // }
catch (Exception e) // catch (Exception e)
{ // {
throw new RuntimeException("Minio Failed to upload file", e); // throw new RuntimeException("Minio Failed to upload file", e);
} // }
finally // finally
{ // {
IoUtils.closeQuietly(inputStream); // IoUtils.closeQuietly(inputStream);
} // }
String fileName = FileUploadUtils.extractFilename(file);
InputStream inputStream = file.getInputStream();
PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.stream(inputStream, file.getSize(), -1)
.contentType(file.getContentType())
.build();
client.putObject(args);
IoUtils.closeQuietly(inputStream);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
} }
/** /**

View File

@ -14,12 +14,16 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 127.0.0.1:8848 server-addr: 43.136.76.125:8848
namespace: 1c8ac19f-8d7a-4008-a291-90b162b37fd5
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 127.0.0.1:8848 server-addr: 43.136.76.125:8848
namespace: 1c8ac19f-8d7a-4008-a291-90b162b37fd5
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置
shared-configs: shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
main:
web-application-type: reactive