更改授权码和钥匙,外链域名即可使用
package co.yixiang.modules.wechat.rest.utils;
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.util.Auth;
/**
* 七牛云工具类
* Zone.zone0:华东
* Zone.zone1:华北
* Zone.zone2:华南
* Zone.zoneNa0:北美
* ———http上传,自动识别上传区域——
* Zone.httpAutoZone
* ———https上传,自动识别上传区域—— //Zone.httpsAutoZone
* Configuration cfg = new Configuration(Zone.zone2());其中Configuration中的Zone.zone2()可以改为Zone.zone0()
*/
public class QiniuUtils {
//访问授权码
public static String accessKey = "I2B9zvxKNWXxO8cp-jCDqXFXVum2UbKHW5zY4w3q";
//秘密钥匙
public static String secretKey = "WoHDOcE0N7-ZYXM1MlSBF6dEK0XC_7dmX15P0nQ5";
//空间名称
public static String bucket = "haoyong-yuanyuzhou";
//外链域名
public static String domain = "http://r673s884h.hn-bkt.clouddn.com/";
//上传方式一:外链+文件名 获取其他图片链接进行上传
public static void upload2Qiniu(String filePath,String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone2());
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(filePath, fileName, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
} catch (QiniuException ex) {
Response r = ex.response;
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}
//上传方式二:文件上传 通过上传文件的方式上传到存储空间
public static String upload2Qiniu(byte[] bytes, String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone2());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(bytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//System.out.println(putRet.key);
// System.out.println(putRet.hash);
//返回文件完整路径
return domain+putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
//System.err.println(r.toString());
try {
System.err.println(r.bodyString());
return "";
} catch (QiniuException ex2) {
//ignore
}
}
return "";
}
//删除文件 参数:存储的图片文件名
public static void deleteFileFromQiniu(String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone2());
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(bucket, key);
} catch (QiniuException ex) {
//如果遇到异常,说明删除失败
//System.err.println(ex.code());
// System.err.println(ex.response.toString());
}
}
}
@RequestMapping("/upload")
@ResponseBody
public HashMap<String, Object> upload(@RequestParam("file") MultipartFile imgFile){
HashMap<String,Object> result = new HashMap<String,Object>();
String originalFilename = imgFile.getOriginalFilename();//获取图片原始文件名
int index = originalFilename.lastIndexOf(".");
String extention = originalFilename.substring(index);//获得图片后缀名 .jpg
String fileName = UUID.randomUUID().toString() + extention; //进行拼接
fileName = fileName.replace("-",""); //将文件路径中的-替换掉
try {
//使用工具类将文件上传到七牛云服务器
String filePath = QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
result.put("filePath",filePath);
} catch (IOException e) {
throw new BusinessException("上传图片失败!",e.getMessage());
}
return result;
}