I am using TUSClient to upload large files in chunks to the server.
Here is my code:
public void uploadFile(UploadFileResponse uploadFileResponse, File file) {
try {
if (System.getProperty("sun.net.http.allowRestrictedHeaders") == null) {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
byte[] bytes = Files.readAllBytes(file.toPath());
Map headers = new HashMap();
headers.put("token", getToken());
headers.put("Content-Length", Integer.toString(bytes.length));
final TusUpload upload = new TusUpload(file);
TusClient client = new TusClient();
client.setUploadCreationURL(new URL(uploadFileResponse.getUploadUrl()));
client.setHeaders(headers);
client.enableResuming(new TusURLMemoryStore());
Integer chunkSize = 1024 * 1024 * 1;
Map metadata = new HashMap();
metadata.put("name", file.getName());
metadata.put("chunkSize", String.format("%d", chunkSize));
metadata.put("contentType", "text/xml");
upload.setMetadata(metadata);
TusExecutor executor = new TusExecutor() {
@Override
protected void makeAttempt() throws IOException, ProtocolException {
TusUploader uploader = client.resumeOrCreateUpload(upload);
uploader.setChunkSize(chunkSize);
int result = 0;
do {
result = uploader.uploadChunk();
} while (result > -1);
uploader.finish();
}
};
executor.makeAttempts();
}catch (Exception e){
throw new customException(e);
}
}
Please consider the server has a Proxy URL to connect to outer world.
uploadFileResponse.getUploadUrl()
Please consider the above statement will return the outer world URL.
How to add Proxy support here.