private static string ftpServerIP = "192.168.0.95";
private static string ftpUserID = "Administrator";
private static string ftpPassword = "trw2007"; static void Main(string[] args)
{
Upload(@"E:\TEMP\ftpUpload.txt");
} private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false; // 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型
reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb
int buffLength = 2048; byte[] buff = new byte[buffLength];
int contentLen; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength);
} // 关闭两个流
strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw ex;
}
} 同样差不多的代码实现FTP下载没有问题,但是一上传文件就在 Stream strm = reqFTP.GetRequestStream(); 这一句抛出异常。不解,向大家请教。
private static string ftpUserID = "Administrator";
private static string ftpPassword = "trw2007"; static void Main(string[] args)
{
Upload(@"E:\TEMP\ftpUpload.txt");
} private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false; // 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型
reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb
int buffLength = 2048; byte[] buff = new byte[buffLength];
int contentLen; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength);
} // 关闭两个流
strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw ex;
}
} 同样差不多的代码实现FTP下载没有问题,但是一上传文件就在 Stream strm = reqFTP.GetRequestStream(); 这一句抛出异常。不解,向大家请教。
解决方案 »
- 关于UDP和组播的一些疑问.
- 关于DataRow 的NewRow
- 求一个排列组合的算法
- [在线等]关于数据集的问题?
- 数据库更新的问题,一年多了都还没有解决,只好求助CSDN
- 怎么用javascript控制gridview中的checkbox只有一个选中。
- 请教:当鼠标在RichTextBox中点击一下,就执行全选操作怎么实现??
- 程序员 如何合理规范自己的电脑文件和文件夹 还有源码什么的
- 怎么实现VB中的"Dim sUseSN As String * 20",可以控制字符串的长度。
- PictureBox控件的重画问题
- c#中窗体实现隐藏,任务栏为什么无图标?再怎样弹出显示?
- dateTimePicker问题Bug??????????????BUG
请问glacier111,还需要什么额外的设置吗?
可以查看一下ftp服务器,该ftp用户的权限,另外还需要查看一下ftp服务器的防火墙之类的