七牛云上传图片的方法:
1,注册七牛云账号,
2,创建一个存储空间bucket,创建的时候回送一个临时的测试域名,这个等上传工具类要用到 ,有效期30天,
3,写java工具类public class upLoadFile {
//...生成上传凭证,然后准备上传 这个是在注册的账户里面有个人中心那里的密钥管理
1
public static String accessKey = “h9r4Vz72dIAEL2PLSYXFEv39GmUOsw99y7wkbQQW”;
public static String secretKey = “BWhiWbB7D-lWkI2bP2c6wBYRbak3MzmcUeoyTaNv”;
// public static String bucket = “vehicle-picture”;
//这个是创建的存储空间的名字
public static String bucket = “image-a”;
//这个就是测试域名 创建的时候一般都会赠送一个测试域名
// public static String domainOfBucket = “http://qn.vwaiter.cn”;
public static String domainOfBucket = “pn7fhqast.bkt.clouddn.com”;
public static void main(String[] args)
{
getUrl(“FpMjfsJwwMDYC-IXjMIhWm9xiYt4”);
}
/**
* 上传图片
* @param file
* @return
*/
public static String uploadFile(MultipartFile file) {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//默认不指定key的情况下,以文件内容的hash值作为文件名
String fileName=null;
String key = null;
try {
Logger.getLogger("start>>>>>>>>>>").info("图片上传");
byte[] uploadBytes=file.getBytes();
ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(byteInputStream,key,upToken,null, null);
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//key为空时默认使用hash值作为key
System.out.println(putRet.key);
System.out.println(putRet.hash);
fileName=putRet.hash;
Logger.getLogger("end>>>>>>>>>>").info("图片上传");
}catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
}
catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
/**
* 上传图片
* @param bytes 要上传图片的字节流
* @return
*/
public static String uploadFile(byte[] bytes) {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//默认不指定key的情况下,以文件内容的hash值作为文件名
String fileName=null;
String key = null;
try {
Logger.getLogger("start>>>>>>>>>>").info("图片上传");
byte[] uploadBytes=bytes;
ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(byteInputStream,key,upToken,null, null);
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//key为空时默认使用hash值作为key
System.out.println(putRet.key);
System.out.println(putRet.hash);
fileName=putRet.hash;
Logger.getLogger("end>>>>>>>>>>").info("图片上传");
}catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
}
catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
/**
* 字节数组上传
* @return
*/
public static String upLoadByte(byte[] data) {
Configuration cfg = new Configuration(Zone.zone0());
UploadManager uploadManager = new UploadManager(cfg);
String fileName=null;
String key = null;
try {
byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(data, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
fileName=putRet.hash;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (UnsupportedEncodingException ex) {
}
return fileName;
}
/**
* 生成加签访问url
*/
public static String getUrl(String key) {
String finalUrl=null;
String fileName = key;
String encodedFileName = null;
try {
encodedFileName = URLEncoder.encode(fileName, "utf-8");
String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
Auth auth = Auth.create(accessKey, secretKey);
long expireInSeconds = 10800;//1小时,可以自定义链接过期时间
finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(finalUrl);
return finalUrl;
}
/**
* 生成加签访问url
*/
public static String getUrl1(String key) {
String finalUrl=null;
String fileName = key;
String encodedFileName = null;
try {
encodedFileName = URLEncoder.encode(fileName, "utf-8");
String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
Auth auth = Auth.create(accessKey, secretKey);
long expireInSeconds = 10800;//1小时,可以自定义链接过期时间
finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(finalUrl);
return finalUrl;
}
//删除上传到七牛云的图片
public static boolean deleteOnQn(String key) throws Exception {
//密钥配置
try {
Auth auth = Auth.create(accessKey,secretKey);
Configuration configuration = new Configuration(Zone.autoZone());
BucketManager bucketManager = new BucketManager(auth,configuration);
bucketManager.delete(bucket,key);
} catch (QiniuException e) {
e.printStackTrace();
throw e;
}
return true;
}
4,需要的依赖架
com.qiniu
qiniu-java-sdk
7.2.11
compile
com.squareup.okhttp3
okhttp
3.3.1
compile
com.google.code.gson
gson
2.6.2
compile
com.qiniu
happy-dns-java
0.1.4
compile
junit
junit
4.12
test
5,xml里面需要文件上传处理
6,写上传接口的controller和service以及serviceimpl,
更多相关技术文章,请访问PHP中文网!
本文原创发布php中文网,转载请注明出处,感谢您的尊重!