【文件属性】:
文件名称:FTP下载源码(根据您指定的路径名下载资源到您指定的路径中)
文件大小:5KB
文件格式:JAVA
更新时间:2013-06-27 05:11:27
FTP下载 根据FTP路径下载
代码简洁又实用
/**
* 断点下载文件
*/
public boolean download(String remote, String local) throws IOException {
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
boolean result;
File f = new File(local);
File ffinish = new File(local.substring(0, local.lastIndexOf(".")));
// 根据remote获取ftp上的文件名
FTPFile[] files = ftpClient.listFiles(remote);
if (files.length != 1) {
return false;
}
long lRemoteSize = files[0].getSize();
// 检查本地文件是否存在
if (ffinish.exists()) {
return false;
}
if (f.exists()) {
OutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(f.length());
result = ftpClient.retrieveFile(remote, out);
out.close();
} else {
OutputStream out = new FileOutputStream(f);
result = ftpClient.retrieveFile(remote, out);
out.close();
}
// 文件下载中止
if (f.length() >= lRemoteSize) {
f.renameTo(new File(local.substring(0, local.lastIndexOf("."))));
}
return result;
}
网友评论
- 用处不大,不是自己想要。
- 下载的时候没有注意不是用C语言写的,所以没有看。
- 很不错,修改了一下可以用
- 还有不有上传的处理啊
- 下载的时候没有注意不是用C语言写的,所以没有看。
- 没用上,建议发的代码最好有个例子,要不看起来比较费劲。谢谢分享。