Java 根據(jù)url下載網(wǎng)絡(luò)資源
URL:統(tǒng)一資源定位符
例如:https://www.baidu.com
URL【統(tǒng)一資源定位符】:定位資源的,定位互聯(lián)網(wǎng)上的某一個(gè)資源。
DNS 域名解析:www.baidu.com【某一域名】指向 39.156.69.79【某一網(wǎng)站空間IP】
URL 組成:協(xié)議://ip地址:端口/項(xiàng)目名/資源
package lesson04;import java.net.MalformedURLException;import java.net.URL;/** * URL:統(tǒng)一資源定位符 */public class URLDemo1 { public static void main(String[] args) throws MalformedURLException { URL url = new URL('http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123'); //協(xié)議名 System.out.println(url.getProtocol()); //主機(jī)名-主機(jī)ip System.out.println(url.getHost()); //端口 System.out.println(url.getPort()); //地址-文件路徑 System.out.println(url.getPath()); //文件-全路徑 System.out.println(url.getFile()); //查詢部分-參數(shù) System.out.println(url.getQuery()); }}
URL下載資源
package lesson04;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;/** * URL下載資源 */public class URLDown { public static void main(String[] args) throws Exception { //1、下載地址 URL url = new URL('https://m10.music.126.net/20201121104035/2c5eb73ce4421a090b62647f6c486e2c/yyaac/obj/wonDkMOGw6XDiTHCmMOi/3625445007/7d37/4109/d0ef/d56e8176f5789a4e6f8b2173ce500bf6.m4a'); //2、連接到這個(gè)資源 HTTP HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream('f6.m4a'); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1){ fos.write(buffer, 0, len); //寫出這個(gè)數(shù)據(jù) } //關(guān)閉 fos.close(); inputStream.close(); //斷開連接 urlConnection.disconnect(); }}
效果一覽
以上就是Java 根據(jù)url下載網(wǎng)絡(luò)資源的詳細(xì)內(nèi)容,更多關(guān)于Java 下載網(wǎng)絡(luò)資源的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python全棧開發(fā)語法總結(jié)2. Python調(diào)用接口合并Excel表代碼實(shí)例3. Python如何批量生成和調(diào)用變量4. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖5. 如何在Python項(xiàng)目中引入日志6. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效7. python b站視頻下載的五種版本8. Python快速將ppt制作成配音視頻課件的操作方法9. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)10. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析
