40 lines
1.2 KiB
Java
40 lines
1.2 KiB
Java
|
|
package com.ruoyi.common.utils;
|
||
|
|
|
||
|
|
import com.google.gson.Gson;
|
||
|
|
import com.qiniu.common.QiniuException;
|
||
|
|
import com.qiniu.http.Response;
|
||
|
|
import com.qiniu.storage.Configuration;
|
||
|
|
import com.qiniu.storage.Region;
|
||
|
|
import com.qiniu.storage.UploadManager;
|
||
|
|
import com.qiniu.storage.model.DefaultPutRet;
|
||
|
|
import com.qiniu.util.Auth;
|
||
|
|
import com.ruoyi.common.utils.uuid.UUID;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author clunt
|
||
|
|
*/
|
||
|
|
public class QiniuUtils {
|
||
|
|
|
||
|
|
public static String getUpToken() {
|
||
|
|
Auth auth = Auth.create("QTNOppkvtufxTxLjt1V7YZwvzV2Rc6WLD5yXLBVY", "V8SM9nkbO-dft4JmG7UaCH6RYxXdqzrvQ0zWO2W3");
|
||
|
|
return auth.uploadToken("suteng");
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String upload(byte[] uploadBytes) throws QiniuException {
|
||
|
|
|
||
|
|
String key = UUID.randomUUID().toString().replaceAll("-", "");
|
||
|
|
|
||
|
|
//设置华南的服务器
|
||
|
|
Configuration cfg = new Configuration(Region.region2());
|
||
|
|
UploadManager uploadManager = new UploadManager(cfg);
|
||
|
|
|
||
|
|
String upToken = getUpToken();
|
||
|
|
Response response = uploadManager.put(uploadBytes, key, upToken);
|
||
|
|
//解析上传成功的结果
|
||
|
|
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||
|
|
|
||
|
|
return "http://st.opsoul.com/" + putRet.key;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|