我有Commons Net FTP文件问题。我有一个应用程序可以将单个文件或目录传输到远程主机,具体取决于用户在JTree中单击的内容。当我传输特定目录时,传输始终在同一文件上失败,并出现“软件导致连接中止:套接字写入错误”。当我选择相同的文件并将其作为单个文件传输时,它工作正常。目录传输的相关代码是:
BufferedOutputStream bos = new BufferedOutputStream(os,8*1024);
if (filelen > 500){
buffer = new byte[(int) (filelen/100)];
}else {
buffer = new byte[(int) filelen];
}
int len;
BufferedInputStream bis = new BufferedInputStream(is,8*1024);
int ba = bis.available();
while ((len = bis.read(buffer)) != -1 && ba != 0)
{
//os.write(buffer, 0, len);
System.out.println(ftp.getReplyString());
System.out.println("len is "+len);
System.out.println("ba is "+ba);
System.out.println("first two bytes are "+buffer[0]+buffer[1]);
System.out.println("last byte is "+buffer[buffer.length-1]);
System.out.println("buffer length is "+buffer.length);
int rc = ftp.getReplyCode();
if (rc == 550){
hftstatus.setText("Copy Error encountered, check log.");
return;
}
bos.write(buffer,0, len);
bos.flush();
System.out.println(ftp.getReplyString());
ba = bis.available();
if (filelen > 500){
if (len > 0){
left -=len;
totleft -=len;
}
}else{
left=0;
}
if (ba < buffer.length){
buffer = new byte[ba];
}
double ip = (left*100)/filelen;
int percomp = (int) (100-ip);
double ap = (totleft*100)/totlength;
int perallcomp = (int) (100-ap);
hftcurrfileprog.setValue(percomp);
hftallfileprog.setValue(perallcomp);
hftstatus.setText("Copying file "+f+" "+percomp+" percent completed.");
}
并且单个文件的相关代码是:
BufferedOutputStream bos = new BufferedOutputStream(os,8*1024);
if (filelen > 500){
buffer = new byte[(int) (filelen/100)];
}else {
buffer = new byte[(int) filelen];
}
int len;
while ((len = bis.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
int rc = ftp.getReplyCode();
if (rc == 550){hftstatus.setText("Copy Error encountered, check log.");}
bos.flush();
bytestran += len;
if (filelen > 100){
left = left-(filelen/100);
}else{
left=0;
}
double ip = (left*100)/filelen;
int percomp = (int) (100-ip);
hftcurrfileprog.setValue(percomp);
hftallfileprog.setValue(percomp);
if (bytestran == filelen){break;}
}
String outm = ftp.getReplyString();
System.out.println(outm);
bos.flush();
bos.close();
bis.close();
os.close();
is.close();
ftp.completePendingCommand();
System.out.println(ftp.getReplyString());
totleft = 0;
double ip = (left*100)/filelen;
int percomp = (int) (100-ip);
int perallcomp = 100;
hftcurrfileprog.setValue(percomp);
hftallfileprog.setValue(perallcomp);
hftstatus.setText("file "+fnode+" copied to directory "+remoteDirPath+nwd);
任何想法或线索都会非常感激。我有很多点击这个,但没有一个唯一的区别在于目录或单个文件传输。谢谢你的帮助。