導航:首頁 > 編程語言 > java斷點續傳ftp

java斷點續傳ftp

發布時間:2022-04-20 06:51:35

❶ FTP該如何實現斷點續傳

客戶端的實現步驟如下:
一、下載:
1、向伺服器發送「REST + 本地文件長度」命令,告訴伺服器,客戶端要斷點下載了。這時伺服器還不知道客戶端要下載哪個文件;
要實現FTP的斷點續傳,FTP伺服器必須支持REST指令,這條指令在FTP協議文本RFC959中就已經定義了,不過它不是FTP伺服器必須支持的指令。一般,你可以在下載前使用REST 100命令進行實驗,如果伺服器正常執行了這條命令,說明該伺服器支持FTP斷點續傳。REST後面跟的數表示下載文件的起始位置,而REST 0表示從文件最開始處下載。REST命令本身並不執行下載功能,你仍需要使用RETR命令執行下載工作。
2、向伺服器發送「RETR + 文件名」命令,通知伺服器要下載的文件名,這時伺服器開始定位文件指針讀文件並發送數據。
3、客戶端定位本地文件指針(文件末尾);
4、兩端的准備工作都做完了以後,客戶端創建socket,以被動或非被動方式建立數據通道,循環調用recv接收數據並追加入本地文件;
二、上傳:
1、獲取伺服器上和本地要上傳文件的同名文件大小;
2、向伺服器發送「APPE + 文件名」,通知伺服器,接下來從數據通道發送給你的數據要附加到這個文件末尾。
3、定位本地文件指針(和FTP上文件大小相同的位置)
4、從文件指針處讀數據並發送。
代碼里將斷點上傳和斷點下載放到同一個函數(MoveFile)里,通過get參數說明是上傳還是下載。

java實現ftp斷點續傳問題

//嘗試移動文件內讀取指針,實現斷點續傳
result
=
uploadFile(remoteFileName,
f,
ftpClient,
remoteSize);
//如果斷點續傳沒有成功,則刪除伺服器上文件,重新上傳
if(result
==
UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
return
UploadStatus.Delete_Remote_Faild;
}
result
=
uploadFile(remoteFileName,
f,
ftpClient,
0);
}

❸ java 網頁怎麼實現文件斷點續傳

斷點設置方法,當有臨時文件時,直接在臨時文件中讀取上次下載中斷時的斷點位置。沒有臨時文件,即第一次下載時,重新設置斷點。
rantmpfile.seek()跳轉到一個位置的目的是為了讓各個斷點存儲的位置盡量分開。

❹ 用Java實現斷點續傳

記的http://www-128.ibm.com/developerworks上好像有文章

❺ JAVA FTP斷點續傳

Java寫的支持斷點續傳的FTP

http://crybaby2005.bokee.com/3148701.html

❻ java 實現ftp上傳下載,windows下和linux下游何區別

packagecom.weixin.util;

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.PrintWriter;
importjava.io.RandomAccessFile;

importorg.apache.commons.net.PrintCommandListener;
importorg.apache.commons.net.ftp.FTP;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;

importcom.weixin.constant.DownloadStatus;
importcom.weixin.constant.UploadStatus;

/**
*支持斷點續傳的FTP實用類
*@version0.1實現基本斷點上傳下載
*@version0.2實現上傳下載進度匯報
*@version0.3實現中文目錄創建及中文文件創建,添加對於中文的支持
*/
publicclassContinueFTP{
publicFTPClientftpClient=newFTPClient();

publicContinueFTP(){
//設置將過程中使用到的命令輸出到控制台
this.ftpClient.addProtocolCommandListener(newPrintCommandListener(newPrintWriter(System.out)));
}

/**
*連接到FTP伺服器
*@paramhostname主機名
*@paramport埠
*@paramusername用戶名
*@parampassword密碼
*@return是否連接成功
*@throwsIOException
*/
publicbooleanconnect(Stringhostname,intport,Stringusername,Stringpassword)throwsIOException{
ftpClient.connect(hostname,port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username,password)){
returntrue;
}
}
disconnect();
returnfalse;
}

