Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼_第1頁
Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼_第2頁
Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼_第3頁
Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼_第4頁
Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能的示例代碼目錄一、題目描述二、解題思路三、代碼詳解

一、題目描述

題目實(shí)現(xiàn):網(wǎng)絡(luò)資源的斷點(diǎn)續(xù)傳功能。

二、解題思路

獲取要下載的資源網(wǎng)址

顯示網(wǎng)絡(luò)資源的大小

上次讀取到的字節(jié)位置以及未讀取的字節(jié)數(shù)

輸入下載的起始位置和結(jié)束位置,開始下載網(wǎng)絡(luò)資源

如果沒有下載完成,可以接著上次的下載位置繼續(xù)下載

創(chuàng)建一個(gè)類:BreakPointSuperveneFrame,繼承JFrame窗體類。

定義一個(gè)download()方法:用于實(shí)現(xiàn)網(wǎng)絡(luò)資源的斷點(diǎn)續(xù)傳。

核心重點(diǎn):通過設(shè)置請(qǐng)求參數(shù)RANGE實(shí)現(xiàn),通過RANGE參數(shù)可以指定下載網(wǎng)絡(luò)的字節(jié)區(qū)間,從而可以實(shí)現(xiàn)每次下載部分網(wǎng)絡(luò)資源的功能

例如:將該參數(shù)設(shè)置為RANGEbytes=0-1024,就表示將網(wǎng)絡(luò)資源中從0~1024之間的內(nèi)容下載到客戶機(jī);如果將該參數(shù)設(shè)置為RANGE:bytes=1024-,就表示將網(wǎng)絡(luò)資源中從1024到結(jié)束位置的內(nèi)容全部下載到客戶機(jī)。

測(cè)試下載這個(gè)鏈接

三、代碼詳解

BreakPointSuperveneFrame

packagecom.xiaoxuzhu;

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.EventQueue;

importjava.awt.Font;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

import.HttpURLConnection;

import.MalformedURLException;

import.URL;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JTextField;

*Description:

*@authorxiaoxuzhu

*@version1.0

*pre

*修改記錄:

*修改后版本修改人修改日期修改內(nèi)容

*2025/5/29.1xiaoxuzhu2025/5/29Create

*/pre

*@date2025/5/29

