SpringBoot上传文件到七牛云
pom依赖
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.27</version>
</dependency>
application.yml配置
qiniu:
url: 填你的域名
accessKey: 你的accesskey
secretKey: 你的secretKey
bucketname: 你的存储空间名
上传配置类
@Data
@Component
@ConfigurationProperties(prefix = "qiniu")
public class QieNiuProperties {
private String accessKey;
private String secretKey;
private String bucketname;
private String url;
}
配置工具类
@Component
public class PictureUtile {
@Resource
private QieNiuProperties qieNiuProperties;
public static String accessKey;
public static String secretKey;
public static String bucketname;
public static String url;
@PostConstruct
public void init() {
accessKey = this.qieNiuProperties.getAccessKey();
secretKey = this.qieNiuProperties.getSecretKey();
bucketname = this.qieNiuProperties.getBucketname();
url = this.qieNiuProperties.getUrl();
}
}
上传工具类
public class QianNiuUpload {
/**
* 上传图片,用本地路径名直接上传
* accessKey secretKey, bucket, url这四个参数在配置文件里,用配置类QieNiuProperties
* @param filePath
* @return
*/
public static String uploadImgByLocalFilePath(String filePath,String accessKey ,String secretKey,String bucket,String url) {
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
//如果是Windows情况下,格式是 D:\\qiniu\\test.png
String localFilePath = filePath;
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = null;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(localFilePath, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return url+putRet.key;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* 将内存中的字节数组上传到空间中 图片base64 不要,号前面的
* @param imgStr
* @return
*/
public static String uploadImgByBytes(String imgStr){
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = PictureUtile.accessKey;
String secretKey = PictureUtile.secretKey;
String bucket = PictureUtile.bucketname;
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] uploadBytes = decoder.decodeBuffer(imgStr);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(uploadBytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
return PictureUtile.url +putRet.key;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* 流的形式
* @param file
* @return
*/
public static String uploadFile(MultipartFile file) {
//构造一个带指定Region对象的配置类 需要设置存储空间
Configuration cfg = new Configuration(Region.huadong());
UploadManager uploadManager= new UploadManager(cfg);
Auth auth = Auth.create(PictureUtile.accessKey, PictureUtile.secretKey);
String token = auth.uploadToken(PictureUtile.bucketname);
try {
String originalFilename = file.getOriginalFilename();
// 文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileKey = UUID.randomUUID().toString() + suffix;
Response response = uploadManager.put(file.getInputStream(), fileKey, token, null, null);
return PictureUtile.url + fileKey;}
catch (Exception e) {
e.printStackTrace();
return "null";
}
}
}
Region配置,这里代表空间的存取区域。
查看自己的存储区域可在空间概览的最下方查看到。
Region对应的设置:
Controller调用
图片base64字符串 String iconImg
String qiuNiuIconImg = QianNiuUpload.uploadImgByBytes(iconImg);
MultipartFile file 流上传
String qiuNiuIconImg =QianNiuUpload.uploadFile(file);