/**
*從FTP伺服器上下載文件,支持斷點續傳,上傳百分比匯報
*@paramremote遠程文件路徑
*@paramlocal本地文件路徑
*@return上傳的狀態
*@throwsIOException
*/
publicDownloadStatusdownload(Stringremote,Stringlocal)throwsIOException{
//設置被動模式
ftpClient.enterLocalPassiveMode();
//設置以二進制方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatusresult;

//檢查遠程文件是否存在
FTPFile[]files=ftpClient.listFiles(newString(remote.getBytes("GBK"),"iso-8859-1"));
if(files.length!=1){
System.out.println("遠程文件不存在");
returnDownloadStatus.Remote_File_Noexist;
}

longlRemoteSize=files[0].getSize();
Filef=newFile(local);
//本地存在文件,進行斷點下載
if(f.exists()){
longlocalSize=f.length();
//判斷本地文件大小是否大於遠程文件大小
if(localSize>=lRemoteSize){
System.out.println("本地文件大於遠程文件,下載中止");
returnDownloadStatus.Local_Bigger_Remote;
}

//進行斷點續傳,並記錄狀態
FileOutputStreamout=newFileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStreamin=ftpClient.retrieveFileStream(newString(remote.getBytes("GBK"),"iso-8859-1"));
byte[]bytes=newbyte[1024];
longstep=lRemoteSize/100;
longprocess=localSize/step;
intc;
while((c=in.read(bytes))!=-1){
out.write(bytes,0,c);
localSize+=c;
longnowProcess=localSize/step;
if(nowProcess>process){
process=nowProcess;
if(process%10==0)
System.out.println("下載進度:"+process);
//TODO更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
booleanisDo=ftpClient.completePendingCommand();
if(isDo){
result=DownloadStatus.Download_From_Break_Success;
}else{
result=DownloadStatus.Download_From_Break_Failed;
}
}else{
OutputStreamout=newFileOutputStream(f);
InputStreamin=ftpClient.retrieveFileStream(newString(remote.getBytes("GBK"),"iso-8859-1"));
byte[]bytes=newbyte[1024];
longstep=lRemoteSize/100;
longprocess=0;
longlocalSize=0L;
intc;
while((c=in.read(bytes))!=-1){
out.write(bytes,0,c);
localSize+=c;
longnowProcess=localSize/step;
if(nowProcess>process){
process=nowProcess;
if(process%10==0)
System.out.println("下載進度:"+process);
//TODO更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
booleanupNewStatus=ftpClient.completePendingCommand();
if(upNewStatus){
result=DownloadStatus.Download_New_Success;
}else{
result=DownloadStatus.Download_New_Failed;
}
}
returnresult;
}

/**
*上傳文件到FTP伺服器,支持斷點續傳
*@paramlocal本地文件名稱,絕對路徑
*@paramremote遠程文件路徑,使用/home/directory1/subdirectory/file.ext按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
*@return上傳結果
*@throwsIOException
*/
publicUploadStatusupload(Stringlocal,Stringremote)throwsIOException{
//設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
//設置以二進制流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
UploadStatusresult;
//對遠程目錄的處理
StringremoteFileName=remote;
if(remote.contains("/")){
remoteFileName=remote.substring(remote.lastIndexOf("/")+1);
//創建伺服器遠程目錄結構,創建失敗直接返回
if(CreateDirecroty(remote,ftpClient)==UploadStatus.Create_Directory_Fail){
returnUploadStatus.Create_Directory_Fail;
}
}

//檢查遠程是否存在文件
FTPFile[]files=ftpClient.listFiles(newString(remoteFileName.getBytes("GBK"),"iso-8859-1"));
if(files.length==1){
longremoteSize=files[0].getSize();
Filef=newFile(local);
longlocalSize=f.length();
if(remoteSize==localSize){
returnUploadStatus.File_Exits;
}elseif(remoteSize>localSize){
returnUploadStatus.Remote_Bigger_Local;
}

//嘗試移動文件內讀取指針,實現斷點續傳
result=uploadFile(remoteFileName,f,ftpClient,remoteSize);

//如果斷點續傳沒有成功,則刪除伺服器上文件,重新上傳
if(result==UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
returnUploadStatus.Delete_Remote_Faild;
}
result=uploadFile(remoteFileName,f,ftpClient,0);
}
}else{
result=uploadFile(remoteFileName,newFile(local),ftpClient,0);
}
returnresult;
}
/**
*斷開與遠程伺服器的連接
*@throwsIOException
*/
publicvoiddisconnect()throwsIOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}

/**
*遞歸創建遠程伺服器目錄
*@paramremote遠程伺服器文件絕對路徑
*@paramftpClientFTPClient對象
*@return目錄創建是否成功
*@throwsIOException
*/
(Stringremote,FTPClientftpClient)throwsIOException{
UploadStatusstatus=UploadStatus.Create_Directory_Success;
Stringdirectory=remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(newString(directory.getBytes("GBK"),"iso-8859-1"))){
//如果遠程目錄不存在,則遞歸創建遠程伺服器目錄
intstart=0;
intend=0;
if(directory.startsWith("/")){
start=1;
}else{
start=0;
}
end=directory.indexOf("/",start);
while(true){
StringsubDirectory=newString(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else{
System.out.println("創建目錄失敗");
returnUploadStatus.Create_Directory_Fail;
}
}

start=end+1;
end=directory.indexOf("/",start);

//檢查所有目錄是否創建完畢
if(end<=start){
break;
}
}
}
returnstatus;
}

/**
*上傳文件到伺服器,新上傳和斷點續傳
*@paramremoteFile遠程文件名,在上傳之前已經將伺服器工作目錄做了改變
*@paramlocalFile本地文件File句柄,絕對路徑
*@paramprocessStep需要顯示的處理進度步進值
*@paramftpClientFTPClient引用
*@return
*@throwsIOException
*/
publicUploadStatusuploadFile(StringremoteFile,FilelocalFile,FTPClientftpClient,longremoteSize)throwsIOException{
UploadStatusstatus;
//顯示進度的上傳
longstep=localFile.length()/100;
longprocess=0;
longlocalreadbytes=0L;
RandomAccessFileraf=newRandomAccessFile(localFile,"r");
OutputStreamout=ftpClient.appendFileStream(newString(remoteFile.getBytes("GBK"),"iso-8859-1"));
//斷點續傳
if(remoteSize>0){
ftpClient.setRestartOffset(remoteSize);
process=remoteSize/step;
raf.seek(remoteSize);
localreadbytes=remoteSize;
}
byte[]bytes=newbyte[1024];
intc;
while((c=raf.read(bytes))!=-1){
out.write(bytes,0,c);
localreadbytes+=c;
if(localreadbytes/step!=process){
process=localreadbytes/step;
System.out.println("上傳進度:"+process);
//TODO匯報上傳狀態
}
}
out.flush();
raf.close();
out.close();
booleanresult=ftpClient.completePendingCommand();
if(remoteSize>0){
status=result?UploadStatus.Upload_From_Break_Success:UploadStatus.Upload_From_Break_Failed;
}else{
status=result?UploadStatus.Upload_New_File_Success:UploadStatus.Upload_New_File_Failed;
}
returnstatus;
}

publicstaticvoidmain(String[]args){
ContinueFTPmyFtp=newContinueFTP();
try{
System.err.println(myFtp.connect("10.10.6.236",21,"5","jieyan"));
// myFtp.ftpClient.makeDirectory(newString("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.changeWorkingDirectory(newString("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.makeDirectory(newString("愛你等於愛自己".getBytes("GBK"),"iso-8859-1"));
// System.out.println(myFtp.upload("E:\yw.flv","/yw.flv",5));
// System.out.println(myFtp.upload("E:\愛你等於愛自己.mp4","/愛你等於愛自己.mp4"));
//System.out.println(myFtp.download("/愛你等於愛自己.mp4","E:\愛你等於愛自己.mp4"));
myFtp.disconnect();
}catch(IOExceptione){
System.out.println("連接FTP出錯:"+e.getMessage());
}
}
}

