package spiderFTP;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpDownload {
private FTPClient ftpClient = new FTPClient();
public boolean connect(String ftp, String localDir){
String[] str = ftp.replace("ftp://", "").split("/");
String hostname = str[0];
int port = 21;
String username = "anonymous";
String password = "12@download";
localDir = localDir.endsWith("/")? localDir : localDir+"/";
String remote ="/";
for(int i = 1; i < str.length; i++){
remote += str[i] + "/";
}
try {
connect(hostname, port, username, password);
download(remote, localDir);
disconnect();
} catch (IOException e) {
System.out.println("连接FTP出错:"+e.getMessage());
e.printStackTrace();
}
return false;
}
/**
* 连接到FTP服务器
* @param hostname 主机名
* @param port 端口
* @param username 用户名
* @param password 密码
* @return 是否连接成功
* @throws IOException
*/
public boolean connect(String hostname,int port,String username,String password) throws IOException{
System.out.println(hostname +" , "+port+" , "+username+" , "+password);
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
return true;
}
}
disconnect();
return false;
}
/**
* 断开与远程服务器的连接
* @throws IOException
*/
public void disconnect() throws IOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}
/**
* 从FTP服务器上下载文件,支持断点续传,上传百分比汇报
* @param remote 远程文件路径
* @param local 本地文件路径
* @return 上传的状态
* @throws IOException
*/
public void download(String remote,String local) throws IOException{
System.out.println("remote,local:"+remote+ " , "+local);
//设置被动模式
ftpClient.enterLocalPassiveMode();
//设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;
//检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes("GBK"),"iso-8859-1"));
System.out.println("共有文件: "+files.length +" 个");
// 检查并创建本地目录
for(int i = 0; i < files.length; i++){
result = downloadFile(files[i], remote, local);
// if()
}
// long lRemoteSize = files[0].getSize();
// System.out.println(files[0].getName());
//本地存在文件,进行断点下载
}
private DownloadStatus downloadFile(FTPFile remoteFile,String remoteDir, String local) throws IOException{
DownloadStatus result;
File f = new File(local + remoteFile.getName());
if(!f.getParentFile().exists() || !f.getParentFile().isDirectory()){
f.getParentFile().mkdirs();
}
long lRemoteSize = remoteFile.getSize();
System.out.println("=====下载======"+remoteFile.getName()+"=======");
if(f.exists()){
long localSize = f.length();
//判断本地文件大小是否大于远程文件大小
if(localSize >= lRemoteSize){
System.out.println("本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}
//进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String((remoteDir+remoteFile.getName()).getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
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)
System.out.println("下载进度:"+process+"%");
//TODO 更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if(isDo){
result = DownloadStatus.Download_From_Break_Success;
}else {
result = DownloadStatus.Download_From_Break_Failed;
}
}else {
f.createNewFile();
OutputStream out = new FileOutputStream(f);
InputStream in= ftpClient.retrieveFileStream(new String((remoteDir+remoteFile.getName()).getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=0;
long localSize = 0L;
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)
System.out.println("下载进度:"+process+"%");
//TODO 更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if(upNewStatus){
result = DownloadStatus.Download_New_Success;
}else {
result = DownloadStatus.Download_New_Failed;
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FtpDownload myFtp = new FtpDownload();
myFtp.connect("ftp://10.24.189.70/11/AA/","e:/temp/delete2");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
网络数据扒取,http ftp 分类扒取
共21个文件
class:10个
java:7个
project:1个
3星 · 超过75%的资源 需积分: 16 22 下载量 35 浏览量
2011-12-22
13:40:37
上传
评论 2
收藏 26KB RAR 举报
温馨提示
网络数据扒取,http ftp 分类扒取。能够扒取网站连接,网站所需下载的地方。有详细步骤。详细代码。。。
资源推荐
资源详情
资源评论
收起资源包目录
spider.rar (21个子文件)
spider
temp
www.baidu.com_gaoji_preferences.html.html 7KB
src
spiderHTML
LinkFilter.java 194B
HtmlParserTool.java 3KB
FileDownLoader.java 4KB
spiderFTP
DownloadStatus.java 420B
FtpDownload.java 7KB
FileDownLoader.java 4KB
testSpider
ZhengHe.java 1KB
bin
spiderHTML
HtmlParserTool$1.class 837B
FileDownLoader.class 4KB
HtmlParserTool.class 3KB
HtmlParserTool$2.class 737B
LinkFilter.class 153B
spiderFTP
FtpDownload.class 6KB
FileDownLoader.class 4KB
DownloadStatus.class 1KB
testSpider
ZhengHe.class 2KB
ZhengHe$1.class 682B
.classpath 2KB
.settings
org.eclipse.jdt.core.prefs 629B
.project 382B
共 21 条
- 1
资源评论
- 我是小兵33662013-09-09觉得还可以,但是不是我想要的。.
axing925
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功