mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-20 23:29:48 +08:00
update 上传阿里云oss
This commit is contained in:
@@ -4,6 +4,7 @@ import com.jsowell.common.config.JsowellConfig;
|
|||||||
import com.jsowell.common.constant.Constants;
|
import com.jsowell.common.constant.Constants;
|
||||||
import com.jsowell.common.core.domain.AjaxResult;
|
import com.jsowell.common.core.domain.AjaxResult;
|
||||||
import com.jsowell.common.util.StringUtils;
|
import com.jsowell.common.util.StringUtils;
|
||||||
|
import com.jsowell.common.util.file.AliyunOssUploadUtils;
|
||||||
import com.jsowell.common.util.file.FileUploadUtils;
|
import com.jsowell.common.util.file.FileUploadUtils;
|
||||||
import com.jsowell.common.util.file.FileUtils;
|
import com.jsowell.common.util.file.FileUtils;
|
||||||
import com.jsowell.framework.config.ServerConfig;
|
import com.jsowell.framework.config.ServerConfig;
|
||||||
@@ -11,6 +12,7 @@ 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.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -160,4 +162,21 @@ public class CommonController {
|
|||||||
return AjaxResult.error(e.getMessage());
|
return AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@PostMapping("/uploadOSS")
|
||||||
|
public AjaxResult uploadFileOSS(MultipartFile file) throws Exception {
|
||||||
|
System.out.println(file);
|
||||||
|
try {
|
||||||
|
String url = AliyunOssUploadUtils.uploadFile(file);
|
||||||
|
AjaxResult ajax = AjaxResult.success();
|
||||||
|
ajax.put("url", url);
|
||||||
|
ajax.put("fileName", FileUtils.getName(url));
|
||||||
|
ajax.put("newFileName", FileUtils.getName(url));
|
||||||
|
ajax.put("originalFilename", file.getOriginalFilename());
|
||||||
|
return ajax;
|
||||||
|
}catch (Exception e){
|
||||||
|
return AjaxResult.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,6 +104,20 @@ minio:
|
|||||||
secretKey: minioadmin
|
secretKey: minioadmin
|
||||||
bucketName: jsowell
|
bucketName: jsowell
|
||||||
|
|
||||||
|
aliyunoss:
|
||||||
|
# 地域节点
|
||||||
|
endpoint: oss-cn-shanghai.aliyuncs.com
|
||||||
|
# AccessKey
|
||||||
|
accessKeyId: LTAI5tBgCN4xuxQF1HV9rf7t
|
||||||
|
# AccessKey 秘钥
|
||||||
|
accessKeySecret: tsxMyujk6KY9h0e4Bx0D7ld16PBUyW
|
||||||
|
# bucket名称
|
||||||
|
bucketName: ydc-oss-dev
|
||||||
|
# bucket下文件夹的路径
|
||||||
|
filehost: img
|
||||||
|
# 访问域名
|
||||||
|
url: ydc-oss-dev.oss-cn-shanghai.aliyuncs.com
|
||||||
|
|
||||||
|
|
||||||
########################微信支付参数#######################################
|
########################微信支付参数#######################################
|
||||||
#微信商户号
|
#微信商户号
|
||||||
|
|||||||
@@ -164,6 +164,13 @@
|
|||||||
<version>8.2.1</version>
|
<version>8.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 阿里云OSS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.10.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package com.jsowell.common.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "aliyunoss")
|
||||||
|
public class AliyunOssConfig {
|
||||||
|
/**
|
||||||
|
* 地域节点
|
||||||
|
*/
|
||||||
|
private String endpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AccessKey
|
||||||
|
*/
|
||||||
|
private String accessKeyId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AccessKey秘钥
|
||||||
|
*/
|
||||||
|
private String accessKeySecret;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bucket名称
|
||||||
|
*/
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bucket下文件夹的路径
|
||||||
|
*/
|
||||||
|
private String filehost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 访问域名
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
public String getEndpoint() {
|
||||||
|
return endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEndpoint(String endpoint) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAccessKeyId() {
|
||||||
|
return accessKeyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccessKeyId(String accessKeyId) {
|
||||||
|
this.accessKeyId = accessKeyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAccessKeySecret() {
|
||||||
|
return accessKeySecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccessKeySecret(String accessKeySecret) {
|
||||||
|
this.accessKeySecret = accessKeySecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBucketName() {
|
||||||
|
return bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBucketName(String bucketName) {
|
||||||
|
this.bucketName = bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFilehost() {
|
||||||
|
return filehost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFilehost(String filehost) {
|
||||||
|
this.filehost = filehost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "AliyunOssConfig{" +
|
||||||
|
"endpoint='" + endpoint + '\'' +
|
||||||
|
", accessKeyId='" + accessKeyId + '\'' +
|
||||||
|
", accessKeySecret='" + accessKeySecret + '\'' +
|
||||||
|
", bucketName='" + bucketName + '\'' +
|
||||||
|
", filehost='" + filehost + '\'' +
|
||||||
|
", url='" + url + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package com.jsowell.common.util.file;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import com.jsowell.common.config.AliyunOssConfig;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AliyunOssUploadUtils {
|
||||||
|
|
||||||
|
private static AliyunOssConfig aliyunOssConfig;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用构造方法注入配置信息
|
||||||
|
*/
|
||||||
|
@Autowired
|
||||||
|
public AliyunOssUploadUtils(AliyunOssConfig aliyunOssConfig) {
|
||||||
|
AliyunOssUploadUtils.aliyunOssConfig = aliyunOssConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static String uploadFile(MultipartFile file) throws Exception {
|
||||||
|
|
||||||
|
// 生成 OSSClient
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());
|
||||||
|
// 原始文件名称
|
||||||
|
// String originalFilename = file.getOriginalFilename();
|
||||||
|
|
||||||
|
// 编码文件名
|
||||||
|
String filePathName = FileUploadUtils.extractFilename(file);
|
||||||
|
// 文件路径名称
|
||||||
|
filePathName = aliyunOssConfig.getFilehost() + "/" + filePathName;
|
||||||
|
try {
|
||||||
|
ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, file.getInputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (ossClient != null) {
|
||||||
|
ossClient.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return aliyunOssConfig.getUrl() + "/" + filePathName;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user