Added new implementation upload huge file

This commit is contained in:
Sandro La Bruzzo 2023-06-22 17:43:53 +02:00
parent 5d0d14528f
commit d472050ad4
2 changed files with 39 additions and 42 deletions

View File

@ -53,6 +53,10 @@
<artifactId>dump-schema</artifactId>
<version>1.2.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>

View File

@ -1,22 +1,23 @@
package eu.dnetlib.dhp.oa.graph.dump;
import java.io.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHeaders;
import org.apache.http.entity.ContentType;
import com.google.gson.Gson;
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModel;
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModelList;
import okhttp3.*;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class ZenodoAPIClient2 implements Serializable {
String urlString;
@ -93,48 +94,40 @@ public class ZenodoAPIClient2 implements Serializable {
public int uploadIS2(InputStream is, String fileName) throws IOException {
final String crlf = "\r\n";
final String twoHyphens = "--";
final String boundary = "*****";
final URL url = new URL(bucket + "/" + fileName);
HttpURLConnection httpUrlConnection = null;
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);
HttpPut put = new HttpPut(bucket + "/" + fileName);
put.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip");
put.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
put.setEntity(new InputStreamEntity(is));
int statusCode;
try (CloseableHttpClient client = HttpClients.createDefault()){
CloseableHttpResponse response = client.execute(put);
statusCode= response.getStatusLine().getStatusCode();
httpUrlConnection.setRequestMethod("PUT");
httpUrlConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/zip");
httpUrlConnection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(
httpUrlConnection.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" +
fileName + "\";filename=\"" +
fileName + "\"" + crlf);
request.writeBytes(crlf);
byte[] buf = new byte[8192];
int length;
while ((length = is.read(buf)) != -1) {
request.write(buf, 0, length);
request.flush();
}
request.flush();
request.close();
int responseCode = httpUrlConnection.getResponseCode();
if(! checkOKStatus(responseCode)){
throw new IOException("Unexpected code " + responseCode + getBody(httpUrlConnection));
}
return responseCode;
if(! checkOKStatus(statusCode)){
throw new IOException("Unexpected code " + statusCode );
}
return statusCode;
}
/**