将文件从FTP服务器下载到网络驱动器

时间:2012-09-06 15:21:53

标签: c# .net ftp filestream

我正在尝试实现从FTP服务器下载文件并将其保存到DB中指定的网络驱动器位置的Windows服务应用程序。 网络共享文件夹授予对" Everyone"

的完全访问权限
bool status = true;
string webError = string.Empty;
FtpWebResponse response = null;
Stream ftpStream = null;
FileStream outputStream = null;
FtpDownloadToFolder = @"\\servername\SharedFolder\";
fileName  = "test.jpg";

FtpWebRequest reqFTP = WebRequest.Create(new Uri(FtpAddress + "/" + fileName)) as FtpWebRequest;
reqFTP.Credentials = new NetworkCredential(FtpUser, FtpPassword);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Timeout = FtpTimeout;
response = reqFTP.GetResponse() as FtpWebResponse;
ftpStream = response.GetResponseStream();

if (!FtpDownloadToFolder.EndsWith("\\"))
{
    FtpDownloadToFolder = FtpDownloadToFolder + "\\";
}
outputStream = new FileStream(FtpDownloadToFolder + "/" + fileName, FileMode.Create);

long cl = response.ContentLength;
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
int readCount = ftpStream.Read(buffer, 0, bufferSize);

while (readCount > 0)
{
    outputStream.Write(buffer, 0, readCount);
    readCount = ftpStream.Read(buffer, 0, bufferSize);
}

所以,我在这里尝试将文件从FTP服务器下载到网络驱动器中。但我得到以下错误:

Access to the path '\\servername\SharedFolder\test.jpg' is denied.

System.Net.WebException: The remote server returned an error: 150 0.066 seconds (measured here), 3.78 Mbytes per second
---> System.IO.IOException: The operation is not allowed on non-connected sockets.
   at System.Net.Sockets.NetworkStream.InitNetworkStream(Socket socket, FileAccess Access)
   at System.Net.Sockets.NetworkStream..ctor(Socket socket, Boolean ownsSocket)
   at System.Net.FtpControlStream.QueueOrCreateFtpDataStream(Stream& stream)
   at System.Net.FtpControlStream.PipelineCallback(PipelineEntry entry, ResponseDescription response, Boolean timeout, Stream& stream)
   at System.Net.CommandStream.PostReadCommandProcessing(Stream& stream)
   at System.Net.CommandStream.PostSendCommandProcessing(Stream& stream)
   at System.Net.CommandStream.ContinueCommandPipeline()
   at System.Net.CommandStream.CheckContinuePipeline()
   --- End of inner exception stack trace ---
   at System.Net.FtpWebRequest.CheckError()
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.CommandStream.CheckContinuePipeline()
   at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   at System.Net.FtpDataStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.Net.FtpWebResponse.Close()

我在这里遗漏了什么吗?如何将文件从FTP服务器复制到网络共享文件夹?

1 个答案:

答案 0 :(得分:0)

通常,您的服务将在仅在本地主机上的帐户下运行。因此,它不是从您网络上经过身份验证的帐户运行的。只需更改运行该服务的帐户,以确保它是您网络AD中的帐户,因此它将成为您所指的Everyone群组的一部分。