❼ 求Java 以上超大文件上傳和斷點續傳伺服器的實現

提供個思路

使用2個介面
介面一,獲取伺服器已上傳文件的大小
介面二,依據介面一的大小從本地文件指定位置讀取文件上傳

❽ Java ftp 斷點續傳報錯

NoClassDefFoundError
1.未通過編譯
2.沒有引入jar包

❾ java 斷點續傳

這個不太難吧?
假設A給B傳文件F(1024位元組)。第一次B接收了512位元組,那麼第二次連接A就應該從513位元組開始傳輸。
也就是說,在第二次傳輸時,B要提供「我要從513位元組開始傳送文件F」的信息,然後A使用FileInputStream構建輸入流讀取本地文件,使用skip(512)方法跳過文件F的前512位元組再傳送文件,之後B將數據追加(append)到先前接收的文件末尾即可。
進一步考慮,如果要實現多線程傳送,即分塊傳輸,也同樣的道理。假如B要求分作兩塊同時傳輸,那麼A啟動兩個線程,一個從513位元組讀到768位元組(工256位元組),第二個線程從769位元組到1024位元組即可。
如果你要從網路上下載文件,就是說A方不是你實現的,那麼你要先確認A方支不支持斷電續傳功能(HTTP1.1),然後你查閱下HTTP1.1協議,在HTTP1.1版本里,可以通過設置請求包頭某個欄位的信息(使用URLConnection創建連接並使用setRequestProperty(String key, String value) 方法設置)從而精確讀取文件的某一段數據的。注意,基於HTTP斷點續傳的關鍵是1.1版本,1.0版本是不支持的。

補充:
嗯,查到了,是設置range屬性,即setRequestProperty("range", "bytes=513-1024").你可以使用迅雷下載某個文件,然後從」線程信息「中就可以看到這個http1.1斷點續傳的所有行為信息了。
希望能解決您的問題。

閱讀全文

與java斷點續傳ftp相關的資料

熱點內容
c523壓縮比 瀏覽:543
命令語氣的人什麼心態 瀏覽:435
程序員喜歡留指甲嗎 瀏覽:516
七牛雲伺服器收費標准 瀏覽:627
時光相冊加密空間密碼忘記 瀏覽:474
華為雲為用戶提供的服務雲伺服器 瀏覽:634
minecraftlinux伺服器搭建 瀏覽:376
linux命令新建文件 瀏覽:708
長線pdf 瀏覽:607
程序員電腦支持手寫 瀏覽:414
解壓頭戴式耳機推薦 瀏覽:344
紙條app上怎麼樣看對方主頁 瀏覽:883
編譯英語單詞怎麼寫 瀏覽:249
編譯原理和匯編原理的區別 瀏覽:864
如何給加密的pdf解密 瀏覽:770
華為盒子時間同步伺服器地址 瀏覽:95
python處理excel亂碼 瀏覽:391
mysql的命令行 瀏覽:822
jpeg採用什麼演算法 瀏覽:701
程序員紅軸薄膜 瀏覽:306