新建存儲空間
進入對象存儲菜單,點擊“新建存儲空間”,這里需要實名認證,上傳身份證正反面之類的,大概一個小時左右就認證成功了,效率真是棒棒噠~
這里的存儲空間名稱要記住,之后在代碼里面會用到。
二.代碼實現
1.在pom.xml添加七牛雲依賴
我的項目使用了maven管理jar包,所以只需直接添加相應依賴即可:
<!--七牛雲-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.6</version>
</dependency>
2.添加七牛雲圖片操作工具類
package gbq.ssm.utils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
/**
* 七牛雲工具類
* Zone.zone0:華東
* Zone.zone1:華北
* Zone.zone2:華南
* Zone.zoneNa0:北美
* ———http上傳,自動識別上傳區域——
* Zone.httpAutoZone
* ———https上傳,自動識別上傳區域—— //Zone.httpsAutoZone
* Configuration cfg = new Configuration(Zone.zone2());其中Configuration中的Zone.zone2()可以改為Zone.zone0()
*/
public class QiniuUtils {
public static String accessKey = "NmqvfVvXetScbrEOXgTE2Szg21qdSxp-sTRHaBSB";
public static String secretKey = "ft26PJWXnAfs6QksAnHQT85FqDDv2VUXP-AXjEt8";
//空間名稱
public static String bucket = "picture-server01";
//外鏈域名
public static String domain = "http://qcsorc5ip.bkt.clouddn.com/";
public static void upload2Qiniu(String filePath,String fileName){
//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone2());
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(filePath, fileName, upToken);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
} catch (QiniuException ex) {
Response r = ex.response;
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}
//上傳文件
public static String upload2Qiniu(byte[] bytes, String fileName){
//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone2());
//...其他參數參考類注釋
UploadManager uploadManager = new UploadManager(cfg);
//默認不指定key的情況下,以文件內容的hash值作為文件名
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(bytes, key, upToken);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
//返回文件完整路徑
return domain+putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
return "";
} catch (QiniuException ex2) {
//ignore
}
}
return "";
}
//刪除文件
public static void deleteFileFromQiniu(String fileName){
//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone2());
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(bucket, key);
} catch (QiniuException ex) {
//如果遇到異常,說明刪除失敗
System.err.println(ex.code());
System.err.println(ex.response.toString());
}
}
}
此工具類中注意事項:
(1).獲取需要操作的賬號的AK和SK
public static String accessKey = "NmqvfVvXetScbrEOXgTE2Szg21qdSxp-sTRHaBSB";
public static String secretKey = "ft26PJWXnAfs6QksAnHQT85FqDDv2VUXP-AXjEt8";
進入個人中心-密鑰管理
(2).獲取要上傳的空間
private static final String bucketname = "你的空間名稱";
此處圖片若不顯示,可以點擊該篇文章右側編輯按鈕查看對應圖片
(3).獲取圖片上傳URL路徑
//外鏈域名
public static String domain = "http://qcsorc5ip.bkt.clouddn.com/";
3.后端代碼調用
@RequestMapping("/upload")
@ResponseBody
public HashMap<String, Object> upload(@RequestParam("file") MultipartFile imgFile){
HashMap<String,Object> result = new HashMap<String,Object>();
String originalFilename = imgFile.getOriginalFilename();//獲取圖片原始文件名
int index = originalFilename.lastIndexOf(".");
String extention = originalFilename.substring(index);//獲得圖片后綴名 .jpg
String fileName = UUID.randomUUID().toString() + extention; //進行拼接
fileName = fileName.replace("-","");
try {
//使用工具類將文件上傳到七牛雲服務器
String filePath = QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
result.put("filePath",filePath);
} catch (IOException e) {
throw new BusinessException("上傳圖片失敗!",e.getMessage());
}
return result;
}
也可以參考https://blog.csdn.net/j1231230/article/details/80061834?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase