文件上传Oss
——前、后端结合(基于Springboot
)
兄弟篇:
所需依赖
<!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.9.1</version>
</dependency>
AliOssUtil
工具类
package com.soft1851.music.admin.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* @author wl_sun
* @description TODO
* @Data 2020/5/1
*/
@Slf4j
public class AliOssUtil {
public static String upload(MultipartFile sourceFile){
// 获取文件名
String fileName = sourceFile.getOriginalFilename();
//uuid生成主文件名
String prefix = UUID.randomUUID().toString();
assert fileName != null;
//源文件的扩展名
String suffix = fileName.substring(fileName.lastIndexOf("."));
//创建File类型的临时文件
File tempFile = null;
try {
tempFile = File.createTempFile(prefix,suffix);
//将MultipartFile转换成File
sourceFile.transferTo(tempFile);
} catch (IOException e) {
log.error(e.getMessage());
}
assert tempFile != null;
return upload(tempFile);
}
public static String upload(File file){
String endpoint = "https://oss-cn-beijing.aliyuncs.com";
String accessKeyId = "LT************z6jbo4";
String accessKeySecret = "6vVUW*********KwVZMjkv";
String bucketName = "bucket名";
String filePath = "存储路径,默认根目录";
String fileName = file.getName();
String newFileName = UUID.randomUUID().toString() + fileName.substring(fileName.indexOf("."));
//创建OSSClient实例
// OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
//上传文件到指定位置,并使用UUID更名
ossClient.putObject(bucketName,filePath + newFileName,file);
//拼接URL
String url = "https://bucket名.oss-cn-beijing.aliyuncs.com/" + filePath + newFileName;
ossClient.shutdown();
return url;
}
public static void main(String[] args) {
File file = new File("C:\\Users\\HP\\Desktop\\en\\Python库大全.pdf");
upload(file);
}
}
controller类接口编写
package com.soft1851.music.admin.controller;
import com.soft1851.music.admin.annotation.ControllerWebLog;
import com.soft1851.music.admin.common.ResponseResult;
import com.soft1851.music.admin.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @author wl_sun
* @description TODO
* @Data 2020/5/1
*/
@RestController
@RequestMapping("/api")
@Slf4j
public class UploadController {
@PostMapping("/uploadFile")
@ControllerWebLog
public ResponseResult uploadSingle(@RequestParam("file") MultipartFile sourceFile) {
String url = AliOssUtil.upload(sourceFile);
return ResponseResult.success(url);
}
}
前端调用接口实现(以上传图片为例)
<input type="file" @change="uploadImage($event)" id="fileBox" />
uploadImage() {
let formData = new FormData()
formData.append('file', event.target.files[0])
this.axios({
method: 'post',
url: this.GLOBAL.baseUrl + '/api/uploadFile',
headers: {
'Content-Type': 'multipart/formdata'
},
data: formData
}).then((res) => {
console.log(res.data.data)
})
},
谢谢,欢迎留言交流