我正在尝试实现一个 Windows 服务应用程序,该应用程序从 FTP 服务器下载文件并将它们保存到 DB 中指定的网络驱动器位置。网络共享文件夹授予“所有人”的完全访问权限
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 服务器复制到网络共享文件夹?