介紹
在實際開發中,我們會有很多處理不同功能的服務器。例如:
應用服務器:負責部署我們的應用
數據庫服務器:運行我們的數據庫
文件服務器:負責存儲用戶上傳文件的服務器
分服務器處理的目的是讓服務器各司其職,從而提高我們項目的運行效率。
常見的圖片存儲方案:
方案一:使用nginx搭建圖片服務器
方案二:使用開源的分布式文件存儲系統,例如Fastdfs、HDFS等
方案三:使用雲存儲,例如阿里雲、七牛雲等
七牛雲存儲
七牛雲(隸屬於上海七牛信息技術有限公司)是國內領先的以視覺智能和數據智能為核心的企業級雲計算服務商,同時也是國內知名智能視頻雲服務商,累計為 70 多萬家企業提供服務,覆蓋了國內80%網民。圍繞富媒體場景推出了對象存儲、融合 CDN 加速、容器雲、大數據平台、深度學習平台等產品、並提供一站式智能視頻雲解決方案。為各行業及應用提供可持續發展的智能視頻雲生態,幫助企業快速上雲,創造更廣闊的商業價值。
通過七牛雲官網介紹我們可以知道其提供了多種服務,我們主要使用的是七牛雲提供的對象存儲服務來存儲圖片。
注冊、登錄
要使用七牛雲的服務,首先需要注冊成為會員。地址:https://portal.qiniu.com/signup
注冊完成后就可以使用剛剛注冊的郵箱和密碼登錄到七牛雲:
登錄成功后點擊頁面右上角管理控制台:
注意:登錄成功后還需要進行實名認證才能進行相關操作。
新建存儲空間
要進行圖片存儲,我們需要在七牛雲管理控制台新建存儲空間。點擊管理控制台首頁對象存儲下的立即添加按鈕,頁面跳轉到新建存儲空間頁面:
可以創建多個存儲空間,各個存儲空間是相互獨立的。
查看存儲空間信息
存儲空間創建后,會在左側的存儲空間列表菜單中展示創建的存儲空間名稱,點擊存儲空間名稱可以查看當前存儲空間的相關信息
開發者中心
可以通過七牛雲提供的開發者中心學習如何操作七牛雲服務,地址:https://developer.qiniu.com/
點擊對象存儲,跳轉到對象存儲開發頁面,地址:https://developer.qiniu.com/kodo
七牛雲提供了多種方式操作對象存儲服務,本項目采用Java SDK方式,地址:https://developer.qiniu.com/kodo/sdk/1239/java
使用Java SDK操作七牛雲需要導入如下maven坐標:
<dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>7.2.0</version> </dependency>
鑒權
Java SDK的所有的功能,都需要合法的授權。授權憑證的簽算需要七牛賬號下的一對有效的Access Key和Secret Key,這對密鑰可以在七牛雲管理控制台的個人中心(https://portal.qiniu.com/user/key)獲得,如下圖:
Java SDK操作七牛雲
本章節我們就需要使用七牛雲提供的Java SDK完成圖片上傳和刪除,我們可以參考官方提供的例子。

//構造一個帶指定Zone對象的配置類 Configuration cfg = new Configuration(Zone.zone0()); //...其他參數參考類注釋 UploadManager uploadManager = new UploadManager(cfg); //...生成上傳憑證,然后准備上傳 String accessKey = "your access key"; String secretKey = "your secret key"; String bucket = "your bucket name"; //如果是Windows情況下,格式是 D:\\qiniu\\test.png String localFilePath = "/home/qiniu/test.png"; //默認不指定key的情況下,以文件內容的hash值作為文件名 String key = null; Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); try { Response response = uploadManager.put(localFilePath, key, upToken); //解析上傳成功的結果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { //ignore } }

//構造一個帶指定Zone對象的配置類 Configuration cfg = new Configuration(Zone.zone0()); //...其他參數參考類注釋 String accessKey = "your access key"; String secretKey = "your secret key"; String bucket = "your bucket name"; String key = "your file key"; 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()); }
封裝工具類
為了方便操作七牛雲存儲服務,我們可以將官方提供的案例簡單改造成一個工具類,在我們的項目中直接使用此工具類來操作就可以:

package com.itheima.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; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /** * 七牛雲工具類 */ public class QiniuUtils { public static String accessKey = "dulF9Wze9bxujtuRvu3yyYb9JX1Sp23jzd3tO708"; public static String secretKey = "vZkhW7iot3uWwcWz9vXfbaP4JepdWADFDHVLMZOe"; public static String bucket = "qiniutest"; public static void upload2Qiniu(String filePath,String fileName){ //構造一個帶指定Zone對象的配置類 Configuration cfg = new Configuration(Zone.zone0()); 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 void upload2Qiniu(byte[] bytes, String fileName){ //構造一個帶指定Zone對象的配置類 Configuration cfg = new Configuration(Zone.zone0()); //...其他參數參考類注釋 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); } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { //ignore } } } //刪除文件 public static void deleteFileFromQiniu(String fileName){ //構造一個帶指定Zone對象的配置類 Configuration cfg = new Configuration(Zone.zone0()); 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()); } } }
將此工具類放在health_common工程中,后續會使用到。