做出更改
parent
7f6db19bcc
commit
ebd55ef6e9
6
pom.xml
6
pom.xml
|
@ -128,6 +128,12 @@
|
||||||
<version>7.2.11</version>
|
<version>7.2.11</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.qiniu</groupId>
|
||||||
|
<artifactId>qiniu-java-sdk</artifactId>
|
||||||
|
<version>[7.2.0, 7.2.99]</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 七牛云依赖Gson -->
|
<!-- 七牛云依赖Gson -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
|
|
@ -18,6 +18,7 @@ public class QiNiuConfig {
|
||||||
private Zone zone;
|
private Zone zone;
|
||||||
private String domainOfBucket ;
|
private String domainOfBucket ;
|
||||||
private long expireInSeconds;
|
private long expireInSeconds;
|
||||||
|
private String domain;
|
||||||
|
|
||||||
private static QiNiuConfig instance = new QiNiuConfig();
|
private static QiNiuConfig instance = new QiNiuConfig();
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ public class QiNiuConfig {
|
||||||
secretKey = "rST5FwmF-DThi10FtddcjM0yVTCtoTnhUasA4hs7";
|
secretKey = "rST5FwmF-DThi10FtddcjM0yVTCtoTnhUasA4hs7";
|
||||||
bucket = "where-is-you";
|
bucket = "where-is-you";
|
||||||
domainOfBucket = "s30y1cerv.hd-bkt.clouddn.com";
|
domainOfBucket = "s30y1cerv.hd-bkt.clouddn.com";
|
||||||
|
domain="s30y1cerv.hd-bkt.clouddn.com";
|
||||||
expireInSeconds = -1;
|
expireInSeconds = -1;
|
||||||
zone = Zone.zone0();
|
zone = Zone.zone0();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -27,4 +27,20 @@ public class QiNiuUpload {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean uploadVideo(MultipartFile file){
|
||||||
|
String key = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 15);
|
||||||
|
boolean b = QiNiuUtil.uploadMultipartFile(file, key, true);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package com.grail.common.core.utils;
|
package com.grail.common.core.utils;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.grail.common.core.exception.file.FileNameLengthLimitExceededException;
|
||||||
|
import com.grail.common.core.exception.file.InvalidExtensionException;
|
||||||
|
import com.grail.common.core.utils.file.FileTypeUtils;
|
||||||
|
import com.grail.common.core.utils.file.MimeTypeUtils;
|
||||||
import com.qiniu.common.QiniuException;
|
import com.qiniu.common.QiniuException;
|
||||||
import com.qiniu.http.Response;
|
import com.qiniu.http.Response;
|
||||||
import com.qiniu.storage.Configuration;
|
import com.qiniu.storage.Configuration;
|
||||||
|
@ -13,6 +17,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
|
import org.apache.commons.fileupload.FileUploadBase;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
@ -187,4 +192,99 @@ public class QiNiuUtil {
|
||||||
Auth auth = Auth.create(QiNiuConfig.getInstance().getAccessKey(), QiNiuConfig.getInstance().getSecretKey());
|
Auth auth = Auth.create(QiNiuConfig.getInstance().getAccessKey(), QiNiuConfig.getInstance().getSecretKey());
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传本地文件
|
||||||
|
* @param localFilePath 本地文件完整路径
|
||||||
|
* @param key 文件云端存储的名称
|
||||||
|
* @param override 是否覆盖同名同位置文件
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean uploadList(String localFilePath, String key, boolean override){
|
||||||
|
//构造一个带指定Zone对象的配置类
|
||||||
|
Configuration cfg = new Configuration(QiNiuConfig.getInstance().getZone());
|
||||||
|
//...其他参数参考类注释
|
||||||
|
UploadManager uploadManager = new UploadManager(cfg);
|
||||||
|
//...生成上传凭证,然后准备上传
|
||||||
|
Auth auth = Auth.create(QiNiuConfig.getInstance().getAccessKey(), QiNiuConfig.getInstance().getSecretKey());
|
||||||
|
String upToken;
|
||||||
|
if(override){
|
||||||
|
upToken = auth.uploadToken(QiNiuConfig.getInstance().getBucket(), key);//覆盖上传凭证
|
||||||
|
}else{
|
||||||
|
upToken = auth.uploadToken(QiNiuConfig.getInstance().getBucket());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Response response = uploadManager.put(localFilePath, key, upToken);
|
||||||
|
//解析上传成功的结果
|
||||||
|
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||||
|
System.out.println(putRet.key);
|
||||||
|
System.out.println(putRet.hash);
|
||||||
|
return true;
|
||||||
|
} catch (QiniuException ex) {
|
||||||
|
Response r = ex.response;
|
||||||
|
System.err.println(r.toString());
|
||||||
|
try {
|
||||||
|
System.err.println(r.bodyString());
|
||||||
|
} catch (QiniuException ex2) {
|
||||||
|
//ignore
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String uploadMultipartFile(MultipartFile file, boolean override) throws FileUploadBase.FileSizeLimitExceededException, InvalidExtensionException {
|
||||||
|
//构造一个带指定Zone对象的配置类
|
||||||
|
Configuration cfg = new Configuration(QiNiuConfig.getInstance().getZone());
|
||||||
|
//...其他参数参考类注释
|
||||||
|
UploadManager uploadManager = new UploadManager(cfg);
|
||||||
|
|
||||||
|
//把文件转化为字节数组
|
||||||
|
InputStream is = null;
|
||||||
|
ByteArrayOutputStream bos = null;
|
||||||
|
int fileNamelength = file.getOriginalFilename().length();
|
||||||
|
if (fileNamelength > 1000)
|
||||||
|
{
|
||||||
|
throw new FileNameLengthLimitExceededException(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
String extension = FileTypeUtils.getExtension(file);
|
||||||
|
try {
|
||||||
|
is = file.getInputStream();
|
||||||
|
bos = new ByteArrayOutputStream();
|
||||||
|
byte[] b = new byte[1024];
|
||||||
|
int len = -1;
|
||||||
|
while ((len = is.read(b)) != -1){
|
||||||
|
bos.write(b, 0, len);
|
||||||
|
}
|
||||||
|
byte[] uploadBytes= bos.toByteArray();
|
||||||
|
|
||||||
|
Auth auth = getAuth();
|
||||||
|
String upToken;
|
||||||
|
if(override){
|
||||||
|
upToken = auth.uploadToken(QiNiuConfig.getInstance().getBucket(), fileName);//覆盖上传凭证
|
||||||
|
}else{
|
||||||
|
upToken = auth.uploadToken(QiNiuConfig.getInstance().getBucket());
|
||||||
|
}
|
||||||
|
//默认上传接口回复对象
|
||||||
|
DefaultPutRet putRet;
|
||||||
|
//进行上传操作,传入文件的字节数组,文件名,上传空间,得到回复对象
|
||||||
|
Response response = uploadManager.put(uploadBytes, fileName, upToken);
|
||||||
|
putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||||
|
System.out.println(putRet.key);//key 文件名
|
||||||
|
System.out.println(putRet.hash);//hash 七牛返回的文件存储的地址,可以使用这个地址加七牛给你提供的前缀访问到这个视频。
|
||||||
|
return QiNiuConfig.getInstance().getDomain()+putRet.hash;
|
||||||
|
}catch (QiniuException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue