七牛云存储 - 七牛上传文件后获取访问链接java
问题描述
当我用java上传文件到七牛服务器后,如何让七牛返回访问该文件的url。
public static void uploadFile(String localFilePath,String fileName){Configuration cfg = new Configuration(Zone.zone1());//...其他参数参考类注释UploadManager uploadManager = new UploadManager(cfg);//...生成上传凭证,然后准备上传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';//默认不指定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 }} }
问题解答
回答1:七牛不会返回文件的url你只需判断上次成功了 URL 你就知道了bucket 对应的IP 或 域名 http://aa.com
那么你的图片访问路径就是:
http://aa.com/自定义的文件名称
相关文章:
1. extra没有加载出来2. PHP类属性声明?3. 分页显示 - MySQL分页查询,是用LIMIT m,n,还是先查出所有ID再在前端分页?4. phpstudy 发现多个后门木马,有人遇到过吗?5. tp6 事务闭包操作问题6. 请教各位高手,关于 PHP取值问题7. mysql优化 - mysql 5.7 not in != 索引有效,是为什么啊?更新了吗?8. mysql - 仅仅只是把单引号与反斜杠转义不用prepare statement能否避免sql注入?9. mysql-配置 - MySQL错误,时不时自动挂掉,无法启动10. thinkphp框架放根目录无法像老师一样可以正常访问

网公网安备