gpt4 book ai didi

asp.net - 将图像上传到 FTP 时出现错误

转载 作者:行者123 更新时间:2023-12-04 07:54:46 30 4
gpt4 key购买 nike

我在这个函数中遇到了很多不同的问题:

public static bool UploadToFTP(string strFileName, string strFolderName)
{
bool isUploaded = false;
string strFilename = string.Empty;
string strFtpURI = string.Empty;
string strFtpUserId = string.Empty;
string strFtpPassword = string.Empty;
byte[] buffer = null;
FileInfo oFileInfo = null;
FileStream oFileStream = null;
FtpWebRequest oFtpWebRequest = null;

try
{
strFilename = strFileName;
oFileInfo = new FileInfo(strFilename);
strFtpURI = Constants.FtpUri;
strFtpUserId = Constants.FtpUserID;
strFtpPassword = Constants.FtpPassword;

oFtpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpURI + "/" + strFolderName + "/" + oFileInfo.Name));

oFtpWebRequest.Credentials = new NetworkCredential(strFtpUserId, strFtpPassword);
oFtpWebRequest.Proxy = null;
oFtpWebRequest.KeepAlive = false;
oFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
oFtpWebRequest.UseBinary = true;
oFtpWebRequest.ContentLength = oFileInfo.Length;

int iBufferLength = 2084;

buffer = new byte[iBufferLength];

int iContentLength = 0;

oFileStream = oFileInfo.OpenRead();

try
{
iContentLength = oFileStream.Read(buffer, 0, iBufferLength);

using (Stream oStream = oFtpWebRequest.GetRequestStream())
{
while (iContentLength != 0)
{
oStream.Write(buffer, 0, iContentLength);

iContentLength = oFileStream.Read(buffer, 0, iBufferLength);
}
isUploaded = true;
FtpUpload.TotalKBFilesUploaded = FtpUpload.TotalKBFilesUploaded + (int)(oFileInfo.Length / 1000);
}
}
catch (Exception ex)
{

}
finally
{
if (oFtpWebRequest != null)
{
oFtpWebRequest.Abort();
oFtpWebRequest = null;
}

if (buffer != null)
{
buffer = null;
}

if (oFileStream != null)
{
oFileStream.Close();
oFileStream.Dispose();
}
}
}
catch (Exception ex)
{
}
finally
{
oFileInfo = null;
}

return isUploaded;
}

这是将 1000 幅图像上传到 FTP,这种方法以多线程方式调用。

不同的错误是:

================================================== ======

消息:操作已超时

错误跟踪:在 System.Net.FtpWebRequest.CheckError() 在 System.Net.FtpWebRequest.GetRequestStream()

================================================== ======

错误信息:无法连接到远程服务器

错误跟踪:在 System.Net.FtpWebRequest.CheckError() 在 System.Net.FtpWebRequest.GetRequestStream()

================================================== ======

错误消息:基础连接已关闭:接收时发生意外错误。

错误跟踪:在 System.Net.FtpWebRequest.CheckError() 在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj) 在 System.Net.CommandStream.Abort(Exception e) 在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) 在System.Net.FtpWebRequest.GetRequestStream()

================================================== ======

错误信息:无法向传输连接写入数据:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接的主机未能响应而建立的连接失败。

错误跟踪:在 System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.FtpDataStream.Write(Byte[] buffer, Int32 offset, Int32 size)
================================================== ======

这些错误来自我从 LOG 文件中检索到的相同方法。

知道是什么原因造成的吗?或者我需要提供更多细节?

最佳答案

达到超时时会遇到以下异常:

Error Message:The underlying connection was closed: An unexpected error occurred on a receive.



如 msdn 文档中所述,超时的默认值是无限的,但 msdn 文档包含一个错误:
http://msdn.microsoft.com/fr-fr/library/vstudio/system.net.ftpwebrequest.timeout(v=vs.80).aspx

事实上,默认值为 100000 毫秒(1 分 40 秒),因此您可以使用以下命令将超时值声明为无限:oFtpWebRequest.Timeout = -1;

http://www.sidesofmarch.com/index.php/archive/2012/04/06/damn-the-documentation-ftpwebrequest-timeout-default-value-is-not-infinite/

关于asp.net - 将图像上传到 FTP 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251198/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com