Java實現(xiàn)網(wǎng)絡(luò)資源的單線程下載
一、題目描述
題目實現(xiàn):在一個線程中完成網(wǎng)絡(luò)資源的下載。
二、解題思路
創(chuàng)建一個類:SingleThreadDownloadFrame,繼承JFrame窗體類。
定義一個download()方法:用于從指定網(wǎng)址下載文件
使用URLConnection類的getInputStream()方法 獲取網(wǎng)頁資源的輸入流對象。
獲得完整路徑,截取路徑,獲得路徑中最后一個斜杠的位置當(dāng)文件名
從輸入流中讀取內(nèi)容,寫到本地文件中。
測試下載這個鏈接
三、代碼詳解
SingleThreadDownloadFrame
package com.xiaoxuzhu; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; /** * Description: 在一個線程中完成網(wǎng)絡(luò)資源的下載 * * @author xiaoxuzhu * @version 1.0 * * <pre> * 修改記錄: * 修改后版本 修改人 修改日期 修改內(nèi)容 * 2022/5/24.1 xiaoxuzhu 2022/5/24 Create * </pre> * @date 2022/5/24 */ public class SingleThreadDownloadFrame extends JFrame { private JTextField tf_address; /** * Launch the application * @param args */ public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { SingleThreadDownloadFrame frame = new SingleThreadDownloadFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame */ public SingleThreadDownloadFrame() { super(); getContentPane().setLayout(null); setTitle("網(wǎng)絡(luò)資源的單線程下載"); setBounds(100, 100, 500, 237); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel(); label.setText("網(wǎng)絡(luò)資源的網(wǎng)址:"); label.setBounds(10, 88, 118, 18); getContentPane().add(label); tf_address = new JTextField(); tf_address.setBounds(117, 86, 357, 22); getContentPane().add(tf_address); final JButton button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { String address = tf_address.getText().trim();// 獲得網(wǎng)址 download(address); // 下載文件 } }); button.setText("單擊開始下載"); button.setBounds(41, 144, 145, 28); getContentPane().add(button); final JButton button_1 = new JButton(); button_1.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { tf_address.setText(null);// 清除文本框內(nèi)容 tf_address.requestFocus();// 文本框獲得焦點 } }); button_1.setText("清 空"); button_1.setBounds(204, 144, 106, 28); getContentPane().add(button_1); final JButton button_2 = new JButton(); button_2.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { System.exit(0); } }); button_2.setText("退 出"); button_2.setBounds(328, 144, 106, 28); getContentPane().add(button_2); final JLabel label_1 = new JLabel(); label_1.setForeground(new Color(0, 0, 255)); label_1.setFont(new Font("", Font.BOLD, 24)); label_1.setText("網(wǎng)絡(luò)資源的單線程下載"); label_1.setBounds(117, 21, 301, 48); getContentPane().add(label_1); } public void download(String urlAddr){ // 從指定網(wǎng)址下載文件 try { URL url = new URL(urlAddr); // 創(chuàng)建URL對象 URLConnection urlConn = url.openConnection(); // 獲得連接對象 urlConn.connect(); // 打開到url引用資源的通信鏈接 InputStream in = urlConn.getInputStream() ; // 獲得輸入流對象 String filePath = url.getFile(); // 獲得完整路徑 int pos = filePath.lastIndexOf("/"); // 獲得路徑中最后一個斜杠的位置 String fileName = filePath.substring(pos+1); // 截取文件名 FileOutputStream out = new FileOutputStream("D:/"+fileName); // 創(chuàng)建輸出流對象 byte[] bytes = new byte[1024]; // 聲明存放下載內(nèi)容的字節(jié)數(shù)組 int len = in.read(bytes); // 從輸入流中讀取內(nèi)容 while (len != -1){ out.write(bytes,0,len); // 將讀取的內(nèi)容寫到輸出流 len = in.read(bytes); // 繼續(xù)從輸入流中讀取內(nèi)容 } out.close(); // 關(guān)閉輸出流 in.close(); // 關(guān)閉輸入流 JOptionPane.showMessageDialog(null, "下載完畢"); } catch (Exception e) { e.printStackTrace(); } } }
到此這篇關(guān)于Java實現(xiàn)網(wǎng)絡(luò)資源的單線程下載的文章就介紹到這了,更多相關(guān)Java資源單線程下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 實戰(zhàn)練手項目之醫(yī)院預(yù)約掛號系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Maven+Vue+mysql實現(xiàn)一個醫(yī)院預(yù)約掛號系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11idea創(chuàng)建SpringBoot項目及注解配置相關(guān)應(yīng)用小結(jié)
Spring Boot是Spring社區(qū)發(fā)布的一個開源項目,旨在幫助開發(fā)者快速并且更簡單的構(gòu)建項目,Spring Boot框架,其功能非常簡單,便是幫助我們實現(xiàn)自動配置,本文給大家介紹idea創(chuàng)建SpringBoot項目及注解配置相關(guān)應(yīng)用,感興趣的朋友跟隨小編一起看看吧2023-11-11Java調(diào)用shell腳本解決傳參和權(quán)限問題的方法
今天小編就為大家分享一篇關(guān)于Java調(diào)用shell腳本解決傳參和權(quán)限問題的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例
這篇文章主要介紹了SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Springboot整合Mybatis和SQLite的詳細(xì)過程
這篇文章主要介紹了Springboot整合Mybatis和SQLite的詳細(xì)過程,本文通過圖文示例相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07spring boot 本地圖片不能加載(圖片路徑)的問題及解決方法
這篇文章主要介紹了spring boot 本地圖片不能加載(圖片路徑)的問題,解決的辦法其實很簡單,只要寫一個配置文件,也就是圖片位置的轉(zhuǎn)化器,原理是虛擬一個在服務(wù)器上的文件夾,與本地圖片的位置進(jìn)行匹配。需要的朋友可以參考下2018-04-04SpringBoot如何優(yōu)雅的處理重復(fù)請求
對于一些用戶請求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復(fù)了,可能會導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請求的方法,需要的朋友可以參考下2023-12-12