最近使用c#開發乙個簡易的ftp客戶端,在測試上傳功能的時候發現丟擲乙個錯誤,【遠端伺服器返回錯誤:(550) 檔案不可用(例如,未找到檔案,無法訪問檔案)】。
然後我就通過vs除錯的工具跟了一下,發現出現異常的地方在下面的**段那
stream strm = reqftp.getrequeststream();
contentlen = fs.read(buff, 0, bufflength);
我們可以知道前面的什麼驗證使用者名稱,密碼都是沒有錯誤的,同時connect函式也順利通過了,所以我感覺我是連線上伺服器了。那麼出現錯誤的這行**是什麼意思呢,這個函式的意思是新建了乙個流物件strm,然後讀buff裡面的資料寫到這個流物件裡面,這個時候就報錯了,肯定是讀的時候出錯了,腦子裡第一反應就是許可權問題,也就是說本地無法寫檔案到伺服器裡去。
解決辦法
顯然是伺服器那邊的問題,這裡測試使用的是windows 2003搭建的乙個ftp伺服器,由於伺服器搭建的預設許可權是只有讀取,所以我們需要把這裡修改一下。
首先進入管理工具中的internet資訊服務(iis)管理器
選擇ftp站點的屬性
選擇主目錄,把寫入的許可權給勾選
這樣設定好了,再使用上傳功能就不會報以前的錯誤了。
最後貼上ftp客戶端類的**
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.net;
using system.io;
using system.windows.forms;
namespace ftp
set
}string ftpuserid;
public
string ftpuserid
set
}string ftppasswd;
public
string ftppasswd
set
}ftpwebrequest reqftp;
private
void
connect(string path)//連線ftp
private
string getfilelist(string path, string wrmethods)//上面的**示例了如何從ftp伺服器上獲得檔案列表
// to remove the trailing '\n'
result.remove(result.tostring().lastindexof('\n'), 1);
reader.close();
response.close();
return result.tostring().split('\n');
} catch (exception ex)
}public
string getfilelist(string path)
public
string getfilelist()
public
string
upload(string filename) //上面的**實現了從ftp伺服器上載檔案的功能
// 關閉兩個流
strm.close();
fs.close();
return
""; }
catch (exception ex)
}public
string
string url = "ftp://" + ftpsrvip + "/" + filename;
connect(url);//連線
reqftp.credentials = new networkcredential(ftpuserid, ftppasswd);
ftpwebresponse response = (ftpwebresponse)reqftp.getresponse();
stream ftpstream = response.getresponsestream();
long cl = response.contentlength;
int buffersize = 2048;
int readcount;
byte buffer = new
byte[buffersize];
readcount = ftpstream.read(buffer, 0, buffersize);
filestream outputstream = new filestream(newfilename, filemode.create);
while (readcount > 0)
ftpstream.close();
outputstream.close();
response.close();
return
""; }
catch (exception ex)
}public
void
deletefilename(string filename)
catch (exception ex)
} //建立目錄
public
string
makedir(string dirname)
catch (webexception ex)
} //刪除目錄
public
void
deldir(string dirname)
catch (exception ex)
} //獲得檔案大小
public
long
getfilesize(string filename)
catch (exception ex)
return filesize;
} //檔案改名
public
void
rename(string currentfilename, string newfilename)
catch (exception ex)
} //獲得檔案明晰
public
string getfilesdetaillist()
//獲得檔案明晰
public
string getfilesdetaillist(string path)
} }
FTP客戶端程式
ftp客戶端程式,vc6.0下除錯通過 client.c include client.h define sol socket 0xffff int fill host addr char host ip addr,struct sockaddr in host,int port else retu...
ftp客戶端程式
include include include include include pragma comment lib,ws2 32.lib file fp socket client,upload wsadata wsadata int wsareturn int portnum char spor...
C 開發OPC客戶端
第乙個隨筆,使用了open live write,作為客戶端.最近使用c 開發乙個小軟體,主要功能是opc客戶端.以後會開發各類別的協議,作為,協議的 棧.因為我本人是搞自動化的,所以搞自動化小夥伴像我這樣喜歡程式設計的可能有,但是一般是邏輯思維強,但是底子相對還是弱的.1,c 開發opc的準備工作...