码迷,mamicode.com
首页 > Web开发 > 详细

七牛上传图片和二进制流方法

时间:2018-10-26 14:25:10      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:fileinput   ini   path   tor   上传   body   app   trace   默认   

package com.qlyd.aspmanager.common.qiniu;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;
import com.qlyd.aspmanager.common.uuid.UUIDTool;

import java.io.FileInputStream;
import java.io.InputStream;

/**
* @author madebychina
* @date 2018/9/5 10:12
*/
public class WebFileUploadTool {
/**
* 七牛AK
*/
public static final String accessKey = "bLNti1fu58oi6ZAlIS0y49RyU00CdUpuajbc8GWj";
/**
* 七牛SK
*/
public static final String secretKey = "7zruUMyV_VqluxsjHQs0Ozb1GrkINmBmAV9GxlLQ";
/**
* 存储空间名字
*/
public static final String bucket = "mini-app";
/**
* 七牛绑定的自定义域名
*/
public static final String BUCKET_HOST_NAME = "https://miniapp.qiluyidian.mobi";

/**
* 上传web文件到七牛
*
* @param filePath 文件地址
* @return 七牛存储地址
*/
public static String uploadWebFile(String filePath) {
String imgUrl = null;
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
String key = UUIDTool.getUUID();
String extension = FileNameTool.getWebFileExtensionName(filePath);
if (extension.length() > 0) {
key += "." + FileNameTool.getWebFileExtensionName(filePath);
}
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
FetchRet fetchRet = bucketManager.fetch(filePath, bucket, key);
//七牛的实际存储地址
imgUrl = BUCKET_HOST_NAME + "/" + fetchRet.key;
} catch (QiniuException ex) {
ex.printStackTrace();
}
return imgUrl;
}

public String uploadImg(FileInputStream file, String key) {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone1());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
// String bucket = "oy09glbzm.bkt.clouddn.com";
//默认不指定key的情况下,以文件内容的hash值作为文件名
try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(file, key, upToken, null, null);
//解析上传成功的结果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
String return_path = BUCKET_HOST_NAME + "/" + putRet.key;
return return_path;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

/**
* 通过输入流上传
*
* @param inputStream
* @return
* @throws
*/

public static String upload(InputStream inputStream) {
String imgUrl = "";
Configuration cfg = new Configuration(Zone.zone0());
//创建上传对象
UploadManager uploadManager = new UploadManager(cfg);
String key = UUIDTool.getUUID();

try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(inputStream, key, upToken, null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
imgUrl = BUCKET_HOST_NAME + "/" + putRet.key;
} catch (QiniuException ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return imgUrl;
}


}

七牛上传图片和二进制流方法

标签:fileinput   ini   path   tor   上传   body   app   trace   默认   

原文地址:https://www.cnblogs.com/sunsing123/p/9855855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!