悬赏园豆:30
[已解决问题]
解决于 2009-02-20 13:10
<pre></pre>
<div>
<div class="cnblogs_code">
<pre><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF;">using</span><span style="color: #000000;"> System;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.IO;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.Net;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.Net.Security;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.Collections;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.Collections.Generic;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.Security.Cryptography.X509Certificates;<br /></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> System.Threading;<br /></span><span style="color: #0000FF;">namespace</span><span style="color: #000000;"> WinApp_IR<br />{<br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">sealed</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> FtpClientService<br /> {<br /> </span><span style="color: #0000FF;">#region</span><span style="color: #000000;"> Internal Members</span><span style="color: #000000;"><br /><br /> </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> NetworkCredential certificate;<br /><br /> </span><span style="color: #0000FF;">#endregion</span><span style="color: #000000;"><br /><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"><summary></span><span style="color: #008000;"><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> 构造函数,提供初始化数据的功能,打开Ftp站点<br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"></summary></span><span style="color: #808080;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> FtpClientService(</span><span style="color: #0000FF;">string</span><span style="color: #000000;"> User, </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> Pwd)<br /> {<br /> certificate </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NetworkCredential( User, Pwd );<br /> }<br /><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"><summary></span><span style="color: #008000;"><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> 创建FTP请求<br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"></summary></span><span style="color: #008000;"><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"><param name="uri"></span><span style="color: #008000;">ftp://myserver/upload.txt</span><span style="color: #808080;"></param></span><span style="color: #008000;"><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"><param name="method"></span><span style="color: #008000;">Upload/Download</span><span style="color: #808080;"></param></span><span style="color: #008000;"><br /> </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;"><returns></returns></span><span style="color: #808080;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> FtpWebRequest CreateFtpWebRequest(Uri uri, </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> method, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> outTime)<br /> {<br /> FtpWebRequest ftpClientRequest </span><span style="color: #000000;">=</span><span style="color: #000000;"> (FtpWebRequest)WebRequest.Create( uri );<br /><br /> ftpClientRequest.Proxy </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">;<br /> ftpClientRequest.Credentials </span><span style="color: #000000;">=</span><span style="color: #000000;"> certificate;<br /> ftpClientRequest.KeepAlive </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;<br /> ftpClientRequest.UseBinary </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;<br /> ftpClientRequest.UsePassive </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;<br /> ftpClientRequest.Method </span><span style="color: #000000;">=</span><span style="color: #000000;"> method;<br /> ftpClientRequest.Timeout </span><span style="color: #000000;">=</span><span style="color: #000000;"> outTime;<br /> ftpClientRequest.EnableSsl </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;<br /><br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> ftpClientRequest;<br /> }<br /> </span><span style="color: #0000FF;">#region</span><span style="color: #000000;"> 支持断点续传</span><span style="color: #000000;"><br /><br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> IsLogin(Uri destinationPath, </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> ftpMethod, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> timeOut)<br /> {<br /> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"><br /> {<br /> Uri uri </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Uri( destinationPath.AbsoluteUri </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">/Test.Gif</span><span style="color: #800000;">"</span><span style="color: #000000;"> );<br /> FtpWebRequest request </span><span style="color: #000000;">=</span><span style="color: #000000;"> CreateFtpWebRequest( uri, ftpMethod, timeOut );<br /><br /><br /> </span><span style="color: #808080;">///////////////</span><span style="color: #008000;">这个地方</span><span style="color: #808080;"><br /></span><span style="color: #000000;"> WebResponse response </span><span style="color: #000000;">=</span><span style="color: #000000;"> request.GetResponse();<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">WebResponse response = request.BeginGetResponse( new AsyncCallback( CopyDataToDestination ), request );</span><span style="color: #008000;"><br /></span><span style="color: #000000;"><br /><br /> request </span><span style="color: #000000;">=</span><span style="color: #000000;"> CreateFtpWebRequest( uri, WebRequestMethods.Ftp.DeleteFile, timeOut );<br /><br /><br /> </span><span style="color: #808080;">///////////////</span><span style="color: #008000;">这个地方</span><span style="color: #808080;"><br /></span><span style="color: #000000;"> response </span><span style="color: #000000;">=</span><span style="color: #000000;"> request.GetResponse();<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">response = request.BeginGetResponse( new AsyncCallback( CopyDataToDestination ), request );</span><span style="color: #008000;"><br /></span><span style="color: #000000;"><br /> response.Close();<br /> }<br /> </span><span style="color: #0000FF;">catch</span><span style="color: #000000;"> (Exception ex)<br /> {<br /> ex.ToString();<br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;<br /> }<br /><br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;<br /> }<br /><br /><br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> UploadFile(</span><span style="color: #0000FF;">string</span><span style="color: #000000;"> sourceFile, Uri destinationPath, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> offSet, </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> ftpMethod, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> timeOut)<br /> {<br /> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"><br /> {<br /> FileInfo file </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> FileInfo( sourceFile );<br /> Uri uri </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Uri( destinationPath.AbsoluteUri </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">/</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> file.Name );<br /> FtpWebRequest request </span><span style="color: #000000;">=</span><span style="color: #000000;"> CreateFtpWebRequest( uri, ftpMethod, timeOut );<br /> request.ContentOffset </span><span style="color: #000000;">=</span><span style="color: #000000;"> offSet;<br /><br /><br /> </span><span style="color: #808080;">///////////////</span><span style="color: #008000;">这个地方</span><span style="color: #808080;"><br /></span><span style="color: #000000;"> Stream requestStream </span><span style="color: #000000;">=</span><span style="color: #000000;"> request.GetRequestStream();</span><span style="color: #008000;">//</span><span style="color: #008000;">需要获取文件的流<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Stream requestStream = request.BeginGetRequestStream( new AsyncCallback( CopyDataToDestination ), request ); </span><span style="color: #008000;"><br /></span><span style="color: #000000;"><br /><br /> FileStream fileStream </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> FileStream( sourceFile, FileMode.Open, FileAccess.Read );</span><span style="color: #008000;">//</span><span style="color: #008000;">创建存储文件的流</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> sourceLength </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">int</span><span style="color: #000000;">)fileStream.Length;<br /> offSet </span><span style="color: #000000;">=</span><span style="color: #000000;"> CopyDataToDestination( fileStream, requestStream, offSet );<br /><br /><br /> </span><span style="color: #808080;">///////////////</span><span style="color: #008000;">这个地方</span><span style="color: #808080;"><br /></span><span style="color: #000000;"> WebResponse response </span><span style="color: #000000;">=</span><span style="color: #000000;"> request.GetResponse();<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">WebResponse response = request.BeginGetResponse( new AsyncCallback( CopyDataToDestination ), request );</span><span style="color: #008000;"><br /></span><span style="color: #000000;"><br /><br /> response.Close();<br /> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (offSet </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)<br /> {<br /> UploadFile( sourceFile, destinationPath, offSet, WebRequestMethods.Ftp.AppendFile, timeOut );<br /> }<br /> }<br /> </span><span style="color: #0000FF;">catch</span><span style="color: #000000;"> (Exception ex)<br /> {<br /> ex.ToString();<br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;<br /> }<br /><br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;<br /> }<br /><br /> </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> CopyDataToDestination(Stream sourceStream, Stream destinationStream, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> offSet)<br /> {<br /> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"><br /> {<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> sourceLength </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">int</span><span style="color: #000000;">)sourceStream.Length;<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> length </span><span style="color: #000000;">=</span><span style="color: #000000;"> sourceLength </span><span style="color: #000000;">-</span><span style="color: #000000;"> offSet;<br /> </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[] buffer </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[length </span><span style="color: #000000;">+</span><span style="color: #000000;"> offSet];<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> bytesRead </span><span style="color: #000000;">=</span><span style="color: #000000;"> sourceStream.Read( buffer, offSet, length );<br /> </span><span style="color: #0000FF;">while</span><span style="color: #000000;"> (bytesRead </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)<br /> {<br /> destinationStream.Write( buffer, </span><span style="color: #800080;">0</span><span style="color: #000000;">, bytesRead );<br /> bytesRead </span><span style="color: #000000;">=</span><span style="color: #000000;"> sourceStream.Read( buffer, </span><span style="color: #800080;">0</span><span style="color: #000000;">, length );<br /> length </span><span style="color: #000000;">=</span><span style="color: #000000;"> length </span><span style="color: #000000;">-</span><span style="color: #000000;"> bytesRead;<br /> offSet </span><span style="color: #000000;">=</span><span style="color: #000000;"> (bytesRead </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) </span><span style="color: #000000;">?</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> : (sourceLength </span><span style="color: #000000;">-</span><span style="color: #000000;"> length);</span><span style="color: #008000;">//</span><span style="color: #008000;">(length - bytesRead); </span><span style="color: #008000;"><br /></span><span style="color: #000000;"> }<br /> }<br /> </span><span style="color: #0000FF;">catch</span><span style="color: #000000;"> (Exception ex)<br /> {<br /> </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> error </span><span style="color: #000000;">=</span><span style="color: #000000;"> ex.ToString();<br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> offSet;<br /> }<br /> </span><span style="color: #0000FF;">finally</span><span style="color: #000000;"><br /> {<br /> destinationStream.Close();<br /> destinationStream.Dispose();<br /> sourceStream.Close();<br /> sourceStream.Dispose();<br /> }<br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> offSet;<br /> }<br /> </span><span style="color: #0000FF;">#endregion</span><span style="color: #000000;"><br /> }<br />}</span></pre>
</div>
<pre></pre>
</div>
KHK
|
初学一级
|
园豆:
170
提问于:2008-10-16 16:25