After authentication and connection establishment, Upload a zipfile by chunks in java
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++;
}
View Answers
Related Tutorials/Questions & Answers:
Advertisements
ModuleNotFoundError: No module named 'chunks':
pip install
chunks
After the installation of
chunks python library...ModuleNotFoundError: No module named '
chunks' Hi,
My Python... '
chunks'
How to remove the ModuleNotFoundError: No module named '
chunks'
ModuleNotFoundError: No module named 'updatable-zipfile'
After the installation of updatable-
zipfile python library, ModuleNotFoundError...ModuleNotFoundError: No module named 'updatable-
zipfile' Hi,
My... named 'updatable-
zipfile'
How to remove the ModuleNotFoundError: No module
ModuleNotFoundError: No module named 'io-chunks' command:
pip install io-
chunks
After the installation of io-
chunks...ModuleNotFoundError: No module named 'io-
chunks' Hi,
My Python...-
chunks'
How to remove the ModuleNotFoundError: No module named 'io
Simple FTP upload in JavaSimple FTP
upload in Java How to write a program for Simple FTP
upload in
Java? Any code example of Simple FTP
upload in
Java will be very helpful for me.
Thanks
ModuleNotFoundError: No module named 'django-cms-chunks'
After the installation of django-cms-
chunks python library, ModuleNotFoundError...ModuleNotFoundError: No module named 'django-cms-
chunks' Hi,
My... named 'django-cms-
chunks'
How to remove the ModuleNotFoundError: No module
ModuleNotFoundError: No module named 'django-cms-chunks'
After the installation of django-cms-
chunks python library, ModuleNotFoundError...ModuleNotFoundError: No module named 'django-cms-
chunks' Hi,
My... named 'django-cms-
chunks'
How to remove the ModuleNotFoundError: No module
Java FTP UploadJava FTP Upload Is there any example code for
Java FTP
Upload? Which is the easy to use
Java FTP
Upload example program at roseindia.net?
Thanks... in
Java.
View the FTP Server :
Upload file example for complete code example
Java upload file to ftpJava upload file to ftp Hi,
How to uploaded file to a ftp server in
Java?
Thanks
Hi,
It's very easy to write a program in
Java....
View complete example at FTP File
Upload in
Java
Thanks
How to search entry in ZipFile.How to search entry in
ZipFile.
In this tutorial, We will discuss the use of getEntry() method. The getEntry()
methods are available in
ZipFile... in it.
About
ZipFile API:ADS_TO_REPLACE_1
The java.util.zip.ZipFile
UPLOADUPLOAD how to
upload image using html
image upload in javaimage
upload in java Hi, I am working with
java. In my application i want to give facility to user to add and change image. I use open dialog box to select image, it will work properly i.e on button click open dialog is open
FTP File Upload in JavaFTP File
Upload in
Java
This tutorial shows you how you can write
Java program... easy to write code for FTP File
Upload in
Java. I am assuming that you have FTP... a
connection to FTP Server and perform
upload/download/move/delete operations
login authentication - Java Beginnerslogin authentication i 've designed the jsp page for login. now i need to connect it with the bean and do
authentication for the login for the "register" table in mysql. can anybody help me with the code immediately
Upload photo - Java BeginnersUpload photo Morning,
Can you tell me coding how to
upload photo...);
Action uploadAction = new UploadAction("
Upload");
Action browseAction = new...");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test
file upload in jsp - Java Beginnersfile
upload in jsp how to
upload a file using jsp. my operating...:
Display file
upload form to the user
UPLOAD THE FILE
Choose the file To
Upload:
2
Java FTP file upload exampleJava FTP file
upload example Where I can find
Java FTP file
upload...; Hi,
We have many examples of
Java FTP file
upload. We are using Apache FTPClient for creating FTP based program in
Java.
See the complete list of FTP
java file upload in struts - Strutsjava file
upload in struts i need code for
upload and download file using struts in flex.plese help me Hi Friend,
Please visit the following links:
http://www.roseindia.net/struts/strutsfileupload.shtml
http
Java-Connection Pool - JDBCJava-
Connection Pool How can I create a
connection pool. My database is MS SQL server 2000(username-sa, pwd-admin) and my application server is Weblogic 8. Which is the best approach to create a
connection pool? Kindly help
Java jdbc connectionJava jdbc connection Does the JDBC-ODBC Bridge support multiple concurrent open statements per
connection Java Jdbc connectionJava Jdbc connection What are the steps involved for making a
connection with a database or how do you connect to a database
how to upload and download images in java?how to
upload and download images in
java? what is the code for uploading and downloading images in
java?
how do I make a photo gallery through JSP....
Upload and Download images:
1)page.jsp:
<%@ page language="
java connection retry in javaconnection retry in java How to retry the
connection automatically once it get disconnect..?
Connection connection = null;
while (
connection == null) {
try {
connection = DriverManager.getConnection(url
Login authentication & mysql - Java Beginners Authentication with MySql using
Java Swing:
1)Create a
java class...Login
authentication & mysql Hi ,
Could you guide or provide sample coding for the following scenerio.
I need to code
authentication for user