tlias管理系统--员工管理--文件上传-OSS集成实现
This commit is contained in:
@@ -45,6 +45,30 @@
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.2</version>
|
||||
</dependency>
|
||||
|
||||
<!--阿里云OSS依赖-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.17.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<!-- no more than 2.3.3-->
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>2.3.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.inmind.pojo.Result;
|
||||
import com.inmind.utils.AliOSSUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -15,7 +18,10 @@ import java.util.UUID;
|
||||
@Slf4j
|
||||
public class UploadController {
|
||||
|
||||
//本文件上传
|
||||
@Autowired
|
||||
private AliOSSUtils aliOSSUtils;
|
||||
|
||||
/* //本文件上传
|
||||
@PostMapping("/upload")
|
||||
public Result upload(String username, Integer age, MultipartFile image) throws IOException {
|
||||
log.info("文件上传:{},{},{}",username,age,image);
|
||||
@@ -32,5 +38,14 @@ public class UploadController {
|
||||
log.info("保存的文件名:{}",filePath);
|
||||
image.transferTo(new File(filePath));
|
||||
return Result.success();
|
||||
}*/
|
||||
|
||||
@PostMapping("/upload")
|
||||
public Result upload( MultipartFile image) throws IOException, ClientException {
|
||||
log.info("文件上传:{}",image);
|
||||
//获取到前端上传的文件,使用OSS上传阿里云服务器中,获取阿里云图片的URL
|
||||
String uploadUrl = aliOSSUtils.upload(image);
|
||||
log.info("文件上传OSS的URL:{}",uploadUrl);
|
||||
return Result.success(uploadUrl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.inmind.utils;
|
||||
|
||||
import com.aliyun.oss.ClientBuilderConfiguration;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
|
||||
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
|
||||
import com.aliyun.oss.common.comm.SignVersion;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.*;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 阿里云 OSS 工具类
|
||||
*/
|
||||
@Component
|
||||
public class AliOSSUtils {
|
||||
|
||||
/*@Value("${aliyun.oss.endpoint}")
|
||||
private String endpoint;
|
||||
@Value("${aliyun.oss.bucketName}")
|
||||
private String bucketName;
|
||||
@Value("${aliyun.oss.region}")
|
||||
private String region ;*/
|
||||
|
||||
private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||
private String bucketName = "inmind-test1";
|
||||
private String region = "cn-hangzhou";
|
||||
|
||||
/**
|
||||
* 实现上传图片到OSS
|
||||
*/
|
||||
public String upload(MultipartFile file) throws IOException, ClientException {
|
||||
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
// 获取上传的文件的输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
// 避免文件覆盖
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
//上传文件到 OSS
|
||||
// 创建OSSClient实例。
|
||||
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
||||
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
|
||||
OSS ossClient = OSSClientBuilder.create()
|
||||
.endpoint(endpoint)
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.clientConfiguration(clientBuilderConfiguration)
|
||||
.region(region)
|
||||
.build();
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||
//文件访问路径
|
||||
String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
|
||||
// 关闭ossClient
|
||||
ossClient.shutdown();
|
||||
return url;// 把上传到oss的路径返回
|
||||
}
|
||||
|
||||
}
|
||||
65
tlias-web-management/src/test/java/com/inmind/Demo.java
Normal file
65
tlias-web-management/src/test/java/com/inmind/Demo.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.inmind;
|
||||
|
||||
import com.aliyun.oss.*;
|
||||
import com.aliyun.oss.common.auth.*;
|
||||
import com.aliyun.oss.common.comm.SignVersion;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class Demo {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
// 填写Bucket名称,例如examplebucket。
|
||||
String bucketName = "inmind-test1";
|
||||
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
|
||||
String objectName = "exampledir/1434.jpg";
|
||||
// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
|
||||
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
// String filePath= "D:\\localpath\\examplefile.txt";
|
||||
String filePath= "C:\\Users\\xuxin\\Desktop\\头像图片\\3.jpg";
|
||||
// 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
|
||||
String region = "cn-hangzhou";
|
||||
|
||||
// 创建OSSClient实例。
|
||||
// 当OSSClient实例不再使用时,调用shutdown方法以释放资源。
|
||||
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
||||
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
|
||||
OSS ossClient = OSSClientBuilder.create()
|
||||
.endpoint(endpoint)
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.clientConfiguration(clientBuilderConfiguration)
|
||||
.region(region)
|
||||
.build();
|
||||
|
||||
try {
|
||||
//根据本地文件的路径获取IO流
|
||||
InputStream inputStream = new FileInputStream(filePath);
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
// 创建PutObject请求。
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||
} catch (OSSException oe) {
|
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
System.out.println("Error Message:" + oe.getErrorMessage());
|
||||
System.out.println("Error Code:" + oe.getErrorCode());
|
||||
System.out.println("Request ID:" + oe.getRequestId());
|
||||
System.out.println("Host ID:" + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
System.out.println("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,10 @@ class TliasWebManagementApplicationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnv() {
|
||||
String ossAccessKeyId = System.getenv("OSS_ACCESS_KEY_ID");
|
||||
System.out.println(ossAccessKeyId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user