Results 1 to 12 of 12
- 03-24-2010, 08:11 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
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.
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.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; }
ThanksLast edited by Eranga; 03-25-2010 at 03:01 AM. Reason: added code tags
- 03-24-2010, 10:24 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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.
- 03-25-2010, 03:03 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-25-2010, 10:09 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
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 05:12 PM. Reason: made correction in code
- 03-25-2010, 10:53 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags next time.
- 03-25-2010, 10:55 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you downloading a text file (doc file) as a flv?
- 03-25-2010, 05:20 PM #7
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
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
- 03-25-2010, 05:31 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's much better lalit. Just for interest, if you change of that byte array from 1024 to some other value what would happen?
- 03-25-2010, 05:38 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
1024 means reads 1024 bytes from InputStrem if you will change this to x . Then it will try to read x bytes from InputStream.
- 03-26-2010, 02:50 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yeah that's true. So why can't you change it. I mean what's the advantage/dis-advantage doing that.
- 03-26-2010, 10:44 AM #11
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
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
- 03-26-2010, 12:27 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Downloading a jar file
By jojo in forum Advanced JavaReplies: 2Last Post: 12-15-2009, 11:04 PM -
Downloading a file from web server
By rlaknar in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-13-2009, 09:15 AM -
"request entity too large" when downloading a 2mb file
By arnab321 in forum CLDC and MIDPReplies: 4Last Post: 11-06-2008, 04:34 PM -
Write access for downloading file.
By Agri in forum Java AppletsReplies: 5Last Post: 09-30-2008, 02:22 AM -
Downloading part of a file with java? What have I done wrong?
By JavaHead08 in forum NetworkingReplies: 1Last Post: 05-30-2008, 10:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks