着急等!

lizhenlz 2004-11-13 08:36:47
ftp上传一个文件行,反复上传就不行,是什么原因呀?
...全文
95 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy__Huang 2004-11-14
  • 打赏
  • 举报
回复
代碼太多了
lizhenlz 2004-11-14
  • 打赏
  • 举报
回复
private Socket createDataSocket()
{
sendCommand("PASV");
if(retValue != 227)
{
throw new IOException(reply.Substring(4));
}
int index1 = reply.IndexOf('(');
int index2 = reply.IndexOf(')');
string ipData = reply.Substring(index1+1,index2-index1-1);
int[] parts = new int[6];
int len = ipData.Length;
int partCount = 0;
string buf="";
for (int i = 0; i < len && partCount <= 6; i++)
{
char ch = Char.Parse(ipData.Substring(i,1));
if (Char.IsDigit(ch))
buf+=ch;
else if (ch != ',')
{
throw new IOException("Malformed PASV reply: " + reply);
}
if (ch == ',' || i+1 == len)
{
try
{
parts[partCount++] = Int32.Parse(buf);
buf="";
}
catch (Exception)
{
throw new IOException("Malformed PASV reply: " + reply);
}
}
}

string ipAddress = parts[0] + "."+ parts[1]+ "." + parts[2] + "." + parts[3];
int port = (parts[4] << 8) + parts[5];
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.Resolve(ipAddress).AddressList[0], port);
try
{
s.Connect(ep);
}
catch(Exception)
{
throw new IOException("Can't connect to remote server");
}
return s;


/// Upload a file.
///
public void upload(string fileName)
{
upload(fileName,false);
}

///
/// Upload a file and set the resume flag.
///
public void upload(string fileName,Boolean resume)
{

if(!logined)
{
login();
}

Socket cSocket = createDataSocket();
long offset=0;

if(resume)
{

try
{

setBinaryMode(true);
offset = getFileSize(fileName);

}
catch(Exception)
{
offset = 0;
}
}

if(offset > 0 )
{
sendCommand("REST " + offset);
if(retValue != 350)
{
//throw new IOException(reply.Substring(4));
//Remote server may not support resuming.
offset = 0;
}
}

sendCommand("STOR "+Path.GetFileName(fileName));

if( !(retValue == 125 || retValue == 150) )
{
throw new IOException(reply.Substring(4));
}

// open input stream to read source file
FileStream input = new FileStream(fileName,FileMode.Open);

if(offset != 0)
{

if(debug)
{
Console.WriteLine("seeking to " + offset);
}
input.Seek(offset,SeekOrigin.Begin);
}

Console.WriteLine("Uploading file "+fileName+" to "+remotePath);

while ((bytes = input.Read(buffer,0,buffer.Length)) > 0)
{
cSocket.Send(buffer, bytes, 0);
}
input.Close();
Console.WriteLine("");
if (cSocket.Connected)
{
cSocket.Close();
}

readReply();
if( !(retValue == 226 || retValue == 250) )
{
throw new IOException(reply.Substring(4));
}
}
}
520NET 2004-11-14
  • 打赏
  • 举报
回复
LG
zhanghw20030303 2004-11-14
  • 打赏
  • 举报
回复
源码贴出来看看
hivak47 2004-11-14
  • 打赏
  • 举报
回复
是不是你的源程序有问题?
hivak47 2004-11-14
  • 打赏
  • 举报
回复
有源码吗?
孟子E章 2004-11-13
  • 打赏
  • 举报
回复
ftp是你自己写的?那一定是代码没有写好啦,好好检查一下吧,网上也有这方面的例子

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