新建存储空间
进入对象存储菜单,点击“新建存储空间”,这里需要实名认证,上传身份证正反面之类的,大概一个小时左右就认证成功了,效率真是棒棒哒~
这里的存储空间名称要记住,之后在代码里面会用到。
二.代码实现
1.在pom.xml添加七牛云依赖
我的项目使用了maven管理jar包,所以只需直接添加相应依赖即可:
com.qiniu
qiniu-java-sdk
7.2.6
2.添加七牛云图片操作工具类
package gbq.ssm.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 = "NmqvfVvXetScbrEOXgTE2Szg21qdSxp-sTRHaBSB";
public static String secretKey = "ft26PJWXnAfs6QksAnHQT85FqDDv2VUXP-AXjEt8";
//空间名称
public static String bucket = "picture-server01";
//外链域名
public static String domain = "http://qcsorc5ip.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());
}
}
}
此工具类中注意事项:
(1).获取需要操作的账号的AK和SK
public static String accessKey = "NmqvfVvXetScbrEOXgTE2Szg21qdSxp-sTRHaBSB";
public static String secretKey = "ft26PJWXnAfs6QksAnHQT85FqDDv2VUXP-AXjEt8";
进入个人中心-密钥管理
(2).获取要上传的空间
private static final String bucketname = "你的空间名称";
此处图片若不显示,可以点击该篇文章右侧编辑按钮查看对应图片
(3).获取图片上传URL路径
//外链域名
public static String domain = "http://qcsorc5ip.bkt.clouddn.com/";
3.后端代码调用
@RequestMapping("/upload")
@ResponseBody
public HashMapupload(@RequestParam("file") MultipartFile imgFile){
HashMapresult = new HashMap ();
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;
}
也可以参考https://blog.csdn.net/j1231230/article/details/80061834?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase