How to use Java to download a mp3 file online?

后端 未结 3 823
时光说笑
时光说笑 2021-02-06 12:11

I used the following method to download an mp3 file at : http://online1.tingclass.com/lesson/shi0529/43/32.mp3

But I got the following error :

java.io.FileNotFou

3条回答
  •  既然无缘
    2021-02-06 12:44

    Using old-school Java IO, but you can map this to the NIO method you are using. Key thing is use of URLConnection.

        URLConnection conn = new URL("http://online1.tingclass.com/lesson/shi0529/43/32.mp3").openConnection();
        InputStream is = conn.getInputStream();
    
        OutputStream outstream = new FileOutputStream(new File("/tmp/file.mp3"));
        byte[] buffer = new byte[4096];
        int len;
        while ((len = is.read(buffer)) > 0) {
            outstream.write(buffer, 0, len);
        }
        outstream.close();
    

提交回复
热议问题