Results 1 to 11 of 11
  1. #1
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default download file/image?

    hello, I use this code/thread to download an image from the internet.
    but The image is unreadable. I used a better method before but I couldnt let it check if it is interrupted, thats why I use this code:
    Java Code:
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class download implements Runnable {
    
        private String url;
        private String fileName;
        private static String newline = System.getProperty("line.separator");
        public boolean succes = false;
    
        public download(String url, String fileName) {
    
            this.url = url;
            this.fileName = fileName;
        }
    
        public void run() {
            URL u;
            InputStream is = null;
            BufferedInputStream dis;
            FileOutputStream writer = null;
            String contents = "";
            char s;
    
            try {
                u = new URL(url);
                is = u.openStream(); 
                dis = new BufferedInputStream(is);
                int i;
                byte[] buffer = new byte[1024];
                while ((i = dis.read(buffer)) != -1) {
                    if (Thread.interrupted()) {
                        dis.close();
                        return;
                    }
                    contents = contents + new String(buffer, 0, i);
                    s = (char) dis.read();
                }
                writer = new FileOutputStream(new File(fileName));
                writer.write(contents.getBytes());
                writer.flush();
                writer.close();
                succes = true;
    
            } catch (MalformedURLException mue) {
            } catch (IOException ioe) {
            } finally {
    
                  try {
                    if (is != null) {
                        is.close();
                    }
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException ioe) {
                }
    
            }
    
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Image data are not Strings so don't try to convert one to the other; use (arrays of) bytes. (and why are you reading an extra byte every loop pass?)

    kind regards,

    Jos

  3. #3
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    I took this code directly from the web, I always avoid using something wih bytes, chars,outputstreams. I never used them and dont understand it.
    can you actually explain this piece of crapy code?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dinosoep View Post
    I took this code directly from the web, I always avoid using something wih bytes, chars,outputstreams. I never used them and dont understand it.
    can you actually explain this piece of crapy code?
    That code tries to read 1024 bytes over and over again; every chunk of bytes is converted to a String and appended to one result String; for reasons that are beyond me an extra byte is read near the end of every loop pass.

    Finally the resulting String is written to a file (converted to one big chunk of bytes again).

    This goes wrong for several reasons: 1024 bytes don't necessarily make up a number of chars in a String; some encoded chars take one, two or more bytes and the total may not end up to be 1024. It can (and does) fail terribly.

    Better immediately write those chunks of bytes to a file after you've read them from the input stream without any conversion at all.

    kind regards,

    Jos

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    I know I look a bit stupid but can you help me?
    its not my homework, just a hobby and I lost 3 days and recieved a lot of frustration. can you help me?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dinosoep View Post
    I know I look a bit stupid but can you help me?
    its not my homework, just a hobby and I lost 3 days and recieved a lot of frustration. can you help me?
    Before you start that while loop open a FileOutputStream and in the body of the loop write as many bytes to the file as you've read just before. After the loop finishes close that FileOutputStream. For a thorough explanation of this all check the Sun Java tutorials; it has an entire section on I/O.

    kind regards,

    Jos

  7. #7
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    thank you, this will be for tomorow, I think

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dinosoep View Post
    thank you, this will be for tomorow, I think
    Good and don't forget to have fun with it.

    kind regards,

    Jos

  9. #9
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    believe it or not; it works :)
    I downloads and it is interuptable.
    thank you

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,960
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Before you start that while loop open a FileOutputStream and in the body of the loop write as many bytes to the file as you've read just before. After the loop finishes close that FileOutputStream. For a thorough explanation of this all check the Sun Java tutorials; it has an entire section on I/O.

    kind regards,

    Jos
    The bit in bold is the bit I always forget...
    I frequently end up writing 1024 bytes (or whatever the buffer length happens to be) out, whatever the value of i. Funnily enough that doesn't work!
    :D

  11. #11
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    Strange, it does the job for me :) :p
    I have another question, should I open another topic?
    Last edited by dinosoep; 12-15-2009 at 10:31 AM.

Similar Threads

  1. Download file from URL?
    By makpandian in forum Networking
    Replies: 0
    Last Post: 03-10-2009, 12:31 PM
  2. Download file from URL?
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 12:30 PM
  3. File Download
    By AnGuRuSO in forum New To Java
    Replies: 9
    Last Post: 10-29-2008, 11:00 AM
  4. image download from server
    By willemjav in forum Java Applets
    Replies: 2
    Last Post: 04-03-2008, 10:13 PM
  5. How to download an image from a HTTP URL
    By Valeriano in forum Networking
    Replies: 1
    Last Post: 05-20-2007, 08:35 PM

Posting Permissions

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