Am trying to upload the large file to the website using java. After authentication, I made use of those cookies to upload the file [My authentication part is done] but Am trying to establish the new connection on every chunk upload [Am not sure whether this idea is correct or not], so the file is not getting uploaded. Could any one pls say where am going wrong in uploading file as chunks. Or Is there any other way to communicate and upload files in an efficient way. (Only using java) ... Seriously thanks you very much for your response...
File file = new File("D:/fileName.gz");
long contentLength = file.length();
String contentLengthString = Long.toString(contentLength);
FileInputStream is = new FileInputStream(file);
int bufferSize = 10485760; // 10 MB = 10485760 bytes
byte[] bytesPortion = new byte[bufferSize];
int byteNumber = 0;
int maxAttempts = 1;
while (is.read(bytesPortion, 0, bufferSize) != -1) {
String contentRange = Integer.toString(byteNumber);
long bytesLeft = contentLength - byteNumber;
System.out.println("\n" + "\n" + "Bytes Left: " + bytesLeft);
if (bytesLeft < bufferSize) {
//copy the bytesPortion array into a smaller array containing only the remaining bytes
bytesPortion = Arrays.copyOf(bytesPortion, (int) bytesLeft);
//This just makes it so it doesn't throw an IndexOutOfBounds exception on the next while iteration. It shouldn't get past another iteration
bufferSize = (int) bytesLeft;
}
byteNumber += bytesPortion.length;
contentRange += "-" + (byteNumber - 1) + "/" + contentLengthString;
int attempts = 0;
boolean success = false;
while (attempts < maxAttempts && !success) {
URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();
// Acts like a browser
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "ihealth-api.xxx.com");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Encoding",
"gzip, deflate");
conn.setRequestProperty("Transfer-Encoding", "chunked");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String cookieString = "";
for (String cookie : this.cookies) {
if(cookieString!=""){
cookieString+="; "+cookie.split(";")[0];
}else{
cookieString=cookie.split(";")[0];
}
}
System.out.println(cookieString);
conn.addRequestProperty("Cookie", cookieString);
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Referer", "https://ihealth.xxx.com/xxx-analyzer/upload");
conn.setRequestProperty("Content-Type","application/gzip");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.addRequestProperty("Content-Range", "bytes%20" + contentRange);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
System.out.println("Data Size"+ bytesPortion);
wr.write(bytesPortion);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Message "+conn.getResponseMessage());
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
attempts++;
}