上传工具类
package com.yy.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;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* 七牛云工具类
*/
public class QiniuUtils {
public static String accessKey = "8Pi4mJIPSewJPaPzVDooAhkMPraPVF2EUkPGNUm9";
public static String secretKey = "aACHfFh02v-OUok7IzMqJOXT-0zPRgCoVPIACyR3";
public static String bucket = "yyhealth-space-1";
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 void 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);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}
//删除文件
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());
}
}
}
基本使用步骤
不要照搬,
- Result 是我写的一个实体类
- imgFile是使用vue上传组件,传递过来的数据
- UID.randomUUID().toString()通俗的可以理解为,为了保证文件名不重复,所采取的一个随机生成文件名的方法
- jedisPool.getResource().sadd这个是操作redis数据库
- 以上的都不怎么重要,主要的还是
QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
这个才是关键,也就是说,我们只需要将工具类给声明出来,然后直接调用就可以了
- 下面的实现是使用框架,应该可以看出来,所以不要盲目全部抄,理解最重要
//文件上传
@RequestMapping("/upload")
public Result upload(@RequestParam("imgFile") MultipartFile imgFile){//获取imgFile
System.out.println(imgFile);
//设置文件名
String originalFilename = imgFile.getOriginalFilename();//原始文件名(上传时文件的名字) 3bd90d2c-4e82-42a1-a401-882c88b06a1a2.jpg
int index = originalFilename.lastIndexOf(".");//获取最后一个点的位置
String extention = originalFilename.substring(index - 1);//通过截取字符串获取:.jpg
String fileName = UUID.randomUUID().toString() + extention;// FuM1Sa5TtL_ekLsdkYWcf5pyjKGu.jpg
try {
//将文件上传到七牛云服务器
QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);//fileName使用uuid自动产生,使其不重复
jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);//操作set集合,想指定的set集合里面添加数据
} catch (IOException e) {
e.printStackTrace();
return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
}
return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
}