C FTP上傳的實現方法 包括斷點上傳

2021-06-27 12:03:35 字數 4442 閱讀 1087

在使用ftp上傳一般使用

ftpwebrequest建立物件 執行對應操作

如下1.直接上傳 

在此需要使用req.ftp.method = webrequestmethods.ftp.uploadfile;

2.斷點續傳(自己實現的,不知道是否科學)

但須先讀取伺服器上已存在的該檔案的大小,上傳時直接跳過該大小 

利用此方法直接從後面繼續讀取檔案即可

filestream fs= fileinf.openread(); 

fs.seek(startfilesize, 0);

具體斷點續傳實現如下

public bool upload(string filename, string ftpserverip, string ftpuserid, string ftppassword, toolstripprogressbar pb,string path)

else

long startfilesize = getfilesize(newfilename, ftpserverip, ftpuserid, ftppassword, path);

if (startfilesize >= allbye)

//startfilesize=0;

long startbye = startfilesize;

pb.maximum = convert.toint32(allbye);

pb.minimum = convert.toint32(startfilesize);

string uri;

if (path.length == 0)

uri = "ftp://" + ftpserverip + "/" + newfilename;

else

uri = "ftp://" + ftpserverip + "/" + path + newfilename;

ftpwebrequest reqftp;

// 根據uri建立ftpwebrequest物件 

reqftp = (ftpwebrequest)ftpwebrequest.create(new uri(uri));

// ftp使用者名稱和密碼 

reqftp.credentials = new networkcredential(ftpuserid, ftppassword);

// 預設為true,連線不會被關閉 

// 在乙個命令之後被執行 

reqftp.keepalive = false;

// 指定執行什麼命令 

// 指定資料傳輸型別 

reqftp.usebinary = true;

// 上傳檔案時通知伺服器檔案的大小 

reqftp.contentlength = fileinf.length;

int bufflength = 2048;// 緩衝大小設定為2kb 

byte buff = new byte[bufflength];

// 開啟乙個檔案流 (system.io.filestream) 去讀上傳的檔案 

filestream fs= fileinf.openread();

try// 關閉兩個流 

strm.close();

fs.close();

}catch

return success;

} 具體ftp上傳類參看下期blog

public bool upload(string filename, string ftpserverip, string ftpuserid, string ftppassword, toolstripprogressbar pb,string path)

else

long startfilesize = getfilesize(newfilename, ftpserverip, ftpuserid, ftppassword, path);

if (startfilesize >= allbye)

//startfilesize=0;

long startbye = startfilesize;

pb.maximum = convert.toint32(allbye);

pb.minimum = convert.toint32(startfilesize);

string uri;

if (path.length == 0)

uri = "ftp://" + ftpserverip + "/" + newfilename;

else

uri = "ftp://" + ftpserverip + "/" + path + newfilename;

ftpwebrequest reqftp;

// 根據uri建立ftpwebrequest物件 

reqftp = (ftpwebrequest)ftpwebrequest.create(new uri(uri));

// ftp使用者名稱和密碼 

reqftp.credentials = new networkcredential(ftpuserid, ftppassword);

// 預設為true,連線不會被關閉 

// 在乙個命令之後被執行 

reqftp.keepalive = false;

// 指定執行什麼命令 

// 指定資料傳輸型別 

reqftp.usebinary = true;

// 上傳檔案時通知伺服器檔案的大小 

reqftp.contentlength = fileinf.length;

int bufflength = 2048;// 緩衝大小設定為2kb 

byte buff = new byte[bufflength];

// 開啟乙個檔案流 (system.io.filestream) 去讀上傳的檔案 

filestream fs= fileinf.openread();

try

// 關閉兩個流 

strm.close();

fs.close(); }

catch

return success;

}public bool upload(string filename, string ftpserverip, string ftpuserid, string ftppassword, toolstripprogressbar pb,string path)

else

long startfilesize = getfilesize(newfilename, ftpserverip, ftpuserid, ftppassword, path);

if (startfilesize >= allbye)

//startfilesize=0;

long startbye = startfilesize;

pb.maximum = convert.toint32(allbye);

pb.minimum = convert.toint32(startfilesize);

string uri;

if (path.length == 0)

uri = "ftp://" + ftpserverip + "/" + newfilename;

else

uri = "ftp://" + ftpserverip + "/" + path + newfilename;

ftpwebrequest reqftp;

// 根據uri建立ftpwebrequest物件 

reqftp = (ftpwebrequest)ftpwebrequest.create(new uri(uri));

// ftp使用者名稱和密碼 

reqftp.credentials = new networkcredential(ftpuserid, ftppassword);

// 預設為true,連線不會被關閉 

// 在乙個命令之後被執行 

reqftp.keepalive = false;

// 指定執行什麼命令 

// 指定資料傳輸型別 

reqftp.usebinary = true;

// 上傳檔案時通知伺服器檔案的大小 

reqftp.contentlength = fileinf.length;

int bufflength = 2048;// 緩衝大小設定為2kb 

byte buff = new byte[bufflength];

// 開啟乙個檔案流 (system.io.filestream) 去讀上傳的檔案 

filestream fs= fileinf.openread();

try

// 關閉兩個流 

strm.close();

fs.close(); }

catch

return success; }

除錯斷點的實現原理

前幾天在公司,突然和 n同學討論起編譯器是如何實現除錯斷點的,這下才突然意識到,每天程式設計在除錯的時候都會去設斷點,但從來沒有思考過編譯器或者作業系統式如何實現程式的除錯斷點的,於是我們展開了討論。首先,我們覺得,不大可能是在目標程式裡插入了 因為這個斷點我們可以隨時設定或取消,如果是在生成程式或...

php檔案上傳簡單實現方法

檔案1 index.php 複製 如下 檔案2 uploadprocess.php 複製 如下 filetype arr array image jpeg image png image gif image gif 允許上傳的型別 if empty post sub echo 等型別 exit ex...

資料清洗的方法包括什麼

資料清洗的方法包括 1 分箱法,就是將需要處理的資料根據一定的規則放進箱子裡,然後進行測試 2 回歸法,就是利用函式的資料進行繪製影象,然後對影象進行光滑處理 3 聚類法,就是將抽象的物件進行集合分組,成為不同的集合,找到在集合意外的孤點。資料清洗的方法包括什麼?清洗資料有三個方法,分別是分箱法 聚...