Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default problem in downloading file from a URL.

    Hi All,

    I have a file on my local server tomcat.

    I am trying to save this file to different location in my computer using a servlet . Servlet reads the file from the URL and writes to some location.
    Here is the code.

    Java Code:
          HttpURLConnection connection = null;
    	   
    	    //OutputStreamWriter wr = null;
            BufferedReader rd  = null;
            StringBuilder sb = null;
            String line = null;
          
            URL serverAddress = null;
          
            try {
            	
            	
                serverAddress = new URL("http://localhost:8080/data/video.flv");
                //set up out communications stuff
                connection = null;
              
                //Set up the initial connection
                connection = (HttpURLConnection)serverAddress.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.setReadTimeout(10000);
                          
                connection.connect();
                      
              
                rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                sb = new StringBuilder();
                
                Writer output = new BufferedWriter(new FileWriter("/home/lalit/file.flv"));
                
                System.out.println(connection.getContentLength());    
                int length = connection.getContentLength();        	
                
               int contentLength = connection.getContentLength();   
               char buffer[] = new char[contentLength];  
               output.write(buffer, 0, contentLength); 	
            	       
                
                
                output.close();
                          
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally
            {
           
                rd = null;           
                connection = null;
            }
    This successfully saves file at specified location but i am not able to open this file. I tried different file formats flv, 3GP, doc etc.


    Thanks
    Last edited by Eranga; 03-25-2010 at 04:01 AM. Reason: added code tags

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    don't use Writers and Readers: they're for characters. Use InputStream and OutputStream, and their read (byte[] bytes) kind of methods, and you should be fine.

    When you use Readers, they convert the in-coming bytes into characters according to some character encoding, and then convert the characters back to bytes when writing to the disk... and messing up your bytes along the way.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Simple what iluxa says is in the way you download the file, the format is changed. Not it the correct format to open it and so on.

  4. #4
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    Thanks iluxa & eranga
    Now it's working file
    Here is my updated code.

    Java Code:
    {
    		
    	    HttpURLConnection connection = null;
    	   
    	   
            InputStream isR = null;
         
            URL serverAddress = null;
          
            try {
            	
            	
                serverAddress = new URL("http://localhost:8080/data/file.doc");
                //set up out communications stuff
                connection = null;
              
                //Set up the initial connection
                connection = (HttpURLConnection)serverAddress.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.setReadTimeout(10000);
                          
                connection.connect();
          
               // rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                isR = connection.getInputStream();
              
                File file = new File("/home/lalit/file.doc");
                FileOutputStream foS =  new FileOutputStream(file);
                
           
                System.out.println(connection.getContentLength());
            	
          
            	long contentLength = connection.getContentLength();  
            	
            	int read = 1024;
            
            	byte buffer[] = new byte[read];  
           
            	int readBytes = 0;
            	while ((isR != null) && ((readBytes = isR.read(buffer)) != -1)) {
    				foS.write(buffer, 0, readBytes);
    			}
            	
            	
            	
                isR.close();
                foS.close();
                          
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally
            {
                //close the connection, set all objects to null
                connection.disconnect();
               connection = null;
            }
            
         
    	}
    Last edited by lalit.wientech; 03-25-2010 at 06:12 PM. Reason: made correction in code

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    hi eranga
    i will keep in mind for code.
    No i am not trying to convert doc to flv.
    I was just trying to test this for different file formats. I have modified the mistake in post.

    Thanks
    Lalit

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's much better lalit. Just for interest, if you change of that byte array from 1024 to some other value what would happen?

  9. #9
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    1024 means reads 1024 bytes from InputStrem if you will change this to x . Then it will try to read x bytes from InputStream.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yeah that's true. So why can't you change it. I mean what's the advantage/dis-advantage doing that.

  11. #11
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    hi eranga,

    nice question.

    We can change this value but it should not exceed the range of Integer.

    I think that

    byte buffer[] = new byte[1024];
    with this value OS tries to load 1024 bytes in buffer.
    If you have so smaller size say 100 then it will take more time in reading and writing.
    It is important that you should increase this value depending upon your availiable memory, capacity of butter etc.

    Please correct if i am wrong.

    Thanks

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by lalit.wientech View Post
    It is important that you should increase this value depending upon your availiable memory, capacity of butter etc.
    Exactly, that's what I want to point to you. We have to thing about lots of things with the programming limitations, such as primitive types range and so on.

Similar Threads

  1. Downloading a jar file
    By jojo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2009, 12:04 AM
  2. Downloading a file from web server
    By rlaknar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2009, 10:15 AM
  3. "request entity too large" when downloading a 2mb file
    By arnab321 in forum CLDC and MIDP
    Replies: 4
    Last Post: 11-06-2008, 05:34 PM
  4. Write access for downloading file.
    By Agri in forum Java Applets
    Replies: 5
    Last Post: 09-30-2008, 03:22 AM
  5. Replies: 1
    Last Post: 05-30-2008, 11:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •