/// <summary>
/// 接收和发送数据的缓冲区
/// </summary>
private static int BLOCK_SIZE = 3072;
Byte[] buffer = new Byte[BLOCK_SIZE]; /// 获得文件列表
/// </summary>
/// <param name="strMask">文件名的匹配字符串</param>
/// <returns></returns>
public string[] Dir(string strMask)
{
// 建立链接
if (!bConnected)
{
Connect();
} //建立进行数据连接的socket
Socket socketData = CreateDataSocket(); //传送命令
SendCommand("NLST " + strMask); //分析应答代码
if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
{
throw new IOException(strReply.Substring(4));
} //获得结果
strMsg = "";
while (true)
{
int iBytes = socketData.Receive(buffer, buffer.Length, 0);
//strMsg += ASCII.GetString(buffer, 0, iBytes);
strMsg+=Encoding.GetEncoding("gb2312").GetString(buffer, 0, iBytes);
if (iBytes == buffer.Length || iBytes<buffer.Length)
{
break;
}
}
string[] seperator = { "\r\n" };
string[] strsFileList = strMsg.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
socketData.Close();//数据socket关闭时也会有返回码
if (iReplyCode != 226)
{
ReadReply();
if (iReplyCode != 226)
{
throw new IOException(strReply.Substring(4));
}
}
return strsFileList;
} /// 建立进行数据连接的socket
/// </summary>
/// <returns>数据连接socket</returns>
private Socket CreateDataSocket()
{
SendCommand("PASV");//PASV
if (iReplyCode != 227)
{
throw new IOException(strReply.Substring(4));
}
int index1 = strReply.IndexOf('(');
int index2 = strReply.IndexOf(')');
string ipData =
strReply.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 strReply: " +
strReply);
}
if (ch == ',' || i + 1 == len)
{
try
{
parts[partCount++] = Int32.Parse(buf);
buf = "";
}
catch (Exception)
{
throw new IOException("Malformed PASV strReply: " +
strReply);
}
}
}
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(IPAddress.Parse(ipAddress), port);
try
{
s.Connect(ep);
}
catch (Exception)
{
throw new IOException("Can't connect to remote server");
}
return s;
} /// <summary>
/// 发送命令并获取应答码和最后一行应答字符串
/// </summary>
/// <param name="strCommand">命令</param>
private void SendCommand(String strCommand)
{
//Byte[] cmdBytes =
// Encoding.ASCII.GetBytes((strCommand + "\r\n").ToCharArray());
byte[] cmdBytes = Encoding.GetEncoding("gb2312").GetBytes((strCommand + "\r\n").ToCharArray());
socketControl.Send(cmdBytes, cmdBytes.Length, 0);
ReadReply();
}
/// <summary>
/// 将一行应答字符串记录在strReply和strMsg
/// 应答码记录在iReplyCode
/// </summary>
private void ReadReply()
{
strMsg = "";
strReply = ReadLine();
iReplyCode = Int32.Parse(strReply.Substring(0, 3));
}
/// <summary>
/// 下载一批文件
/// </summary>
/// <param name="strFileNameMask">路径名+文件名的匹配字符串</param>
/// <param name="strFolder">本地目录(不得以\结束)</param>
public void Get(string strFileNameMask, string strFolder)
{
if (!bConnected)
{
Connect();
}
//获取文件目录
string[] strFiles = Dir(strFileNameMask);
OracleDataReader odr = null;
OracleCommand com = null;
OracleConnection con = DBhelper.getCon();
con.Open();
for (int i = 0; i < strFiles.Length ;i++ )
{
string sql = "select h.id from hl_DD_relation h where h.id = '" + strFiles[i] + "'";
com = new OracleCommand(sql, con);
odr = com.ExecuteReader();
if (odr.Read())
{
continue;
}
if (!strFiles[i].Equals(""))//一般来说strFiles的最后一个元素可能是空字符串
{
if (strFiles[i].IndexOf(".") < 0)
continue;
this.InsertDB(strFiles[i]);
Get(strFiles[i], strFolder, strFiles[i]);
this.updateDB(strFiles[i],DateTime.Now.ToString());
if (strFiles[i].IndexOf(".") != -1)
this.Delete(strFiles[i]);
}
int j = i;
j++;
if (j == strFiles.Length-1)
break;
}
con.Close();
odr.Close();
com.Dispose();
}如果目录下的文件数量大于2000个的话就会报标题的异常'Data connection closed transfer aborted' 但是,我用F11一步步调试的话,上万个文件名都能正常获取,如果调用Dir(即批量下载里面调用)方法用F10跳过的话就会报这个异常!这是为什么啊~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货