當前位置:首頁 » 文件管理 » ftpwebrequest上傳

ftpwebrequest上傳

發布時間: 2023-07-08 08:43:56

㈠ C# winform如何實現批量上傳文件到遠程伺服器

基本思路是遠程伺服器假設ftp,本地用System.Net.FtpWebRequest建立Ftp客戶端,用NetworkCredential.NetworkCredential建立用戶名和密碼驗證方式,用System.Net.WebRequestMethods.Ftp設置上傳和下載命令,文件通過FileStream流進行發送和接收。

㈡ FtpWebRequest 多個文件同時上傳。求C#代碼,舉個栗子就好。

#
usingSystem;
usingSystem.IO;
usingSystem.Net;
namespaceFtp
{
classProgram
{
staticvoidMain(string[]args)
{
DirectoryInfodir=newDirectoryInfo("upload");//上傳文件所在的目錄
FileInfo[]fileInfos=dir.GetFiles();
foreach(FileInfoiteminfileInfos)//遍歷目錄下的所有文件
{
UploadFile(item);//開始上傳
}
Console.WriteLine("done!");
}
privatestaticvoidUploadFile(FileInfofi)
{
FileStreamfs=fi.OpenRead();
longlength=fs.Length;
FtpWebRequestreq=(FtpWebRequest)WebRequest.Create(newUri("ftp://192.168.56.1/"+fi.Name));
req.Credentials=newNetworkCredential("name","password");
req.Method=WebRequestMethods.Ftp.UploadFile;
req.UseBinary=true;
req.ContentLength=length;
req.Timeout=10*1000;
try
{
Streamstream=req.GetRequestStream();
intBufferLength=2048;
byte[]b=newbyte[BufferLength];
inti;
while((i=fs.Read(b,0,BufferLength))>0)
{
stream.Write(b,0,i);
}
stream.Close();
stream.Dispose();
}
catch(Exceptionex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

㈢ C#winform怎麼上傳文件到伺服器

DYKDHNSBSAZGHSNZG

㈣ c# FTP上傳文件

C#ftp上傳,參考如下:
///<summary>
///上傳文件
///</summary>/
//<paramname="fileinfo">需要上傳的文件</param>
///<paramname="targetDir">目標路徑</param>
///<paramname="hostname">ftp地址</param>/
//<paramname="username">ftp用戶名</param>/
//<paramname="password">ftp密碼</param>
publicstaticvoidUploadFile(FileInfofileinfo,stringtargetDir,stringhostname,stringusername,stringpassword)
{//1.checktarget
stringtarget;
if(targetDir.Trim()=="")
{return;}
target=Guid.NewGuid().ToString();
//使用臨時文件名
森乎嫌stringURI="FTP://"+hostname+"/"+targetDir+"/"+target;
///WebClientwebcl=newWebClient();
System.Net.FtpWebRequestftp=GetRequest(URI,username,password);
//設置FTP命令設置所要執行的FTP命令,
//ftp.Method=System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假設此處為顯示指定路徑下的文件列表
ftp.Method=System.Net.WebRequestMethods.Ftp.UploadFile;
//指定文件傳輸的數據類型
ftp.UseBinary=true;
ftp.UsePassive=true;//告訴ftp文件大小
ftp.ContentLength=fileinfo.Length;
//緩沖大小設置為2KB
constintBufferSize=2048;
byte[]content=newbyte[BufferSize-1+1];
intdataRead;//打開一個文件流(System.IO.FileStream)去讀上傳的文件
using(FileStreamfs=fileinfo.OpenRead())
{
try{//把上頃橡傳的文件寫入流
using(Streamrs=ftp.GetRequestStream())
{do
{//每次讀文件流的2KB
dataRead=fs.Read(content,0,BufferSize);rs.Write(content,0,dataRead);}
while(!(dataRead<BufferSize));rs.Close();}}
catch(Exceptionex){}finally{fs.Close();}}
ftp=null;//設置FTP命令
ftp=GetRequest(URI,username,password);
ftp.Method=System.Net.WebRequestMethods.Ftp.Rename;//改名
ftp.RenameTo=此手fileinfo.Name;try{ftp.GetResponse();}
catch(Exceptionex)
{
ftp=GetRequest(URI,username,password);ftp.Method=System.Net.WebRequestMethods.Ftp.DeleteFile;//刪除
ftp.GetResponse();throwex;}finally
{
//fileinfo.Delete();}//可以記錄一個日誌"上傳"+fileinfo.FullName+"上傳到"+"FTP://"+hostname+"/"+targetDir+"/"+fileinfo.Name+"成功.");
ftp=null;
#region
/******FtpWebResponse*****///FtpWebResponseftpWebResponse=(FtpWebResponse)ftp.GetResponse();
#endregion
}
///<summary>
///下載文件
///</summary>
///<paramname="localDir">下載至本地路徑</param>
///<paramname="FtpDir">ftp目標文件路徑</param>
///<paramname="FtpFile">從ftp要下載的文件名</param>
///<paramname="hostname">ftp地址即IP</param>
///<paramname="username">ftp用戶名</param>
///<paramname="password">ftp密碼</param>
publicstaticvoidDownloadFile(stringlocalDir,stringFtpDir,stringFtpFile,stringhostname,stringusername,stringpassword)
{
stringURI="FTP://"+hostname+"/"+FtpDir+"/"+FtpFile;
stringtmpname=Guid.NewGuid().ToString();
stringlocalfile=localDir+@""+tmpname;

System.Net.FtpWebRequestftp=GetRequest(URI,username,password);
ftp.Method=System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary=true;
ftp.UsePassive=false;

using(FtpWebResponseresponse=(FtpWebResponse)ftp.GetResponse())
{
using(StreamresponseStream=response.GetResponseStream())
{
//looptoread&writetofile
using(FileStreamfs=newFileStream(localfile,FileMode.CreateNew))
{
try
{
byte[]buffer=newbyte[2048];
intread=0;
do
{
read=responseStream.Read(buffer,0,buffer.Length);
fs.Write(buffer,0,read);
}while(!(read==0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch(Exception)
{
//
fs.Close();
//deletetargetfileasit'sincomplete
File.Delete(localfile);
throw;
}
}

responseStream.Close();
}

response.Close();
}try
{
File.Delete(localDir+@""+FtpFile);
File.Move(localfile,localDir+@""+FtpFile);


ftp=null;
ftp=GetRequest(URI,username,password);
ftp.Method=System.Net.WebRequestMethods.Ftp.DeleteFile;
ftp.GetResponse();

}
catch(Exceptionex)
{
File.Delete(localfile);
throwex;
}

//記錄日誌"從"+URI.ToString()+"下載到"+localDir+@""+FtpFile+"成功.");
ftp=null;
}

///<summary>
///搜索遠程文件
///</summary>
///<paramname="targetDir"></param>
///<paramname="hostname"></param>
///<paramname="username"></param>
///<paramname="password"></param>
///<paramname="SearchPattern"></param>
///<returns></returns>
publicstaticList<string>ListDirectory(stringtargetDir,stringhostname,stringusername,stringpassword,stringSearchPattern)
{
List<string>result=newList<string>();
try
{
stringURI="FTP://"+hostname+"/"+targetDir+"/"+SearchPattern;

System.Net.FtpWebRequestftp=GetRequest(URI,username,password);
ftp.Method=System.Net.WebRequestMethods.Ftp.ListDirectory;
ftp.UsePassive=true;
ftp.UseBinary=true;


stringstr=GetStringResponse(ftp);
str=str.Replace(" "," ").TrimEnd(' ');
str=str.Replace(" "," ");
if(str!=string.Empty)
result.AddRange(str.Split(' '));

returnresult;
}
catch{}
returnnull;
}

(FtpWebRequestftp)
{
//Gettheresult,streamingtoastring
stringresult="";
using(FtpWebResponseresponse=(FtpWebResponse)ftp.GetResponse())
{
longsize=response.ContentLength;
using(Streamdatastream=response.GetResponseStream())
{
using(StreamReadersr=newStreamReader(datastream,System.Text.Encoding.Default))
{
result=sr.ReadToEnd();
sr.Close();
}

datastream.Close();
}

response.Close();
}

returnresult;
}
///在ftp伺服器上創建目錄
///</summary>
///<paramname="dirName">創建的目錄名稱</param>
///<paramname="ftpHostIP">ftp地址</param>
///<paramname="username">用戶名</param>
///<paramname="password">密碼</param>
publicvoidMakeDir(stringdirName,stringftpHostIP,stringusername,stringpassword)
{
try
{
stringuri="ftp://"+ftpHostIP+"/"+dirName;
System.Net.FtpWebRequestftp=GetRequest(uri,username,password);
ftp.Method=WebRequestMethods.Ftp.MakeDirectory;

FtpWebResponseresponse=(FtpWebResponse)ftp.GetResponse();
response.Close();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
///<summary>
///刪除目錄
///</summary>
///<paramname="dirName">創建的目錄名稱</param>
///<paramname="ftpHostIP">ftp地址</param>
///<paramname="username">用戶名</param>
///<paramname="password">密碼</param>
publicvoiddelDir(stringdirName,stringftpHostIP,stringusername,stringpassword)
{
try
{
stringuri="ftp://"+ftpHostIP+"/"+dirName;
System.Net.FtpWebRequestftp=GetRequest(uri,username,password);
ftp.Method=WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponseresponse=(FtpWebResponse)ftp.GetResponse();
response.Close();
}
catch(Exceptionex)
{

MessageBox.Show(ex.Message);
}
}
///<summary>
///文件重命名
///</summary>
///<paramname="currentFilename">當前目錄名稱</param>
///<paramname="newFilename">重命名目錄名稱</param>
///<paramname="ftpServerIP">ftp地址</param>
///<paramname="username">用戶名</param>
///<paramname="password">密碼</param>
publicvoidRename(stringcurrentFilename,stringnewFilename,stringftpServerIP,stringusername,stringpassword)
{
try
{

FileInfofileInf=newFileInfo(currentFilename);
stringuri="ftp://"+ftpServerIP+"/"+fileInf.Name;
System.Net.FtpWebRequestftp=GetRequest(uri,username,password);
ftp.Method=WebRequestMethods.Ftp.Rename;

ftp.RenameTo=newFilename;
FtpWebResponseresponse=(FtpWebResponse)ftp.GetResponse();

response.Close();
}
catch(Exceptionex){MessageBox.Show(ex.Message);}}
(stringURI,stringusername,stringpassword)
{
//根據伺服器信息FtpWebRequest創建類的對象
FtpWebRequestresult=(FtpWebRequest)FtpWebRequest.Create(URI);
//提供身份驗證信息
result.Credentials=newSystem.Net.NetworkCredential(username,password);
//設置請求完成之後是否保持到FTP伺服器的控制連接,默認值為true
result.KeepAlive=false;
returnresult;
}

㈤ VB6.0 FTP下載上傳問題【可加分!】

首先介紹ftp下載,代碼如下: Const Str_FtpServer As String = " ftp://ftp.lob.cn" Private Str_Username As String = "lob" Private Str_Password As String = "xxxxxxxx" Private Sub DownloadFile(ByVal Str_Path As String) Try Dim Str_Filename As String = Str_Path Dim ftpReq As FtpWebRequest = WebRequest.Create(Str_Filename) ftpReq.Method = WebRequestMethods.Ftp.DownloadFile ftpReq.Credentials = New NetworkCredential(Str_Username, Str_Password) Dim FTPResp As FtpWebResponse = ftpReq.GetResponse Dim ftpRespStream As Stream = FTPResp.GetResponseStream 'FTPResp.StatusDescription 為回顯狀態的描述 可用於調試 ftpRespStream.Save("d:\lobdotcn.rar") '保存到本地的路徑,如果你樂意也可以直接定義到參數中 FTPResp.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub 調用方式:DownloadImage(Str_FtpServer& "/logo.jpg")==============================================================你先看看是你想要的不?是的話再往下看從這里過來的: http://blog.chinaunix.net/u/22170/showart_475726.html 呵呵我是做C#的,但是看你這分數實在太誘人,就幫你找了找~

記得採納啊

熱點內容
如何刪除下載的鬧鍾鈴聲安卓 發布:2025-02-04 22:03:35 瀏覽:656
死神腳本 發布:2025-02-04 21:57:03 瀏覽:166
phpposthtml 發布:2025-02-04 21:37:46 瀏覽:88
最新asp源碼 發布:2025-02-04 21:17:33 瀏覽:571
讓linux死機 發布:2025-02-04 20:48:08 瀏覽:141
單方塊生存伺服器里如何獲取岩漿 發布:2025-02-04 20:48:07 瀏覽:785
快速指數演算法 發布:2025-02-04 20:20:40 瀏覽:299
python在類中定義函數調用函數 發布:2025-02-04 20:14:47 瀏覽:596
安卓手機的壁紙是哪個 發布:2025-02-04 20:14:44 瀏覽:202
java發展前景 發布:2025-02-04 20:10:19 瀏覽:77