添加断点上传
parent
ebd55ef6e9
commit
34b71257f2
26
pom.xml
26
pom.xml
|
@ -154,6 +154,32 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>3.14.2</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.5</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.qiniu</groupId>
|
||||||
|
<artifactId>happy-dns-java</artifactId>
|
||||||
|
<version>0.1.6</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -33,6 +33,17 @@ public class QiNiuUpload {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String bigFile(MultipartFile file){
|
||||||
|
|
||||||
|
String originalFilename = file.getOriginalFilename();
|
||||||
|
|
||||||
|
String key = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 15);
|
||||||
|
|
||||||
|
String s = QiNiuUtil.bigFile(originalFilename, key);
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,10 @@ 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;
|
||||||
|
import com.qiniu.storage.Region;
|
||||||
import com.qiniu.storage.UploadManager;
|
import com.qiniu.storage.UploadManager;
|
||||||
import com.qiniu.storage.model.DefaultPutRet;
|
import com.qiniu.storage.model.DefaultPutRet;
|
||||||
|
import com.qiniu.storage.persistent.FileRecorder;
|
||||||
import com.qiniu.util.Auth;
|
import com.qiniu.util.Auth;
|
||||||
import com.qiniu.util.UrlSafeBase64;
|
import com.qiniu.util.UrlSafeBase64;
|
||||||
import com.grail.common.core.config.QiNiuConfig;
|
import com.grail.common.core.config.QiNiuConfig;
|
||||||
|
@ -25,6 +27,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 七牛上传下载工具类
|
* 七牛上传下载工具类
|
||||||
|
@ -32,6 +35,44 @@ import java.net.URLEncoder;
|
||||||
**/
|
**/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class QiNiuUtil {
|
public class QiNiuUtil {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String bigFile(String localFilePath,String key){
|
||||||
|
Configuration cfg = new Configuration(Region.region0());
|
||||||
|
Auth auth = Auth.create(QiNiuConfig.getInstance().getAccessKey(), QiNiuConfig.getInstance().getSecretKey());
|
||||||
|
String upToken = auth.uploadToken(QiNiuConfig.getInstance().getBucket());
|
||||||
|
String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), QiNiuConfig.getInstance().getBucket()).toString();
|
||||||
|
try {
|
||||||
|
//设置断点续传文件进度保存目录
|
||||||
|
FileRecorder fileRecorder = new FileRecorder(localTempDir);
|
||||||
|
UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
|
||||||
|
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);
|
||||||
|
} catch (QiniuException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
if (ex.response != null) {
|
||||||
|
System.err.println(ex.response);
|
||||||
|
try {
|
||||||
|
String body = ex.response.toString();
|
||||||
|
System.err.println(body);
|
||||||
|
return body;
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传本地文件
|
* 上传本地文件
|
||||||
* @param localFilePath 本地文件完整路径
|
* @param localFilePath 本地文件完整路径
|
||||||
|
|
Loading…
Reference in New Issue