URL網(wǎng)絡(luò)資源下載

package com.studying.JavaNet.DownloadWeb;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
public class download2 {
? ?public static void main(String[] args) throws Exception {
? ? ? ?//改成你想要的資源,啥都可
? ? ? ?URL url = new URL("http://localhost:8080/MasterXu/darkFile.txt");
? ? ? ?//連接
? ? ? ?HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
? ? ? ?//下載
? ? ? ?InputStream is = urlConnection.getInputStream();
? ? ? ?//下載位置
? ? ? ?FileOutputStream fos = new FileOutputStream("darkFile.txt");
? ? ? ?//讀取
? ? ? ?byte[] buffer = new byte[1024];
? ? ? ?int len;
? ? ? ?while((len=is.read(buffer))!=-1)
? ? ? ?{
? ? ? ? ? ?fos.write(buffer,0,len);//寫入數(shù)據(jù)
? ? ? ?}
? ? ? ?fos.close();
? ? ? ?is.close();
? ? ? ?urlConnection.disconnect();//斷開連接
? ?}
}