请教各位大侠c# ftp 上传文件问题 5
privateFtpStatusCodeftpUpload(stringfileName,stringuploadUrl){StreamrequestStream=nul...
private FtpStatusCode ftpUpload(string fileName, string uploadUrl)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =(FtpWebRequest)WebRequest.Create(uploadUrl);//创建完整的ftp上传路径
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//代表上传文件
uploadRequest.Proxy = null;//代表无通信代理
NetworkCredential nc = new NetworkCredential();//身份验证
nc.UserName = "jinkeyiliao";
nc.Password = "123456";//ftp的用户名和密码
uploadRequest.Credentials = nc;
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open,FileAccess.ReadWrite);//打开要上传的文件
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
return uploadResponse.StatusCode;
}
catch (UriFormatException ex)
{
MessageBox.Show(ex.Message+"无效的统一资源标识符");
}
catch (IOException ex)
{
MessageBox.Show(ex.Message+"输入输出错误");
}
catch (WebException ex)
{
MessageBox.Show(ex.Message+"网络异常");
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
return FtpStatusCode.Undefined;
}
private void button2_Click(object sender, EventArgs e)
{
string uploadUrl = "ftp://202.85.222.176/binglifiles/1.doc";
FtpStatusCode newFtp = ftpUpload(textBox1.Text, uploadUrl);
}
其中textbox1.text为选定的文件的完整文件名,点击button2以后WebException捕获到异常,异常信息为object reference not set to an instance of an object,请各位帮忙看看是哪儿的问题 展开
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =(FtpWebRequest)WebRequest.Create(uploadUrl);//创建完整的ftp上传路径
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//代表上传文件
uploadRequest.Proxy = null;//代表无通信代理
NetworkCredential nc = new NetworkCredential();//身份验证
nc.UserName = "jinkeyiliao";
nc.Password = "123456";//ftp的用户名和密码
uploadRequest.Credentials = nc;
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open,FileAccess.ReadWrite);//打开要上传的文件
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
return uploadResponse.StatusCode;
}
catch (UriFormatException ex)
{
MessageBox.Show(ex.Message+"无效的统一资源标识符");
}
catch (IOException ex)
{
MessageBox.Show(ex.Message+"输入输出错误");
}
catch (WebException ex)
{
MessageBox.Show(ex.Message+"网络异常");
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
return FtpStatusCode.Undefined;
}
private void button2_Click(object sender, EventArgs e)
{
string uploadUrl = "ftp://202.85.222.176/binglifiles/1.doc";
FtpStatusCode newFtp = ftpUpload(textBox1.Text, uploadUrl);
}
其中textbox1.text为选定的文件的完整文件名,点击button2以后WebException捕获到异常,异常信息为object reference not set to an instance of an object,请各位帮忙看看是哪儿的问题 展开
2个回答
展开全部
这句话的意思是“为将对象引用设置到对象的实例”,请检查你的textBox1.Text的值是否为null。
更多追问追答
追问
你好这位仁兄,不是空的,文件的完整路径+文件名已经取到textbox1.text中去了
追答
你可以在出现异常的地方 前面加一个值的判断 ,出现这个情况 基本上是调用了一个空对象
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-01-18
展开全部
你力压
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询