|
|
@@ -41,120 +41,96 @@ public class ObjectUploadService {
|
|
|
this.endpoint = endpoint;
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet putObject(File file, int channelId, long userId) {
|
|
|
- try {
|
|
|
- String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
- String api = String.format("%s/", endpoint);
|
|
|
- HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
|
|
|
- .version(HttpClient.Version.HTTP_1_1)
|
|
|
- .header("x-content-sha256sum", sha256sum)
|
|
|
- .header("x-channel-id", channelId+"")
|
|
|
- .header("x-user-id", userId+"");
|
|
|
-
|
|
|
- HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofFile(Path.of(file.getAbsolutePath()))).build();
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- return getResult(httpResponse);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
+ public UploadFileRet putObject(File file, int channelId, long userId) throws Exception {
|
|
|
+ String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
+ String api = String.format("%s/", endpoint);
|
|
|
+ HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
|
|
|
+ .version(HttpClient.Version.HTTP_1_1)
|
|
|
+ .header("x-content-sha256sum", sha256sum)
|
|
|
+ .header("x-channel-id", channelId+"")
|
|
|
+ .header("x-user-id", userId+"");
|
|
|
+
|
|
|
+ HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofFile(Path.of(file.getAbsolutePath()))).build();
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
+ return getResult(httpResponse);
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet putObject(InputStream inputStream, int channelId, int userId) {
|
|
|
- try {
|
|
|
- String api = String.format("%s/", endpoint);
|
|
|
- HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
|
|
|
- .version(HttpClient.Version.HTTP_1_1)
|
|
|
- .header("x-content-sha256sum", "1234567890")
|
|
|
- .header("x-channel-id", channelId+"")
|
|
|
- .header("x-user-id", userId+"");
|
|
|
-
|
|
|
- BufferedInputStream bis = new BufferedInputStream(inputStream);
|
|
|
- Supplier<? extends InputStream> streamSupplier = (Supplier<BufferedInputStream>) () -> bis;
|
|
|
- HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofInputStream(streamSupplier)).build();
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- return getResult(httpResponse);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ public UploadFileRet putObjectWithJdkHttp(InputStream inputStream, int channelId, int userId) throws Exception {
|
|
|
+ String api = String.format("%s/", endpoint);
|
|
|
+ HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
|
|
|
+ .version(HttpClient.Version.HTTP_1_1)
|
|
|
+ .header("x-content-sha256sum", "1234567890")
|
|
|
+ .header("x-channel-id", channelId+"")
|
|
|
+ .header("x-user-id", userId+"");
|
|
|
+
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(inputStream);
|
|
|
+ Supplier<? extends InputStream> streamSupplier = (Supplier<BufferedInputStream>) () -> bis;
|
|
|
+ HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofInputStream(streamSupplier)).build();
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
+ return getResult(httpResponse);
|
|
|
+ }
|
|
|
|
|
|
- return null;
|
|
|
+ public UploadFileRet postObjectWithJdkHttp(File file, int channelId, long userId) throws Exception {
|
|
|
+ String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
+ MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
|
|
|
+ .addPart("file", Paths.get(file.getAbsolutePath()))
|
|
|
+ .addPart("channelId", ""+channelId)
|
|
|
+ .addPart("client", "client")
|
|
|
+ .addPart("sha256sum", sha256sum);
|
|
|
+
|
|
|
+ String api = String.format("%s/", endpoint);
|
|
|
+ HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
+ .version(HttpClient.Version.HTTP_1_1)
|
|
|
+ .header("x-user-id", userId+"")
|
|
|
+ //.header("authorization", "Bearer 0123456789")
|
|
|
+ .header("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
|
|
|
+ .POST(publisher.build())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
+ return getResult(httpResponse);
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet postObject1(File file, int channelId, long userId) {
|
|
|
- try {
|
|
|
- String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
- MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
|
|
|
- .addPart("file", Paths.get(file.getAbsolutePath()))
|
|
|
- .addPart("channelId", ""+channelId)
|
|
|
- .addPart("client", "client")
|
|
|
- .addPart("sha256sum", sha256sum);
|
|
|
-
|
|
|
- String api = String.format("%s/", endpoint);
|
|
|
- HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
- .version(HttpClient.Version.HTTP_1_1)
|
|
|
- .header("x-user-id", userId+"")
|
|
|
- //.header("authorization", "Bearer 0123456789")
|
|
|
- .header("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
|
|
|
- .POST(publisher.build())
|
|
|
- .build();
|
|
|
-
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- return getResult(httpResponse);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ public UploadFileRet postObject(File file, int channelId, long userId) throws Exception {
|
|
|
+ String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("channelId", ""+channelId);
|
|
|
+ map.put("client", "client");
|
|
|
+ map.put("userId", ""+userId);
|
|
|
+ map.put("sha256sum", sha256sum);
|
|
|
+ UploadParam uploadParam = new UploadParam(file, map);
|
|
|
+
|
|
|
+ String api = String.format("%s/", endpoint);
|
|
|
+ WebResponse webResponse = webRequest.upload(api, uploadParam);
|
|
|
+ int statusCode = webResponse.getStatusCode();
|
|
|
+ String body = webResponse.getBody();
|
|
|
+ if (statusCode != 200) {
|
|
|
+ String errMsg = String.format("%s -> %s", statusCode, body);
|
|
|
+ throw new Exception(errMsg);
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public UploadFileRet postObject(File file, int channelId, long userId) {
|
|
|
- try {
|
|
|
- String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
- map.put("channelId", ""+channelId);
|
|
|
- map.put("client", "client");
|
|
|
- map.put("userId", ""+userId);
|
|
|
- map.put("sha256sum", sha256sum);
|
|
|
- UploadParam uploadParam = new UploadParam(file, map);
|
|
|
-
|
|
|
- String api = String.format("%s/", endpoint);
|
|
|
- WebResponse webResponse = webRequest.upload(api, uploadParam);
|
|
|
- int statusCode = webResponse.getStatusCode();
|
|
|
- String body = webResponse.getBody();
|
|
|
- if (statusCode != 200) {
|
|
|
- log.error("{} -> {}", statusCode, body);
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
|
|
|
- WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
- if (webResult.getCode() != 0) {
|
|
|
- log.error("{}", webResult.getMsg());
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- return webResult.getData();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
|
|
|
+ WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
+ if (webResult.getCode() != 0) {
|
|
|
+ throw new Exception(webResult.getMsg());
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
+ return webResult.getData();
|
|
|
}
|
|
|
|
|
|
- private UploadFileRet getResult(HttpResponse<String> httpResponse) {
|
|
|
+ private UploadFileRet getResult(HttpResponse<String> httpResponse) throws Exception {
|
|
|
int statusCode = httpResponse.statusCode();
|
|
|
String body = httpResponse.body();
|
|
|
if (statusCode != 200) {
|
|
|
- log.error("{} -> {}", statusCode, body);
|
|
|
- return null;
|
|
|
+ String errMsg = String.format("%s -> %s", statusCode, body);
|
|
|
+ throw new Exception(errMsg);
|
|
|
}
|
|
|
|
|
|
Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
|
|
|
WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
if (webResult.getCode() != 0) {
|
|
|
- log.error("{}", webResult.getMsg());
|
|
|
- return null;
|
|
|
+ String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
|
|
|
+ throw new Exception(errMsg);
|
|
|
}
|
|
|
|
|
|
return webResult.getData();
|