最近使用apache上面的commons 2.0开发ftp使用过程中,因为涉及到大文件的长时间传输,在最后经常导致程序死掉,不知道大家有没有办法解决。
其中传文件的代码如下:
如果上传大的文件,比如说是500M文件,ftp传输大概需要两个小时左右的时候,ftpClient.completePendingCommand()方法就会死掉,我在网上也查了一些内容,说是打开的流必须关闭后ftp Server才会返回正确的状态吗。问题是我关闭了同样没有收到正确的状态码。
如果换成appendFile(String remoteFile,InputStream in)这种方法就不会存在问题,
因为项目需要汇报进度,所以必须使用流的方式读取,希望知道解决方案的兄弟帮忙,多谢!
其中传文件的代码如下:
/**
* 上传文件到服务器,新上传和断点续传
* @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
* @param localFile 本地文件File句柄,绝对路径
* @param processStep 需要显示的处理进度步进值
* @param ftpClient FTPClient引用
* @return
* @throws IOException
*/
public boolean uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{
boolean status;
//显示进度的上传
long step = localFile.length() / 100;
long process = 0;
long localreadbytes = 0L;
RandomAccessFile raf = new RandomAccessFile(localFile,"r");
OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"),"iso-8859-1"));
//断点续传设置起始位置
if(remoteSize>0){
ftpClient.setRestartOffset(remoteSize);
process = remoteSize /step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
//开始上传
while((c = raf.read(bytes))!= -1){
out.write(bytes,0,c);
localreadbytes+=c;
if(localreadbytes / step != process){
process = localreadbytes / step;
// System.out.println("上传进度:" + process);
//TODO 添加汇报上传状态内容
}
}
logger.info("清空输出流");
out.flush();
logger.info("关闭文件读句柄");
raf.close();
logger.info("关闭输出流");
out.close();
logger.info("关闭输出流成功");
boolean status = ftpClient.completePendingCommand();
logger.info("从上传方法返回");
return status;
}
如果上传大的文件,比如说是500M文件,ftp传输大概需要两个小时左右的时候,ftpClient.completePendingCommand()方法就会死掉,我在网上也查了一些内容,说是打开的流必须关闭后ftp Server才会返回正确的状态吗。问题是我关闭了同样没有收到正确的状态码。
如果换成appendFile(String remoteFile,InputStream in)这种方法就不会存在问题,
因为项目需要汇报进度,所以必须使用流的方式读取,希望知道解决方案的兄弟帮忙,多谢!