上傳檔案。普通和ftp

2022-02-13 10:56:06 字數 2295 閱讀 3945

/// 上傳檔案

///

/// 檔案儲存位置

/// 檔案

///

public string uploadfile( system.web.ui.webcontrols.fileupload fu)

string dhost = "ftphost";

string dport = "ftpport";

string dlogin = "ftplogin";

string dpwd = "ftppwd";

string host = "";

string login = "";

string password = "";

string port = "";

host = configurationmanager.connectionstrings[dhost].connectionstring;

login = configurationmanager.connectionstrings[dlogin].connectionstring;

password = configurationmanager.connectionstrings[dpwd].connectionstring;

port = configurationmanager.connectionstrings[dport].connectionstring;

ftpwebrequest reqftp;

// 根據uri建立ftpwebrequest物件

guid gg = guid.newguid();

string filename = gg.tostring() + "_" + filename.substring(filename.lastindexof("\\") + 1);

string path = "ftp://" + host + "//" + filename;

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

// ftp使用者名稱和密碼

reqftp.credentials = new networkcredential(login, password);

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

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

reqftp.keepalive = false;

// 指定執行什麼命令

reqftp.method = webrequestmethods.ftp.uploadfile;

// 指定資料傳輸型別

reqftp.usebinary = true;

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

reqftp.contentlength = fu.postedfile.inputstream.length;

// 緩衝大小設定為2kb

int bufflength = 2048;

byte buff = new byte[bufflength];

int contentlen;

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

stream fs = (stream)fu.postedfile.inputstream;

// 把上傳的檔案寫入流

stream strm = reqftp.getrequeststream();

// 每次讀檔案流的2kb

contentlen = fs.read(buff, 0, bufflength);

// 流內容沒有結束

while (contentlen != 0)

// 關閉兩個流

strm.close();

fs.close();

return filename;

}catch (exception ex)

#endregion

}else\", libpath, filename);

rstr = uploadstream(b, filename, path);

}catch (exception error)

return rstr;}}

///

/// 上傳檔案流

///

/// 流檔案

/// 檔名稱

/// 儲存路徑

///

protected string uploadstream(byte fs, string filename, string requestpath)

catch (exception error)

}

FTP檔案上傳

是網上找的例子,原文找不到了。public class ftputils2 return 判斷是否登入成功 public boolean ftplogin else ftp伺服器連線回答 int reply this.ftpclient.getreplycode if ftpreply.isposi...

ftp檔案上傳

pom依賴引入 commons net commons net 3.3實現 service public class ftpserviceimpl private string ftphost ftp伺服器位址 value private int ftpport ftp伺服器端口 value pri...

FTP上傳檔案

工作中要發布 或者api,之前採用過vs自帶的ftp發布方式,但感覺不太自由,不能部分更新發布 後來就採用vs檔案發布,然後手動的將發布的檔案放到伺服器上,每次發布都要開啟伺服器不太方便。就採用了現在的方式 服務端filezilla 客戶端flashfxp。這裡記錄一下安裝和除錯filezilla ...