七牛云存儲 - 七牛上傳文件后獲取訪問鏈接java
問題描述
當(dāng)我用java上傳文件到七牛服務(wù)器后,如何讓七牛返回訪問該文件的url。
public static void uploadFile(String localFilePath,String fileName){Configuration cfg = new Configuration(Zone.zone1());//...其他參數(shù)參考類注釋UploadManager uploadManager = new UploadManager(cfg);//...生成上傳憑證,然后準(zhǔn)備上傳String accessKey = 'your access key';String secretKey = 'your secret key';String bucket = 'your bucket name';//如果是Windows情況下,格式是 D:qiniutest.png//String localFilePath = '/home/qiniu/test.png';//默認(rèn)不指定key的情況下,以文件內(nèi)容的hash值作為文件名String key = null;Auth auth = Auth.create(accessKey, secretKey);String upToken = auth.uploadToken(bucket);try { Response response = uploadManager.put(localFilePath, key, upToken); //解析上傳成功的結(jié)果 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 }} }
問題解答
回答1:七牛不會返回文件的url你只需判斷上次成功了 URL 你就知道了bucket 對應(yīng)的IP 或 域名 http://aa.com
那么你的圖片訪問路徑就是:
http://aa.com/自定義的文件名稱
相關(guān)文章:
1. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?2. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?3. docker start -a dockername 老是卡住,什么情況?4. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?5. docker gitlab 如何git clone?6. dockerfile - [docker build image失敗- npm install]7. docker不顯示端口映射呢?8. docker-compose 為何找不到配置文件?9. docker內(nèi)創(chuàng)建jenkins訪問另一個容器下的服務(wù)器問題10. 最新版本的PhpStudy支持64位的PHP嗎? 為什么我把64位的PHP放入后,后臺收不到POST
