java斷點(diǎn)續(xù)傳功能實(shí)例(java獲取遠(yuǎn)程文件)
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net . * ;
/**
* 文件傳送客戶端:獲取遠(yuǎn)程文件
*/
public class GetRemoteFile_Client_GoOn { public GetRemoteFile_Client_GoOn()
{
}
private boolean FileExist(String pathAndFile) // 確定文件是否已經(jīng)下載,但沒(méi)有下載完成
{
File file = new File(pathAndFile);
if (file.exists())
return true ;
else
return false ;
}
private long FileSize(String pathAndFile) // 確定已經(jīng)下載了的文件大小
{
File file = new File(pathAndFile);
return file.length();
}
private void FileRename(String fName,String nName) // 將下載完全的文件更名,去掉.tp名
{
File file = new File(fName);
file.renameTo( new File(nName));
file.delete();
}
public static void main(String[] args)
{
URL url = null ;
HttpURLConnection urlc = null ;
DataOutputStream dos = null ;
BufferedInputStream bis = null ;
FileOutputStream fos = null ;
String localFile = " d:////x.x " ; // 文件保存的地方及文件名,具體情況可以改
String localFile_bak = localFile + " .tp " ; // 未下載完文件加.tp擴(kuò)展名,以便于區(qū)別
GetRemoteFile_Client_GoOn gco = new GetRemoteFile_Client_GoOn();
long fileSize = 0 ;
long start = System.currentTimeMillis();
int len = 0 ;
byte [] bt = new byte [ 1024 ];
// byte[] buffer=new byte[50*1024];
RandomAccessFile raFile = null ;
long TotalSize = 0 ; // 要下載的文件總大小
try
{
url = new URL( " http://www.netbox.cn/download/nbsetup.EXE " );
urlc = (HttpURLConnection) url.openConnection();
TotalSize = Long.parseLong(urlc.getHeaderField( " Content-Length " ));
System.out.println( " 下載文件大小為: " + TotalSize);
urlc.disconnect(); // 先斷開,下面再連接,否則下面會(huì)報(bào)已經(jīng)連接的錯(cuò)誤
urlc = (HttpURLConnection) url.openConnection();
// 確定文件是否存在
if (gco.FileExist(localFile_bak)) // 采用斷點(diǎn)續(xù)傳,這里的依據(jù)是看下載文件是否在本地有.tp有擴(kuò)展名同名文件
{
System.out.println( " 文件續(xù)傳中… " );
fileSize = gco.FileSize(localFile_bak); // 取得文件在小,以便確定隨機(jī)寫入的位置
System.out.println( " fileSize: " + fileSize);
// 設(shè)置User-Agent
// urlc.setRequestProperty("User-Agent","NetFox");
// 設(shè)置斷點(diǎn)續(xù)傳的開始位置
urlc.setRequestProperty( " RANGE " , " bytes= " + fileSize + " - " );
// urlc.setRequestProperty("RANGE", "bytes="+fileSize); // 這樣寫不行,不能少了這個(gè)"-".
// 設(shè)置接受信息
urlc.setRequestProperty( " Accept " , " image/gif,image/x-xbitmap,application/msword,*/* " );
raFile = new RandomAccessFile(localFile_bak, " rw " ); // 隨機(jī)方位讀取
raFile.seek(fileSize); // 定位指針到fileSize位置
bis = new BufferedInputStream(urlc.getInputStream());
while ((len = bis.read(bt)) > 0 ) // 循環(huán)獲取文件
{
raFile.write(bt, 0 , len);
// buffer=buffer+bt;
// System.
}
System.out.println( " 文件續(xù)傳接收完畢! " );
}
else // 采用原始下載
{
fos = new FileOutputStream(localFile_bak); // 沒(méi)有下載完畢就將文件的擴(kuò)展名命名.bak
dos = new DataOutputStream(fos);
bis = new BufferedInputStream(urlc.getInputStream());
System.out.println( " 正在接收文件… " );
int test = 0 ;
while ((len = bis.read(bt)) > 0 ) // 循環(huán)獲取文件
{
dos.write(bt, 0 , len);
test ++ ;
if (test == 50 ) // 這里是測(cè)試,你可以刪除這里,就可以正常下載了
break ;
}
// System.out.println("文件正常接收完畢!");
}
System.out.println( " 共用時(shí): " +
(System.currentTimeMillis() - start) / 1000 );
if (bis != null )
bis.close();
if (dos != null )
dos.close();
if (fos != null )
fos.close();
if (raFile != null )
raFile.close();
System.out.println( " localFile_bak: " + gco.FileSize(localFile_bak));
if (gco.FileSize(localFile_bak) == TotalSize) // 下載完畢后,將文件重命名
{
gco.FileRename(localFile_bak,localFile);
}
System.exit( 0 );
}
catch (Exception e)
{
try
{
if (bis != null )
bis.close();
if (dos != null )
dos.close();
if (fos != null )
fos.close();
if (raFile != null )
raFile.close();
}
catch (IOException f)
{
f.printStackTrace();
}
e.printStackTrace();
}
System.exit( 0 );
}
}
相關(guān)文章
Spring Cloud Admin健康檢查 郵件、釘釘群通知的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Admin健康檢查 郵件、釘釘群通知的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
SpringBoot中YAML語(yǔ)法及幾個(gè)注意點(diǎn)說(shuō)明
這篇文章主要介紹了SpringBoot中YAML語(yǔ)法及幾個(gè)注意點(diǎn)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
分布式醫(yī)療掛號(hào)系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存
這篇文章主要為大家介紹了分布式醫(yī)療掛號(hào)系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
java+mysql模擬實(shí)現(xiàn)銀行系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java+mysql模擬實(shí)現(xiàn)銀行系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
使用maven運(yùn)行Java Main的三種方法解析
這篇文章主要介紹了使用maven運(yùn)行Java Main的三種方式的相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
springboot項(xiàng)目打docker鏡像實(shí)例(入門級(jí))
最近做個(gè)項(xiàng)目,我們想把自己的程序打包成鏡像,并運(yùn)行在docker容器中,本文主要介紹了springboot項(xiàng)目打docker鏡像實(shí)例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼
這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請(qǐng)求中某個(gè)接口需要升級(jí),正常做法是升級(jí)我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03

