|
|
|
@@ -7,6 +7,8 @@ import io.minio.PutObjectArgs; |
|
|
|
import io.minio.RemoveObjectArgs; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
import org.apache.commons.io.IOUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Primary; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
@@ -27,6 +29,7 @@ import java.net.URLEncoder; |
|
|
|
@Service |
|
|
|
public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(MinioSysFileServiceImpl.class); |
|
|
|
@Autowired |
|
|
|
private MinioConfig minioConfig; |
|
|
|
|
|
|
|
@@ -44,7 +47,7 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
String fileName = FileUploadUtils.extractFilename(file); |
|
|
|
|
|
|
|
//fileName = URLEncoder.encode(fileName, "UTF-8"); |
|
|
|
System.out.println("Minio filename:" + fileName); |
|
|
|
log.info("Minio filename:" + fileName); |
|
|
|
PutObjectArgs args = PutObjectArgs.builder() |
|
|
|
.bucket(minioConfig.getBucketName()) |
|
|
|
.object(fileName) |
|
|
|
@@ -52,7 +55,7 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
.contentType(file.getContentType()) |
|
|
|
.build(); |
|
|
|
client.putObject(args); |
|
|
|
System.out.println("Minio 文件上传成功"); |
|
|
|
log.info("Minio 文件上传成功"); |
|
|
|
// 获取fileName的最后一个'/'的位置的字符串,并用URLEncoder进行UTF-8编码后,再拼接到fileName中 |
|
|
|
String splitStr = fileName.substring(fileName.lastIndexOf("/") + 1); |
|
|
|
splitStr = URLEncoder.encode(splitStr, "UTF-8"); |
|
|
|
@@ -69,8 +72,8 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
public String uploadFile(String fileName, File file) throws Exception { |
|
|
|
fileName = FileUploadUtils.extractFilename(fileName); |
|
|
|
InputStream inputStream = FileUtils.openInputStream(file); |
|
|
|
System.out.println("file original filename = " + fileName); |
|
|
|
System.out.println("file size = " + file.length()); |
|
|
|
log.info("file original filename = " + fileName); |
|
|
|
log.info("file size = " + file.length()); |
|
|
|
|
|
|
|
// 使用IOUtils.toByteArray()方法确保正确地读取输入流中的数据 |
|
|
|
byte[] fileBytes = IOUtils.toByteArray(inputStream); |
|
|
|
@@ -78,7 +81,7 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
// 上传文件到MinIO |
|
|
|
String minioFileName = uploadFileToMinIO(fileName, fileBytes); |
|
|
|
|
|
|
|
System.out.println("MinIO 文件上传成功,Minio 文件名:" + minioFileName); |
|
|
|
log.info("MinIO 文件上传成功,Minio 文件名:" + minioFileName); |
|
|
|
|
|
|
|
// 获取fileName的最后一个'/'的位置的字符串,并用URLEncoder进行UTF-8编码后,再拼接到fileName中 |
|
|
|
String splitStr = fileName.substring(fileName.lastIndexOf("/") + 1); |
|
|
|
@@ -92,8 +95,8 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
public String uploadTempFile(String fileName, File file) throws Exception { |
|
|
|
fileName = FileUploadUtils.extractFilename(fileName); |
|
|
|
InputStream inputStream = FileUtils.openInputStream(file); |
|
|
|
System.out.println("file original filename = " + fileName); |
|
|
|
System.out.println("file size = " + file.length()); |
|
|
|
log.info("file original filename = " + fileName); |
|
|
|
log.info("file size = " + file.length()); |
|
|
|
|
|
|
|
// 使用IOUtils.toByteArray()方法确保正确地读取输入流中的数据 |
|
|
|
byte[] fileBytes = IOUtils.toByteArray(inputStream); |
|
|
|
@@ -101,7 +104,7 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
// 上传文件到MinIO |
|
|
|
String minioFileName = uploadFileToMinIO(fileName, fileBytes); |
|
|
|
|
|
|
|
System.out.println("MinIO Temp文件上传成功,Minio 文件名:" + minioFileName); |
|
|
|
log.info("MinIO Temp文件上传成功,Minio 文件名:" + minioFileName); |
|
|
|
|
|
|
|
// 获取fileName的最后一个'/'的位置的字符串,并用URLEncoder进行UTF-8编码后,再拼接到fileName中 |
|
|
|
String splitStr = fileName.substring(fileName.lastIndexOf("/") + 1); |
|
|
|
@@ -137,7 +140,7 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
.build() |
|
|
|
); |
|
|
|
|
|
|
|
System.out.println("文件上传到MinIO成功:" + fileName); |
|
|
|
log.info("文件上传到MinIO成功:" + fileName); |
|
|
|
String splitStr = fileName.substring(fileName.lastIndexOf("/") + 1); |
|
|
|
splitStr = URLEncoder.encode(splitStr, "UTF-8"); |
|
|
|
fileName = fileName.substring(0, fileName.lastIndexOf("/") + 1) + splitStr; |
|
|
|
@@ -147,12 +150,14 @@ public class MinioSysFileServiceImpl implements ISysFileService { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 文件删除接口 |
|
|
|
* 文件删除接口,通过minio的文件url进行删除 |
|
|
|
* |
|
|
|
* @param fileName 文件名 |
|
|
|
* @param url 文件url |
|
|
|
* @return 结果 |
|
|
|
*/ |
|
|
|
public Boolean deleteFile(String fileName) throws Exception { |
|
|
|
public Boolean deleteFile(String url) throws Exception { |
|
|
|
String bucketName = minioConfig.getBucketName(); |
|
|
|
String fileName = url.substring(url.indexOf(bucketName) + bucketName.length() + 1); |
|
|
|
client.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName).build()); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|