1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Downloading a file in Java

Discussion in 'Other Languages' started by Elliott1992, Dec 3, 2011.

  1. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
    Is there a way in Java to download a Music file (mp3) or something like that to a specific folder (and specific name)from the internet (I have the download link)?

    If so please can someone post here how? I've searched google and they just save the website source code in a file and don't download the music file.

    Thank you in advance,

    Elliott
     
  2. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
    Anybody help??
     
  3. infTee

    infTee Junior Member

    Joined:
    Mar 2, 2010
    Messages:
    101
    Likes Received:
    97
    Location:
    Ireland
  4. t0m3313

    t0m3313 Regular Member

    Joined:
    Sep 11, 2011
    Messages:
    224
    Likes Received:
    56
    Code:
    import java.net.URL;
    import java.io.*;
    
    public class Download {
    
        public static void main(String[] args){
            
            try{
                URL url = new URL("Put url here");
                File outputFile = new File("output.mp3");
                org.apache.commons.io.FileUtils.copyURLToFile(url, outputFile);
            }catch(Exception ex){
                ex.printStackTrace();
            }
    
        }
    }
    
    I hope that´s helping you ;)

    EDIT: I didn´t test this with an mp3, but with standard html pages this works just fine :)
     
    Last edited: Dec 4, 2011
  5. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
    That one has an error with the org.apache bit...
     
  6. t0m3313

    t0m3313 Regular Member

    Joined:
    Sep 11, 2011
    Messages:
    224
    Likes Received:
    56
    Then you need to download this jar.

    Code:
    commons.apache.org/io/download_io.cgi
    If you´re using eclipse as programming environment, go to Project => Properties => Java Build Path => Libraries => add external jar.

    Don´t know how it works in others as I am using eclipse only, but I think you can work that out ;)
     
    Last edited: Dec 4, 2011
  7. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
    Still doesn't work for Mp3's as it just saves the website and not the actual downloaded item
     
  8. BreakAllTheClocks

    BreakAllTheClocks Regular Member

    Joined:
    Apr 23, 2010
    Messages:
    421
    Likes Received:
    242
    I just tested that using

    "http://www.archive.org/download/huck_finn_librivox/huckfinn_43_twain_apc_64kb.mp3"

    Worked fine.
     
  9. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
  10. t0m3313

    t0m3313 Regular Member

    Joined:
    Sep 11, 2011
    Messages:
    224
    Likes Received:
    56
    Sorry, I don´t know how to do that :( But glad it worked for a .mp3 URL :)
     
  11. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
    Ok cheers don't worry about it. Guess it's not possible
     
  12. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Next time make sure you post more details of what you want on the first post. This is not "how to download an mp3", it s how to rip an mp3 from that specific site. :AR15firin

    Not specifying as many details as possible on your first post when making a request is disrespectful to the people that actually give their time to help you.
     
  13. Elliott1992

    Elliott1992 Junior Member

    Joined:
    Apr 16, 2011
    Messages:
    163
    Likes Received:
    30
    cheers for the helpful post
     
  14. t0m3313

    t0m3313 Regular Member

    Joined:
    Sep 11, 2011
    Messages:
    224
    Likes Received:
    56
    Yes you´re right with that, the post was a bit misleading.

    But it was very nice to find out, how it works ;) I didn´t know it before either, so don´t worry about my time, I learned something too :)
     
    • Thanks Thanks x 1