下载图片:
public class URLTest {
public static void main(String[] args) throws Exception {
//要复制图片的地址
URL url = new URL("http://pics4.baidu.com/feed/500fd9f9d72a60596fe549051190049c023bba67.jpeg?token=db66af7efb7371437559b66676578fe3");
// //连接到这个资源
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//获取输入流
InputStream inputStream = urlConnection.getInputStream();
//写到哪个文件的输出流
FileOutputStream fos = new FileOutputStream("picture.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer))!= -1){
fos.write(buffer,0,len);
}
}
下载音乐:
public class URLTest { public static void main(String[] args) throws Exception { //要复制音乐的地址 URL url = new URL("http://ss.sycdn.kuwo.cn/d8a9b1edb2718f66ca6a923b5975ba98/60c22e79/resource/n3/99/15/3712425278.mp3"); // //连接到这个资源 HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //获取输入流 InputStream inputStream = urlConnection.getInputStream(); //写到哪个文件的输出流 FileOutputStream fos = new FileOutputStream("无羁.mp3"); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer))!= -1){ fos.write(buffer,0,len); } } }