当我用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:\\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 } } }
七牛不会返回文件的url
你只需判断上次成功了 URL 你就知道了
bucket 对应的IP 或 域名 http://aa.com
那么你的图片访问路径就是:
http://aa.com/自定义的文件名称