tlias管理系统-文件上传接口实现&OSS工具类抽取实现功能实现
This commit is contained in:
@@ -46,6 +46,29 @@
|
||||
<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>
|
||||
<!--jdk9以上OSS所需依赖-->
|
||||
<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>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
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.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
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);
|
||||
//中.国.梦.txt
|
||||
String name = image.getOriginalFilename();
|
||||
int index = name.lastIndexOf(".");
|
||||
String extname = name.substring(index);
|
||||
String fileName = UUID.randomUUID().toString()+extname;//9bc01854-9b25-499c-9e40-0bf4055cbcbd.txt 9bc01854-9b25-499c-9e40-0bf4055cbcbd.jpg
|
||||
File file = new File("D:\\upload_images\\"+fileName);
|
||||
//将文件保存在本地服务的目录下D:/upload_images/1.txt
|
||||
image.transferTo(file);
|
||||
return Result.success();
|
||||
}*/
|
||||
|
||||
|
||||
@PostMapping("/upload")
|
||||
public Result upload(MultipartFile image) throws IOException, ClientException {
|
||||
//中.国.梦.txt
|
||||
String name = image.getOriginalFilename();
|
||||
log.info("文件上传:文件名:{}",name);
|
||||
//调用阿里云OSS功能,提交图片到云端
|
||||
String url = aliOSSUtils.upload(image);
|
||||
return Result.success(url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.inmind.utils;
|
||||
|
||||
import com.aliyun.oss.*;
|
||||
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.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class AliOSSUtils {
|
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||
private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||
private String bucketName = "inmind-test1";
|
||||
private String region = "cn-hangzhou";
|
||||
|
||||
public String upload(MultipartFile file) throws IOException, ClientException {
|
||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
//要使用uuid更换掉文件名,避免同名问题
|
||||
String filename = file.getOriginalFilename();//1.jpg 2.txt
|
||||
int index = filename.lastIndexOf(".");
|
||||
String extname = filename.substring(index);
|
||||
String objectName = UUID.randomUUID().toString()+extname;
|
||||
//地图片要修改为动态的文件上传的图片
|
||||
//获取上传文件的输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
||||
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
|
||||
OSS ossClient = OSSClientBuilder.create()
|
||||
.endpoint(endpoint)
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.clientConfiguration(clientBuilderConfiguration)
|
||||
.region(region)
|
||||
.build();
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||
ossClient.shutdown();
|
||||
//组装文件访问路径
|
||||
String url = endpoint.split("//")[0]+"//"+bucketName+"."+endpoint.split("//")[1]+"/"+objectName;//https://inmind-test1.oss-cn-hangzhou.aliyuncs.com/1.jpg
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,8 @@ spring.datasource.password=1234
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
#开启mybatis的驼峰命名自动映射开关,将a_time ---->aTime
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
||||
|
||||
#文件上传大小配置
|
||||
spring.servlet.multipart.max-file-size= 10MB
|
||||
spring.servlet.multipart.max-request-size=100MB
|
||||
17
tlias-web-management/src/main/resources/static/upload.html
Normal file
17
tlias-web-management/src/main/resources/static/upload.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>上传文件</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form action="/upload" method="post" enctype="multipart/form-data">
|
||||
姓名: <input type="text" name="username"><br>
|
||||
年龄: <input type="text" name="age"><br>
|
||||
头像: <input type="file" name="image"><br>
|
||||
<input type="submit" value="提交">
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
63
tlias-web-management/src/test/java/com/inmind/Demo.java
Normal file
63
tlias-web-management/src/test/java/com/inmind/Demo.java
Normal file
@@ -0,0 +1,63 @@
|
||||
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/444.jpg";
|
||||
// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
|
||||
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
String filePath= "D:\\upload_images\\4.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 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.inmind;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@SpringBootTest
|
||||
class TliasWebManagementApplicationTests {
|
||||
|
||||
@@ -10,4 +12,18 @@ class TliasWebManagementApplicationTests {
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUuid(){
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
System.out.println(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOss(){
|
||||
String ossAccessKeyId = System.getenv("OSS_ACCESS_KEY_ID");
|
||||
System.out.println(ossAccessKeyId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user