publicclassBreakPointSuperveneFrameextendsJFrame{

privateJTextFieldtf_totalLength;

privateJTextFieldtf_residuaryLength;

privateJTextFieldtf_readToPos;

privateJTextFieldtf_address;

privateJTextFieldtf_endPos;

privateJTextFieldtf_startPos;

privateStringurlAddress="";//用于存儲(chǔ)網(wǎng)絡(luò)資源的地址

privatelongtotalLength=0;//存儲(chǔ)網(wǎng)絡(luò)資源的大小,以字節(jié)為單位

privatelongreadToPos=0;//存儲(chǔ)上次讀取到的位置

privatelongresiduaryLength=0;//存儲(chǔ)未讀內(nèi)容的大小

*Launchtheapplication

*@paramargs

publicstaticvoidmain(Stringargs[]){

EventQueue.invokeLater(newRunnable(){

publicvoidrun(){

try{

BreakPointSuperveneFrameframe=newBreakPointSuperveneFrame();

frame.setVisible(true);

}catch(Exceptione){

e.printStackTrace();

*Createtheframe

publicBreakPointSuperveneFrame(){

super();

getContentPane().setLayout(null);

setTitle("下載網(wǎng)絡(luò)資源的斷點(diǎn)續(xù)傳");

setBounds(100,100,514,238);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tf_startPos=newJTextField();

tf_startPos.setBounds(80,165,113,22);

getContentPane().add(tf_startPos);

finalJLabellabel=newJLabel();

label.setText("起始位置:");

label.setBounds(10,167,74,18);

getContentPane().add(label);

finalJLabellabel_1=newJLabel();

label_1.setText("結(jié)束位置:");

label_1.setBounds(199,167,74,18);

getContentPane().add(label_1);

tf_endPos=newJTextField();

tf_endPos.setBounds(267,165,117,22);

getContentPane().add(tf_endPos);

finalJLabellabel_2=newJLabel();

label_2.setText("網(wǎng)絡(luò)資源的地址:");

label_2.setBounds(10,52,113,18);

getContentPane().add(label_2);

tf_address=newJTextField();

tf_address.addActionListener(newActionListener(){

publicvoidactionPerformed(finalActionEvente){

try{

urlAddress=tf_address.getText().trim();

URLurl=newURL(urlAddress);//獲得網(wǎng)絡(luò)資源的URL

HttpURLConnectionconnection=(HttpURLConnection)url

.openConnection();//獲得連接對(duì)象

connection.connect();//連接網(wǎng)絡(luò)資源

totalLength=connection.getContentLength();//獲得網(wǎng)絡(luò)資源的長度

connection.disconnect();//斷開連接

tf_totalLength.setText(String.valueOf(totalLength));//顯示總長度

tf_readToPos.setText("0");//顯示上次讀取到的位置

residuaryLength=totalLength;//未讀內(nèi)容為文件總長度

tf_residuaryLength.setText(String.valueOf(residuaryLength));//顯示未讀內(nèi)容

}catch(MalformedURLExceptione1){

e1.printStackTrace();

}catch(IOExceptione2){

e2.printStackTrace();

tf_address.setBounds(119,50,365,22);

getContentPane().add(tf_address);

finalJLabellabel_3=newJLabel();

label_3.setForeground(newColor(0,0,255));

label_3.setFont(newFont("",Font.BOLD,14));

label_3.setText("輸入網(wǎng)絡(luò)資源的地址并回車,可以獲得網(wǎng)絡(luò)資源的大小。");

label_3.setBounds(10,10,384,22);

getContentPane().add(label_3);

finalJLabellabel_4=newJLabel();

label_4.setForeground(newColor(128,0,0));

label_4.setText("網(wǎng)絡(luò)資源的大小為");

label_4.setBounds(10,76,113,38);

getContentPane().add(label_4);

finalJLabellabel_5=newJLabel();

label_5.setText("上次讀取到");

label_5.setBounds(10,123,74,18);

getContentPane().add(label_5);

tf_readToPos=newJTextField();

tf_readToPos.setBounds(80,121,113,22);

tf_readToPos.setEnabled(false);

getContentPane().add(tf_readToPos);

finalJLabellabel_6=newJLabel();

label_6.setText("字節(jié)處,還剩");

label_6.setBounds(202,123,87,18);

getContentPane().add(label_6);

tf_residuaryLength=newJTextField();

tf_residuaryLength.setBounds(285,120,117,22);

tf_residuaryLength.setEnabled(false);

getContentPane().add(tf_residuaryLength);

finalJLabellabel_7=newJLabel();

label_7.setText("字節(jié)未讀。");

label_7.setBounds(404,123,80,18);

getContentPane().add(label_7);

finalJLabellabel_4_1=newJLabel();

label_4_1.setForeground(newColor(128,0,0));

label_4_1.setText("個(gè)字節(jié)。");

label_4_1.setBounds(404,76,80,38);

getContentPane().add(label_4_1);

tf_totalLength=newJTextField();

tf_totalLength.setBounds(119,84,283,22);

tf_totalLength.setEnabled(false);

getContentPane().add(tf_totalLength);

finalJButtonbutton=newJButton();

button.setBounds(395,162,89,28);

getContentPane().add(button);

button.addActionListener(newActionListener(){

publicvoidactionPerformed(finalActionEvente){

if(totalLength==0){

JOptionPane.showMessageDialog(null,

"沒有網(wǎng)絡(luò)資源。\n\n請(qǐng)輸入正確的網(wǎng)址,然后回車。");

return;

longstartPos=0;//起始位置

longendPos=0;//結(jié)束位置

try{

startPos=Long.parseLong(tf_startPos.getText().trim());//起始位置

endPos=Long.parseLong(tf_endPos.getText().trim());//結(jié)束位置

}catch(Exceptionex){

JOptionPane.showMessageDialog(null,"輸入的起始位置或結(jié)束位置不正確。");

return;

readToPos=endPos;//記錄讀取到的位置

residuaryLength=totalLength-readToPos;//記錄未讀內(nèi)容的大小

tf_readToPos.setText(String.valueOf(readToPos));//顯示讀取到的位置

tf_residuaryLength.setText(String.valueOf(residuaryLength));//顯示未讀字節(jié)數(shù)

tf_startPos.setText(String.valueOf(readToPos));//設(shè)置下一個(gè)讀取點(diǎn)的開始位置

tf_endPos.setText(String.valueOf(totalLength));//設(shè)置下一個(gè)讀取點(diǎn)的結(jié)束位置

tf_endPos.requestFocus();//使結(jié)束位置文本框獲得焦點(diǎn)

tf_endPos.selectAll();//選擇結(jié)束位置文本框中的全部內(nèi)容,方便輸入結(jié)束位置值

download(startPos,endPos);//調(diào)用方法進(jìn)行下載

button.setText("開始下載");

publicvoiddownload(longstartPosition,longendPosition){

try{

URLurl=newURL(urlAddress);//獲得網(wǎng)絡(luò)資源的URL

HttpURLConnectionconnection=(HttpURLConnection)url

.openConnection();//獲得連接對(duì)象

connection.setRequestProperty("User-Agent","NetFox");//設(shè)置請(qǐng)求屬性

StringrangeProperty="bytes="+startPosition+"-";//定義請(qǐng)求范圍屬性

if(endPosition0){

rangeProperty+=endPosition;//調(diào)整請(qǐng)求范圍屬性

connection.setRequestProperty("RANGE",rangeProperty);//設(shè)置請(qǐng)求范圍屬性

connection.connect();//連接網(wǎng)絡(luò)資源

InputStreamin=connection.getInputStream();//獲得輸入流對(duì)象

Stringfile=url.getFile();//獲得文件

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論