}
}
/**
* 从FTP服务器上下载文件,支持断点续传,下载百分比汇报
* @param remote 远程文件路径及名称
* @param local 本地文件完整绝对路径
* @return 下载的状态
* @throws IOException
*/
public DownloadStatus download(String remote, String local)
throws IOException {
// 设置被动模式
ftpClient.enterLocalPassiveMode();
// 设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;
// 检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(
remote.getBytes("GBK"), "iso-8859-1"));
if (files.length != 1) {
log.info("远程文件不存在");
return DownloadStatus.RemoteFileNotExist;
}
long lRemoteSize = files[0].getSize();
File f = new File(local);
// 本地存在文件,进行断点下载
if (f.exists()) {
long localSize = f.length();
// 判断本地文件大小是否大于远程文件大小
if (localSize >= lRemoteSize) {
log.info("本地文件大于远程文件,下载中止");
return DownloadStatus.LocalFileBiggerThanRemoteFile;
}
// 进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("GBK"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
step = step==0 1:step;//文件过小,step可能为0
long process = localSize / step;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0){
log.info("下载进度:" + process);
}
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
result = DownloadStatus.DownloadFromBr