mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-13 03:39:55 +08:00
update 图片上传
This commit is contained in:
@@ -140,4 +140,24 @@ public class CommonController {
|
|||||||
log.error("下载文件失败", e);
|
log.error("下载文件失败", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义 Minio 服务器上传请求
|
||||||
|
*/
|
||||||
|
@PostMapping("/uploadMinio")
|
||||||
|
public AjaxResult uploadFileMinio(MultipartFile file) throws Exception {
|
||||||
|
try {
|
||||||
|
// 上传并返回新文件名称
|
||||||
|
String fileName = FileUploadUtils.uploadMinio(file);
|
||||||
|
AjaxResult ajax = AjaxResult.success();
|
||||||
|
ajax.put("url", fileName);
|
||||||
|
ajax.put("fileName", fileName);
|
||||||
|
ajax.put("newFileName", FileUtils.getName(fileName));
|
||||||
|
ajax.put("originalFilename", file.getOriginalFilename());
|
||||||
|
return ajax;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return AjaxResult.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,13 @@ logging:
|
|||||||
qrcodeurl:
|
qrcodeurl:
|
||||||
prefix: https://api.jsowellcloud.com
|
prefix: https://api.jsowellcloud.com
|
||||||
|
|
||||||
|
# Minio配置
|
||||||
|
minio:
|
||||||
|
url: http://localhost:9000
|
||||||
|
accessKey: minioadmin
|
||||||
|
secretKey: minioadmin
|
||||||
|
bucketName: jsowell
|
||||||
|
|
||||||
|
|
||||||
########################微信支付参数#######################################
|
########################微信支付参数#######################################
|
||||||
#微信商户号
|
#微信商户号
|
||||||
|
|||||||
@@ -157,6 +157,13 @@
|
|||||||
<artifactId>hutool-all</artifactId>
|
<artifactId>hutool-all</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Minio 文件存储 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.minio</groupId>
|
||||||
|
<artifactId>minio</artifactId>
|
||||||
|
<version>8.2.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package com.jsowell.common.config;
|
||||||
|
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minio 配置信息
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "minio")
|
||||||
|
public class MinioConfig
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 服务地址
|
||||||
|
*/
|
||||||
|
private static String url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private static String accessKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private static String secretKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储桶名称
|
||||||
|
*/
|
||||||
|
private static String bucketName;
|
||||||
|
|
||||||
|
public static String getUrl()
|
||||||
|
{
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url)
|
||||||
|
{
|
||||||
|
MinioConfig.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getAccessKey()
|
||||||
|
{
|
||||||
|
return accessKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccessKey(String accessKey)
|
||||||
|
{
|
||||||
|
MinioConfig.accessKey = accessKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSecretKey()
|
||||||
|
{
|
||||||
|
return secretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecretKey(String secretKey)
|
||||||
|
{
|
||||||
|
MinioConfig.secretKey = secretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getBucketName()
|
||||||
|
{
|
||||||
|
return bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBucketName(String bucketName)
|
||||||
|
{
|
||||||
|
MinioConfig.bucketName = bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MinioClient getMinioClient()
|
||||||
|
{
|
||||||
|
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.jsowell.common.util.file;
|
package com.jsowell.common.util.file;
|
||||||
|
|
||||||
import com.jsowell.common.config.JsowellConfig;
|
import com.jsowell.common.config.JsowellConfig;
|
||||||
|
import com.jsowell.common.config.MinioConfig;
|
||||||
import com.jsowell.common.constant.Constants;
|
import com.jsowell.common.constant.Constants;
|
||||||
import com.jsowell.common.exception.file.FileNameLengthLimitExceededException;
|
import com.jsowell.common.exception.file.FileNameLengthLimitExceededException;
|
||||||
import com.jsowell.common.exception.file.FileSizeLimitExceededException;
|
import com.jsowell.common.exception.file.FileSizeLimitExceededException;
|
||||||
@@ -19,7 +20,7 @@ import java.util.Objects;
|
|||||||
/**
|
/**
|
||||||
* 文件上传工具类
|
* 文件上传工具类
|
||||||
*
|
*
|
||||||
* @author jsowell
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class FileUploadUtils {
|
public class FileUploadUtils {
|
||||||
/**
|
/**
|
||||||
@@ -33,10 +34,15 @@ public class FileUploadUtils {
|
|||||||
public static final int DEFAULT_FILE_NAME_LENGTH = 100;
|
public static final int DEFAULT_FILE_NAME_LENGTH = 100;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认上传的地址
|
* 本地默认上传的地址
|
||||||
*/
|
*/
|
||||||
private static String defaultBaseDir = JsowellConfig.getProfile();
|
private static String defaultBaseDir = JsowellConfig.getProfile();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minio默认上传的地址
|
||||||
|
*/
|
||||||
|
private static String bucketName = MinioConfig.getBucketName();
|
||||||
|
|
||||||
public static void setDefaultBaseDir(String defaultBaseDir) {
|
public static void setDefaultBaseDir(String defaultBaseDir) {
|
||||||
FileUploadUtils.defaultBaseDir = defaultBaseDir;
|
FileUploadUtils.defaultBaseDir = defaultBaseDir;
|
||||||
}
|
}
|
||||||
@@ -45,6 +51,10 @@ public class FileUploadUtils {
|
|||||||
return defaultBaseDir;
|
return defaultBaseDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getBucketName() {
|
||||||
|
return bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以默认配置进行文件上传
|
* 以默认配置进行文件上传
|
||||||
*
|
*
|
||||||
@@ -105,6 +115,53 @@ public class FileUploadUtils {
|
|||||||
return getPathFileName(baseDir, fileName);
|
return getPathFileName(baseDir, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以默认BucketName配置上传到Minio服务器
|
||||||
|
*
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @return 文件名称
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static final String uploadMinio(MultipartFile file) throws IOException {
|
||||||
|
try {
|
||||||
|
return uploadMinino(getBucketName(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义bucketName配置上传到Minio服务器
|
||||||
|
*
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @return 文件名称
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static final String uploadMinio(MultipartFile file, String bucketName) throws IOException {
|
||||||
|
try {
|
||||||
|
return uploadMinino(bucketName, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String uploadMinino(String bucketName, MultipartFile file, String[] allowedExtension)
|
||||||
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||||
|
InvalidExtensionException {
|
||||||
|
int fileNamelength = file.getOriginalFilename().length();
|
||||||
|
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
|
||||||
|
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
||||||
|
}
|
||||||
|
assertAllowed(file, allowedExtension);
|
||||||
|
try {
|
||||||
|
String fileName = extractFilename(file);
|
||||||
|
String pathFileName = MinioUtil.uploadFile(bucketName, fileName, file);
|
||||||
|
return pathFileName;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编码文件名
|
* 编码文件名
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.jsowell.common.util.file;
|
||||||
|
|
||||||
|
import com.jsowell.common.util.ServletUtils;
|
||||||
|
import com.jsowell.common.util.spring.SpringUtils;
|
||||||
|
import io.minio.GetPresignedObjectUrlArgs;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import io.minio.PutObjectArgs;
|
||||||
|
import io.minio.http.Method;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minio 文件存储工具类
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class MinioUtil {
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param bucketName 桶名称
|
||||||
|
* @param fileName
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {
|
||||||
|
String url = "";
|
||||||
|
MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
|
||||||
|
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||||
|
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());
|
||||||
|
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());
|
||||||
|
url = url.substring(0, url.indexOf('?'));
|
||||||
|
return ServletUtils.urlDecode(url);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user