七牛云存儲(chǔ) - 七牛上傳文件后獲取訪問(wèn)鏈接java
問(wèn)題描述
當(dāng)我用java上傳文件到七牛服務(wù)器后,如何讓七牛返回訪問(wèn)該文件的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 }} }
問(wèn)題解答
回答1:七牛不會(huì)返回文件的url你只需判斷上次成功了 URL 你就知道了bucket 對(duì)應(yīng)的IP 或 域名 http://aa.com
那么你的圖片訪問(wèn)路徑就是:
http://aa.com/自定義的文件名稱
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題2. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.3. PHP求助,求幫忙謝謝各位4. extra沒(méi)有加載出來(lái)5. Span標(biāo)簽6. django進(jìn)行數(shù)據(jù)庫(kù)的查詢7. python - linux怎么在每天的凌晨2點(diǎn)執(zhí)行一次這個(gè)log.py文件8. mysql - 為什么where條件中or加索引不起作用?9. 微信開(kāi)放平臺(tái) - Android上使用微信Oauth, 返回ERR_BAN是什麼意思?10. javascript - 天貓首頁(yè)首屏數(shù)據(jù)來(lái)源
