添加断点上传

master
sikadi 2023-11-02 16:56:14 +08:00
parent ebd55ef6e9
commit 34b71257f2
3 changed files with 78 additions and 0 deletions

26
pom.xml
View File

@ -154,6 +154,32 @@
</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>
</project>

View File

@ -33,6 +33,17 @@ public class QiNiuUpload {
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;
}

View File

@ -8,8 +8,10 @@ import com.grail.common.core.utils.file.MimeTypeUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import com.qiniu.util.UrlSafeBase64;
import com.grail.common.core.config.QiNiuConfig;
@ -25,6 +27,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Paths;
/**
*
@ -32,6 +35,44 @@ import java.net.URLEncoder;
**/
@Slf4j
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