我正在尝试将文件上传至FTP,之前一直运行正常,但今天突然无法100%的工作,让我想不出原因。
我已经尝试了几乎所有方法。
以下是我使用的方法,请让我知道是否需要更多信息:
我已经尝试了几乎所有方法。
以下是我使用的方法,请让我知道是否需要更多信息:
- 这不是一个Web服务。
- 我已将FtpWebRequest的Timeout和ReadWriteTimeout增加到-1。
- 我已将用于写入文件内容的流的WriteTimeout和ReadTimeout设置为适当的值。
- 我们已确保出站规则允许我们通信的端口,并且可以确认我们可以使用FileZilla从同一台机器/网络进行写入/读取操作。
- 文件不是很大,最大只有1MB,而且是简单的文本文件。
private static void FtpUpload(String username, String password, String address,
Int32 port, Boolean usePassive, String filePath)
{
try
{
String fileName = "";
fileName = Path.GetFileName(filePath);
FtpWebRequest request = null;
request = (FtpWebRequest)FtpWebRequest.Create(String.Format("ftp://{0}", address));
request.Credentials = new NetworkCredential(
username,
password);
request.UsePassive = usePassive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = -1;
request.ReadWriteTimeout = -1;
request.KeepAlive = false;
request.Proxy = null;
Console.WriteLine(String.Format("Uploading {0}...", fileName));
Stream ftpStream = null;
using (ftpStream = request.GetRequestStream())
{
ftpStream.WriteTimeout = -1;
ftpStream.ReadTimeout = -1;
using (FileStream file = File.OpenRead(filePath))
{
file.CopyTo(ftpStream);
}
}
}
catch
{
throw;
}
}
编辑:
这段代码片段有效。请注意,当我更新此片段以使用using块时,它会重新失败。是否使用using块可能是原因?
public static void FtpUpload(
String username,
String password,
String address,
Int32 port,
Boolean usePassive,
String filePath)
{
string ftpServerIP = String.Format("ftp://{0}", address);
string ftpUserID = username;
string ftpPassword = password;
FileInfo fileInf = new FileInfo(filePath);
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP);
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = false;
request.UsePassive = false;
request.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = request.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch
{
throw;
}
}
usePassive=true
吗?这可能是你异常的原因(参见 SO 问题)。 - Pilgerstorfer FranzWrite(...)
(MSDN) 方法,而不是copy(..)
。 - Pilgerstorfer Franz