Results 1 to 6 of 6
  1. #1
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default URL Reading question

    Greetings i am trying to create something like a download manager for practice, but i am stack in the URL reading part where i have to convert my succesfull url reading to bytes, i am using the example from sun tutorials so basicaly what i need is to convert the bufferedreader ( in ) to the binary executable i am downloading.


    URL yahoo = new URL("http://localhost/Duck.exe");
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    yahoo.openStream()));

    String inputLine; <-- guess this goes out

    while ((inputLine = in.readLine()) != null) <-- guess this goes out
    System.out.println(inputLine); <-- guess this goes out

    in.close();

  2. #2
    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

    Do you want to download any file type?

    You can read the file into a byte array and then it write back to a local file.

  3. #3
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    preferably any file.. not only EXEs but i wouldn't mind if its only .exes

  4. #4
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    anything, anyone?

  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

    Default

    Did you try what I say? Read the content in specified size into a byte array and write back to a local file.

  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

    Default

    Read the following code.

    Java Code:
    import java.io.*;
    
    public class ImageDownloader {
    
        public static void main(String args[]) throws IOException {
    
            BufferedInputStream in = new BufferedInputStream(
                    new java.net.URL("http://192.168.2.130/images/arr0w_down.gif").openStream());
            FileOutputStream fos = new FileOutputStream("img.gif");
            BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
            byte data[] = new byte[1024];
            while (in.read(data, 0, 1024) >= 0) {
                bout.write(data);
            }
            bout.close();
            in.close();
        }
    }

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Replies: 11
    Last Post: 11-03-2009, 06:32 PM
  3. Replies: 5
    Last Post: 08-21-2009, 12:03 PM
  4. [SOLVED] File reading question
    By wiz0r in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 02:21 AM
  5. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 06:48 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
  •