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數(shù)組擴容縮容與拷貝的實現(xiàn)和原理
這篇文章主要帶大家學(xué)習(xí)數(shù)組的擴容、縮容及拷貝,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
SpringBoot如何使用RequestBodyAdvice進行統(tǒng)一參數(shù)處理
這篇文章主要介紹了SpringBoot使用RequestBodyAdvice進行統(tǒng)一參數(shù)處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java的wait(), notify()和notifyAll()使用心得
本篇文章是對java的 wait(),notify(),notifyAll()進行了詳細的分析介紹,需要的朋友參考下2013-08-08
Java詳解ScriptEngine接口動態(tài)執(zhí)行JS腳本
ScriptEngine是基本接口,其方法必須在本規(guī)范的每個實現(xiàn)中完全起作用。這些方法提供基本腳本功能。 寫入這個簡單接口的應(yīng)用程序可以在每個實現(xiàn)中進行最少的修改。 它包括執(zhí)行腳本的方法,以及設(shè)置和獲取值的方法2022-08-08
Spring Core動態(tài)代理的實現(xiàn)代碼
通過JDK的Proxy方式或者CGLIB方式生成代理對象的時候,相關(guān)的攔截器已經(jīng)配置到代理對象中去了,接下來通過本文給大家介紹Spring Core動態(tài)代理的相關(guān)知識,需要的朋友可以參考下2021-10-10

