java 读取 远程文件_利用JAVA获取远程文件及使用断点续传 供学习者使用
閑來(lái)沒(méi)事,就做做,程序還是要多寫才好@
原理不說(shuō),文件方面及I/O方面,去BAIDU一下就知道,斷點(diǎn)續(xù)傳的原理也很簡(jiǎn)單,就是在原來(lái)已經(jīng)下載的基礎(chǔ)之上繼續(xù)下載就可以了,用到了這么關(guān)鍵的一句:urlc.setRequestProperty("RANGE", "bytes="+fileSize+"-");其它就應(yīng)該很簡(jiǎn)單吧.
/**
* 文件傳送客戶端:獲取遠(yuǎn)程文件,并采用斷點(diǎn)續(xù)傳原理
* 軟件很簡(jiǎn)單,寫這個(gè)東東經(jīng)歷了三個(gè)步子:
* 1、寫了一個(gè)文件傳送程序,基于客戶端與服務(wù)端
* 2、寫了一個(gè)從網(wǎng)上獲取文件的程序,學(xué)了怎么樣使用:HttpURLConnection及URL
* 3、就是現(xiàn)在這個(gè)程序,在2的基礎(chǔ)之上加上了斷點(diǎn)續(xù)傳的原理。
* 不過(guò),功能雖然是達(dá)到了,不過(guò),里面還有很多不如意的地方,慢慢改進(jìn)吧,特別是效率方面
* 如果有實(shí)際需要,可以把這個(gè)改成線程的,很簡(jiǎn)單了哦。
* 準(zhǔn)備做第四個(gè)版本:多線程文件下載。
*/
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);
}
}
總結(jié)
以上是生活随笔為你收集整理的java 读取 远程文件_利用JAVA获取远程文件及使用断点续传 供学习者使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: redis set 超时_Redis 更
- 下一篇: java c 效率_吐槽一下java的