Home  >  Q&A  >  body text

七牛云存储 - 七牛上传文件后获取访问链接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:\\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
            }
        }
    }
PHP中文网PHP中文网2741 days ago869

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-18 10:49:31

    Qiniu will not return the URL of the file
    You only need to judge the last successful URL and you will know
    The corresponding IP or domain name of the bucket http://aa.com

    Then your image access path is:

    http://aa.com/Customized file name

    reply
    0
  • Cancelreply