After authentication and connection establishment, Upload a zipfile by chunks in java

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:
After authentication and connection establishment, Upload a zipfile by chunks in java
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
After authentication and connection establishmenr, Upload a zipfile by chunks in java
After authentication and connection establishmenr, Upload a zipfile by chunks... 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
Advertisements
After authentication and connection establishmenr, Upload a zipfile by chunks in java
After authentication and connection establishmenr, Upload a zipfile by chunks... 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
Maven dependency for org.finos.legend.engine - legend-engine-xt-relationalStore-executionPlan-connection-authentication version 4.4.4 is released. Learn to use legend-engine-xt-relationalStore-executionPlan-connection-authentication version 4.4.4 in Maven based Java projects
-executionPlan-connection-authentication version 4.4.4 in Java projects. Follow the step... of legend-engine-xt-relationalStore-executionPlan-connection-authentication released...-relationalStore-executionPlan-connection-authentication project have released
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 'pycopy-zipfile'
python with following command: pip install pycopy-zipfile After...ModuleNotFoundError: No module named 'pycopy-zipfile'  Hi, My... named 'pycopy-zipfile' How to remove the ModuleNotFoundError: No module named
ModuleNotFoundError: No module named 'micropython-zipfile'
-zipfile After the installation of micropython-zipfile python library...ModuleNotFoundError: No module named 'micropython-zipfile'  Hi, My... named 'micropython-zipfile' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'micropython-zipfile'
-zipfile After the installation of micropython-zipfile python library...ModuleNotFoundError: No module named 'micropython-zipfile'  Hi, My... named 'micropython-zipfile' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'pycopy-zipfile'
python with following command: pip install pycopy-zipfile After...ModuleNotFoundError: No module named 'pycopy-zipfile'  Hi, My... named 'pycopy-zipfile' How to remove the ModuleNotFoundError: No module named
ModuleNotFoundError: No module named 'pycopy-zipfile'
python with following command: pip install pycopy-zipfile After...ModuleNotFoundError: No module named 'pycopy-zipfile'  Hi, My... named 'pycopy-zipfile' How to remove the ModuleNotFoundError: No module named
ModuleNotFoundError: No module named 'pycopy-zipfile'
python with following command: pip install pycopy-zipfile After...ModuleNotFoundError: No module named 'pycopy-zipfile'  Hi, My... named 'pycopy-zipfile' How to remove the ModuleNotFoundError: No module named
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 'django-zipfile'
python with following command: pip install django-zipfile After...ModuleNotFoundError: No module named 'django-zipfile'  Hi, My... named 'django-zipfile' How to remove the ModuleNotFoundError: No module named
org.finos.legend.engine - legend-engine-xt-relationalStore-executionPlan-connection-authentication version 4.15.6 Maven dependency. How to use legend-engine-xt-relationalStore-executionPlan-connection-authentication version 4.15.6 in pom.xml?
-connection-authentication version 4.15.6 java library in your project. Now you...-relationalStore-executionPlan-connection-authentication Maven dependency? How to use ...-relationalStore-executionPlan-connection-authentication in pom.xml? How to use legend-engine
org.finos.legend.engine - legend-engine-xt-relationalStore-executionPlan-connection-authentication version 4.16.1 Maven dependency. How to use legend-engine-xt-relationalStore-executionPlan-connection-authentication version 4.16.1 in pom.xml?
-connection-authentication version 4.16.1 java library in your project. Now you...-relationalStore-executionPlan-connection-authentication Maven dependency? How to use ...-relationalStore-executionPlan-connection-authentication in pom.xml? How to use legend-engine
ModuleNotFoundError: No module named 'multiprocess-chunks'
-chunks After the installation of multiprocess-chunks python library...ModuleNotFoundError: No module named 'multiprocess-chunks'  Hi, My... named 'multiprocess-chunks' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'multiprocess-chunks'
-chunks After the installation of multiprocess-chunks python library...ModuleNotFoundError: No module named 'multiprocess-chunks'  Hi, My... named 'multiprocess-chunks' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'multiprocess-chunks'
-chunks After the installation of multiprocess-chunks python library...ModuleNotFoundError: No module named 'multiprocess-chunks'  Hi, My... named 'multiprocess-chunks' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'multiprocess-chunks'
-chunks After the installation of multiprocess-chunks python library...ModuleNotFoundError: No module named 'multiprocess-chunks'  Hi, My... named 'multiprocess-chunks' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'redsolutioncms.django-chunks'
command: pip install redsolutioncms.django-chunks After the installation...ModuleNotFoundError: No module named 'redsolutioncms.django-chunks'  ...: No module named 'redsolutioncms.django-chunks' How to remove
ModuleNotFoundError: No module named 'django-chunks'
with following command: pip install django-chunks After...ModuleNotFoundError: No module named 'django-chunks'  Hi, My... 'django-chunks' How to remove the ModuleNotFoundError: No module named
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 Java
Simple 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
dyanamic upload of images using struts without database connection
dyanamic upload of images using struts without database connection  I want to upload images dynamically with out using data base..and want to create image for every user in my db
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 'itertools_chunks_slices'
itertools_chunks_slices After the installation of itertools_chunks_slices...ModuleNotFoundError: No module named 'itertools_chunks_slices'  Hi...: No module named 'itertools_chunks_slices' How to remove the ModuleNotFoundError
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 'itertools_chunks_slices'
itertools_chunks_slices After the installation of itertools_chunks_slices...ModuleNotFoundError: No module named 'itertools_chunks_slices'  Hi...: No module named 'itertools_chunks_slices' How to remove the ModuleNotFoundError
Java FTP Upload
Java 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 connection leak
java connection leak  How to check the connection leak in Java
Java upload file to ftp
Java 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
Simple Java Desktop Upload application
Simple Java Desktop Upload application  I try do simple example for upload applicationtake file from c:\ put to d:\ :) PLEASE HELP
UPLOAD
UPLOAD  how to upload image using html
image upload in java
image 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 Java
FTP 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
connection with MySQL to java.
connection with MySQL to java.   how to connect MySQL database with jsp
login authentication - Java Beginners
login 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 Beginners
Upload 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 Beginners
file 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 example
Java 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 - Struts
java 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 - JDBC
Java-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 connection
Java jdbc connection  Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection
Java Jdbc connection
Java Jdbc connection  What are the steps involved for making a connection with a database or how do you connect to a database
JAVA DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
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
Maven Repository/Dependency: org.finos.legend.engine | legend-engine-xt-relationalStore-executionPlan-connection-authentication
Maven Repository/Dependency of Group ID org.finos.legend.engine and Artifact ID legend-engine-xt-relationalStore-executionPlan-connection-authentication...-executionPlan-connection-authentication dependencies. # Version
connection retry in java
connection 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

Ads