Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners After authentication and connection establishmenr, Upload a zipfile by chunks in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Hari Prasanth
After authentication and connection establishmenr, Upload a zipfile by chunks in java
0 Answer(s)      a year and 2 months ago
Posted in : Java Beginners


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++;
            }

Advertisement
View Answers

Related Tutorials/Questions & Answers:
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
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 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
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
login authentication - Java Beginners
login authentication  i 've problem in authenticating users thru jsp and java bean. cud u pls help me. here is the bean and jsp code samples. after... to successloging java bean code -------------- public void
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
send without authentication mail using java - JavaMail
send without authentication mail using java  Dear All, I am tring to send simple mail without authentication using java. is it possible, could i send email without authentication. I have written following code. public
Authentication - JavaMail
Authentication program in Java  How to write a program in Java for user authentication
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: The java.util.zip.ZipFile class extends
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
java connection leak
java connection leak  How to check the connection leak in Java
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
connection with MySQL to java.
connection with MySQL to java.   how to connect MySQL database with jsp
Java Glossary : Authentication
Java Glossary : Authentication       Authentication is the process of ensuring to some in order... key.  A web authentication where a web page includes a dialog box having
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 DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
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 jdbc connection
Java jdbc connection  Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection
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 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
Java FTP Connection
Java FTP Connection  I am learning to develop a program in Java. As a learning process I want to make a connection to the FTP server just to check if it works? How to make Java FTP Connection to FTP server hosted in my network
Java-Connection Pool - JDBC
Java-Connection Pool  How can I create a connection pool. My database... is Weblogic 8. Which is the best approach to create a connection pool? ...{ public static void main(String[] args){ Connection con = null; String url
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
Java Connection to Oracle database
Java Connection to Oracle database  HI I am ubable to connnect jdbc to Oracle database.i had gone to control panel >>Administrative... type properly and the Test Connection failed.So Kindly tell me what to type in TNS
Connection pooling - Java Beginners
Connection pooling  Respected Sir/Madam, I am Ragavendran.R.. I need some clarification on what connection pooling is? Moreover can u provide me an example of how to implement connection pooling concept
Authentication in Servlets
Authentication in Servlets  What are different Authentication options available in Servlets
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
Connection pooling - Java Beginners
Connection pooling  Respected Sir/Madam, I am Ragavendran.R.. I got your link which u have sent for implementing connection pooling... for implementing connection pooling for the below mentioned: Ahead.jsp: Client
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
Use of size and getInputStream method of ZipFile.
Use of size and getInputStream method of ZipFile. In this tutorial, We...;are available in ZipFile class. The ZipFile class is used to read entries from zip files.  In this Example, The size() method of ZipFile class returns number
java database connection - JDBC
java database connection  sir i want to join my project with MS access database. i am making my project in netbeans.please tell me the coding to do...{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connect
 

 

 

DMCA.com