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

Posted sunsing123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了七牛上传图片和二进制流方法相关的知识,希望对你有一定的参考价值。

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;
}


}




































































































































以上是关于七牛上传图片和二进制流方法的主要内容,如果未能解决你的问题,请参考以下文章

二进制流图片上传类-分享

JS 上传图片转换成二进制流base64

C#winform 中上传图片保存到数据库中?

umeditor实现ctrl+v粘贴word图片并上传

Canvas保存图片到七牛云

Canvas保存图片到七牛云