下载一首歌
程序员文章站
2022-03-04 17:40:16
...
package URLLoadDown;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class LoadDown {
public static void main(String[] args) {
try {
//1.下载地址
URL url = new URL("https://audio04.dmhmusic.com/71_53_T10062028738_128_4_4_0_sdk-cpm/cn/0103/M00/78/CD/ChR45WFFlYmADtB8AAdWqAeDULU575.mp3?xcode=b4838506b4a317418f03fb92bf9f472e720ee1d");
//2.连接到这个资源
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("575.mp3");
byte[] buffer = new byte[512];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注:方法从狂神说Java学得。
下一篇: 一天一首歌