springBoot接入阿里云oss的實(shí)現(xiàn)步驟
maven導(dǎo)入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- 阿里云OSS -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
</dependencies>
定義阿里云上傳結(jié)果實(shí)體
package com.example.demo.entity;
import lombok.Data;
/**
* 阿里云上傳結(jié)果集
*
* @author wushuai
* @create 2021-01-25
*/
@Data
public class AliyunOssResult {
/**
* code:200成功
* code: 400失敗
*/
private int code;
/**
* 上傳成功的返回url
*/
private String url;
/**
* 提示信息
*/
private String msg;
}
yml設(shè)置阿里云oss參數(shù)
aliyunOss: endpoint: "http://oss-cn-shanghai.aliyuncs.com" accessKeyId: "xxxxxxx" accessKeySecret: "xxxxxxx" bucketName: "xxxxxx" urlPrefix: "http://bucketName.oss-cn-shanghai.aliyuncs.com/"
yml設(shè)置上傳文件大小限制
spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB
工具類封裝
package com.example.demo.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.example.demo.entity.AliyunOssResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
@Component
public class AliyunOSSUtil {
@Value("${aliyunOss.endpoint}")
private String endpoint;
@Value("${aliyunOss.accessKeyId}")
private String accessKeyId;
@Value("${aliyunOss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyunOss.bucketName}")
private String bucketName;
@Value("${aliyunOss.urlPrefix}")
private String urlPrefix;
/**
* 上傳文件,以IO流方式
*
* @param inputStream 輸入流
* @param objectName 唯一objectName(在oss中的文件名字)
*/
public AliyunOssResult upload(InputStream inputStream, String objectName) {
AliyunOssResult aliyunOssResult = new AliyunOssResult();
try {
// 創(chuàng)建OSSClient實(shí)例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上傳內(nèi)容到指定的存儲空間(bucketName)并保存為指定的文件名稱(objectName)。
ossClient.putObject(bucketName, objectName, inputStream);
// 關(guān)閉OSSClient。
ossClient.shutdown();
aliyunOssResult.setCode(200);
aliyunOssResult.setUrl(urlPrefix+objectName);
aliyunOssResult.setMsg("上傳成功");
} catch (Exception e) {
e.printStackTrace();
aliyunOssResult.setCode(400);
aliyunOssResult.setMsg("上傳失敗");
}
return aliyunOssResult;
}
/**
* 刪除OSS中的單個文件
*
* @param objectName 唯一objectName(在oss中的文件名字)
*/
public void delete(String objectName) {
try {
// 創(chuàng)建OSSClient實(shí)例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 刪除文件。
ossClient.deleteObject(bucketName, objectName);
// 關(guān)閉OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 批量刪除OSS中的文件
*
* @param objectNames oss中文件名list
*/
public void delete(List<String> objectNames) {
try {
// 創(chuàng)建OSSClient實(shí)例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 批量刪除文件。
DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectNames));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
// 關(guān)閉OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取文件臨時url
*
* @param objectName oss中的文件名
* @param effectiveTime 有效時間(ms)
*/
public String getUrl(String objectName,long effectiveTime){
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 設(shè)置URL過期時間
Date expiration = new Date(new Date().getTime() + effectiveTime);
GeneratePresignedUrlRequest generatePresignedUrlRequest ;
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, objectName);
generatePresignedUrlRequest.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
}
controller接收調(diào)用
package com.example.demo.controller;
import com.example.demo.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private AliyunOSSUtil aliyunOSSUtil;
@RequestMapping(value = "/uploadFile")
public @ResponseBody
Object uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
String strPath) throws IOException {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String objectName = strPath+"/"+ UUID.randomUUID().toString()+"."+suffix;
return aliyunOSSUtil.upload(file.getInputStream(),objectName);
}
}
postman測試


到此這篇關(guān)于springBoot接入阿里云oss的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)springBoot接入阿里云oss內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring cloud如何修復(fù)zuul跨域配置異常的問題
最近的開發(fā)過程中,使用spring集成了spring-cloud-zuul,在配置zuul跨域的時候遇到了問題,下面這篇文章主要給大家介紹了關(guān)于spring cloud如何修復(fù)zuul跨域配置異常的問題,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
SpringCloud整合分布式服務(wù)跟蹤zipkin的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud整合分布式服務(wù)跟蹤zipkin的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringBoot啟動異常Exception in thread “main“ 
本文主要介紹了SpringBoot啟動異常Exception in thread “main“ java.lang.UnsupportedClassVersionError,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Springboot使用Maven占位符@替換不生效問題及解決
這篇文章主要介紹了Springboot使用Maven占位符@替換不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
java實(shí)現(xiàn)利用String類的簡單方法讀取xml文件中某個標(biāo)簽中的內(nèi)容
下面小編就為大家?guī)硪黄猨ava實(shí)現(xiàn)利用String類的簡單方法讀取xml文件中某個標(biāo)簽中的內(nèi)容。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

