java斷點續(xù)傳功能實例(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)下載,但沒有下載完成
{
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擴展名,以便于區(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(); // 先斷開,下面再連接,否則下面會報已經(jīng)連接的錯誤
urlc = (HttpURLConnection) url.openConnection();
// 確定文件是否存在
if (gco.FileExist(localFile_bak)) // 采用斷點續(xù)傳,這里的依據(jù)是看下載文件是否在本地有.tp有擴展名同名文件
{
System.out.println( " 文件續(xù)傳中… " );
fileSize = gco.FileSize(localFile_bak); // 取得文件在小,以便確定隨機寫入的位置
System.out.println( " fileSize: " + fileSize);
// 設(shè)置User-Agent
// urlc.setRequestProperty("User-Agent","NetFox");
// 設(shè)置斷點續(xù)傳的開始位置
urlc.setRequestProperty( " RANGE " , " bytes= " + fileSize + " - " );
// urlc.setRequestProperty("RANGE", "bytes="+fileSize); // 這樣寫不行,不能少了這個"-".
// 設(shè)置接受信息
urlc.setRequestProperty( " Accept " , " image/gif,image/x-xbitmap,application/msword,*/* " );
raFile = new RandomAccessFile(localFile_bak, " rw " ); // 隨機方位讀取
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); // 沒有下載完畢就將文件的擴展名命名.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 ) // 這里是測試,你可以刪除這里,就可以正常下載了
break ;
}
// System.out.println("文件正常接收完畢!");
}
System.out.println( " 共用時: " +
(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)文章
使用Idea簡單快速搭建springcloud項目的圖文教程
這篇文章主要介紹了使用Idea簡單快速搭建springcloud項目,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01JPA?@ManyToMany?報錯StackOverflowError的解決
這篇文章主要介紹了JPA?@ManyToMany?報錯StackOverflowError的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12springboot實現(xiàn)定時任務(wù)@Scheduled方式
這篇文章主要介紹了springboot實現(xiàn)定時任務(wù)@Scheduled方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07java web開發(fā)之servlet圖形驗證碼功能的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java web開發(fā)之servlet中圖形驗證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11親手教你SpringBoot中的多數(shù)據(jù)源集成問題
本文主要是介紹基于springboot的多數(shù)據(jù)源切換,輕量級的一種集成方案,對于小型的應(yīng)用可以采用這種方案,我之前在項目中用到是因為簡單,便于擴展以及優(yōu)化,對SpringBoot多數(shù)據(jù)源集成問題感興趣的朋友一起看看吧2022-03-03- 本章具體介紹了HashMap、TreeMap兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
IDEA生成可運行jar包(包含第三方j(luò)ar包)流程詳解
這篇文章主要介紹了IDEA生成可運行jar包(包含第三方j(luò)ar包)流程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑
這篇文章主要介紹了詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03最新評論