|
@@ -27,23 +27,27 @@ import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author reghao
|
|
* @author reghao
|
|
|
- * @date 2022-12-09 10:45:42
|
|
|
|
|
|
|
+ * @date 2023-06-02 16:19:04
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class ObjectMultipartUploadService {
|
|
public class ObjectMultipartUploadService {
|
|
|
- static String endpoint = "http://localhost:8010/";
|
|
|
|
|
- static FilePart filePart = new FilePart();
|
|
|
|
|
- static HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
- static WebRequest webRequest = new DefaultWebRequest();
|
|
|
|
|
|
|
+ private final String endpoint;
|
|
|
|
|
+ private final FilePart filePart = new FilePart();
|
|
|
|
|
+ private final HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
+ private final WebRequest webRequest = new DefaultWebRequest();
|
|
|
|
|
|
|
|
- static void create() throws URISyntaxException, IOException, InterruptedException {
|
|
|
|
|
|
|
+ public ObjectMultipartUploadService(String endpoint) {
|
|
|
|
|
+ this.endpoint = endpoint;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void create() throws URISyntaxException, IOException, InterruptedException {
|
|
|
MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
|
|
MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
|
|
|
publisher.addPart("filename", "1.zip")
|
|
publisher.addPart("filename", "1.zip")
|
|
|
.addPart("size", 1+"")
|
|
.addPart("size", 1+"")
|
|
|
.addPart("sha256sum", "1234567890");
|
|
.addPart("sha256sum", "1234567890");
|
|
|
|
|
|
|
|
String authorization = "";
|
|
String authorization = "";
|
|
|
- String api = "http://localhost:8010/?create";
|
|
|
|
|
|
|
+ String api = endpoint + "/?create";
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
|
.header("Authorization", authorization)
|
|
.header("Authorization", authorization)
|
|
@@ -63,9 +67,9 @@ public class ObjectMultipartUploadService {
|
|
|
System.out.println();
|
|
System.out.println();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static void get() throws URISyntaxException, IOException, InterruptedException {
|
|
|
|
|
|
|
+ public void get() throws URISyntaxException, IOException, InterruptedException {
|
|
|
String authorization = "";
|
|
String authorization = "";
|
|
|
- String api = "http://localhost:8010/?multipart";
|
|
|
|
|
|
|
+ String api = endpoint + "/?multipart";
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
|
.header("Authorization", authorization)
|
|
.header("Authorization", authorization)
|
|
@@ -92,7 +96,7 @@ public class ObjectMultipartUploadService {
|
|
|
* @return
|
|
* @return
|
|
|
* @date 2023-05-24 14:56:50
|
|
* @date 2023-05-24 14:56:50
|
|
|
*/
|
|
*/
|
|
|
- static void postObject1(byte[] bytes, UploadFilePart uploadFilePart)
|
|
|
|
|
|
|
+ public void postObject1(byte[] bytes, UploadFilePart uploadFilePart)
|
|
|
throws URISyntaxException, IOException, InterruptedException {
|
|
throws URISyntaxException, IOException, InterruptedException {
|
|
|
MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
|
|
MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
@@ -109,7 +113,7 @@ public class ObjectMultipartUploadService {
|
|
|
.addPart("chunkNumber", uploadFilePart.getChunkNumber()+"");
|
|
.addPart("chunkNumber", uploadFilePart.getChunkNumber()+"");
|
|
|
|
|
|
|
|
String authorization = "";
|
|
String authorization = "";
|
|
|
- String api = "http://localhost:8010/?multipart";
|
|
|
|
|
|
|
+ String api = endpoint + "/?multipart";
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
|
.header("Authorization", authorization)
|
|
.header("Authorization", authorization)
|
|
@@ -121,7 +125,7 @@ public class ObjectMultipartUploadService {
|
|
|
System.out.println();
|
|
System.out.println();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static void postObject(byte[] bytes, UploadFilePart uploadFilePart) {
|
|
|
|
|
|
|
+ public void postObject(byte[] bytes, UploadFilePart uploadFilePart) {
|
|
|
UploadParam uploadParam = new UploadParam(bytes, "");
|
|
UploadParam uploadParam = new UploadParam(bytes, "");
|
|
|
Map<String, String> params = new HashMap<>();
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("channelId", uploadFilePart.getChannelId()+"");
|
|
params.put("channelId", uploadFilePart.getChannelId()+"");
|
|
@@ -135,7 +139,7 @@ public class ObjectMultipartUploadService {
|
|
|
params.put("chunkNumber", uploadFilePart.getChunkNumber()+"");
|
|
params.put("chunkNumber", uploadFilePart.getChunkNumber()+"");
|
|
|
uploadParam.setTextParams(params);
|
|
uploadParam.setTextParams(params);
|
|
|
|
|
|
|
|
- String api = "http://localhost:8010/?multipart";
|
|
|
|
|
|
|
+ String api = endpoint + "/?multipart";
|
|
|
WebResponse webResponse = webRequest.upload(api, uploadParam);
|
|
WebResponse webResponse = webRequest.upload(api, uploadParam);
|
|
|
int statusCode = webResponse.getStatusCode();
|
|
int statusCode = webResponse.getStatusCode();
|
|
|
if (statusCode != 200) {
|
|
if (statusCode != 200) {
|
|
@@ -157,7 +161,7 @@ public class ObjectMultipartUploadService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static void upload(String pid, int channelId, File file) throws Exception {
|
|
|
|
|
|
|
+ public void upload(File file, int channelId) throws Exception {
|
|
|
String identifier = DigestUtil.sha256sum(new FileInputStream(file));
|
|
String identifier = DigestUtil.sha256sum(new FileInputStream(file));
|
|
|
String filename = file.getName();
|
|
String filename = file.getName();
|
|
|
String relativePath = file.getAbsolutePath();
|
|
String relativePath = file.getAbsolutePath();
|
|
@@ -175,19 +179,9 @@ public class ObjectMultipartUploadService {
|
|
|
byte[] part = filePart.getPart(file.getAbsolutePath(), chunkSize, start);
|
|
byte[] part = filePart.getPart(file.getAbsolutePath(), chunkSize, start);
|
|
|
long chunkNumber = i + 1;
|
|
long chunkNumber = i + 1;
|
|
|
int currentChunkSize = part.length;
|
|
int currentChunkSize = part.length;
|
|
|
- UploadFilePart uploadFilePart = new UploadFilePart(pid, channelId, identifier, filename, relativePath,
|
|
|
|
|
|
|
+ UploadFilePart uploadFilePart = new UploadFilePart(channelId, identifier, filename, relativePath,
|
|
|
totalSize, chunkSize, totalChunks, chunkNumber, currentChunkSize);
|
|
totalSize, chunkSize, totalChunks, chunkNumber, currentChunkSize);
|
|
|
postObject(part, uploadFilePart);
|
|
postObject(part, uploadFilePart);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
|
|
- String filePath = "/home/reghao/Downloads/abc.mp4";
|
|
|
|
|
-
|
|
|
|
|
- String pid = "0";
|
|
|
|
|
- int channelId = 1;
|
|
|
|
|
- upload(pid, channelId, new File(filePath));
|
|
|
|
|
- //create();
|
|
|
|
|
- //get();
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|