上传文件工具
This commit is contained in:
9
pom.xml
9
pom.xml
@@ -31,8 +31,13 @@
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
||||
|
||||
|
||||
<!-- oss -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.10.2</version>
|
||||
</dependency>
|
||||
<!-- poi -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.zmzm.finance.util;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
//当项目已启动,spring接口,spring加载之后,执行接口一个方法
|
||||
@Component
|
||||
public class ConstantPropertiesUtils implements InitializingBean {
|
||||
|
||||
//读取配置文件内容
|
||||
@Value("${aliyun.oss.file.endpoint}")
|
||||
private String endpoint;
|
||||
|
||||
@Value("${aliyun.oss.file.keyid}")
|
||||
private String keyId;
|
||||
|
||||
@Value("${aliyun.oss.file.keysecret}")
|
||||
private String keySecret;
|
||||
|
||||
@Value("${aliyun.oss.file.bucketname}")
|
||||
private String bucketName;
|
||||
|
||||
//定义公开静态常量
|
||||
public static String END_POIND;
|
||||
public static String ACCESS_KEY_ID;
|
||||
public static String ACCESS_KEY_SECRET;
|
||||
public static String BUCKET_NAME;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
END_POIND = endpoint;
|
||||
ACCESS_KEY_ID = keyId;
|
||||
ACCESS_KEY_SECRET = keySecret;
|
||||
BUCKET_NAME = bucketName;
|
||||
}
|
||||
}
|
||||
40
src/main/java/com/zmzm/finance/util/FileUtil.java
Normal file
40
src/main/java/com/zmzm/finance/util/FileUtil.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.zmzm.finance.util;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class FileUtil {
|
||||
|
||||
public String uploadFile(MultipartFile file){
|
||||
// 工具类获取值
|
||||
String endpoint = ConstantPropertiesUtils.END_POIND;
|
||||
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
|
||||
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
|
||||
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
|
||||
try {
|
||||
// 创建OSS实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
//获取上传文件输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
//获取文件名称
|
||||
String fileName = file.getOriginalFilename();
|
||||
//1 在文件名称里面添加随机唯一的值
|
||||
String datePath = "finance/platform";
|
||||
fileName = datePath+"/"+fileName;
|
||||
//把上传之后文件路径返回 需要把上传到阿里云oss路径手动拼接出来
|
||||
// https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
|
||||
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
|
||||
//调用oss方法实现上传
|
||||
ossClient.putObject(bucketName,fileName, inputStream);
|
||||
// 关闭OSSClient。
|
||||
ossClient.shutdown();
|
||||
return url;
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user