设为首页 收藏本站
查看: 1087|回复: 0

[经验分享] Java写的支持断点续传的FTP

[复制链接]

尚未签到

发表于 2016-6-10 07:20:09 | 显示全部楼层 |阅读模式
  http://crybaby2005.bokee.com/3148701.html
  

public class FileInfo {
public String name;
public String description;
public FileInfo(String nm, String dscrp) {
name = nm;
description = dscrp;
}
}
  

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.Vector;
public class FTPSearch extends Thread {
String _host;
static final int PORT = 21;
ServerSocket ss;
Socket _ds;
Socket _sc;
PrintStream _out;
String _hostAddress;
BufferedReader _in;
BufferedReader _dataIn;
String _reply;
protected StringBuffer _log = new StringBuffer(10000);
Vector _result;
String _user;
String _pass;
public FTPSearch(String ip, String user, String pass) {
_host = ip;
_user = user;
_pass = pass;
_result = new Vector();
}
public FTPSearch(String ip) {
_host = ip;
_user = "anonymous";
_pass = "name_zm@sohu.com";
_result = new Vector();
}
protected boolean login() throws UnknownHostException, IOException {
boolean successful = false;
StringBuffer reply = new StringBuffer();
_sc = new Socket(_host, PORT);
_hostAddress = _sc.getLocalAddress().getHostAddress().replace('.', ',');
_sc.setSoTimeout(15000);
_out = new PrintStream(_sc.getOutputStream(), true);
_in = new BufferedReader(new InputStreamReader(_sc.getInputStream()));
reply.append(readReply().trim());
if (reply.toString().startsWith("220"))
if (comm("USER " + _user))
if (comm("PASS " + _pass))
successful = true;
comm("TYPE A");
return successful;
}
protected String getCurrentDir() throws IOException {
String currentDir = null;
if (comm("PWD")) {
StringTokenizer st = new StringTokenizer(_reply);
st.nextToken();
StringBuffer rtDir = new StringBuffer(st.nextToken());
currentDir = rtDir.substring(1, rtDir.length() - 1);
}
return currentDir;
}
public void search() throws IOException, InterruptedException {
try {
for (int i = 0; i < 5; i++) {
if (login())
break;
System.out.println("Wait 10 seconds to try again...");
sleep(10000);
}
scan();
logout();
} catch (IOException ex) {
} finally {
PrintStream out = new PrintStream(new FileOutputStream("Log.txt"));
out.println(_log);
out.close();
printResult("result.txt");
}
}
protected void scan() throws IOException {
Vector fileNames = new Vector();
tellPort();
comm("LIST -R");
// Get data_connection's InputStream.
try {
_ds = ss.accept();
System.out.println(ss.toString());
} catch (IOException ex) {
ss.close();
}
_dataIn = new BufferedReader(
new InputStreamReader(_ds.getInputStream()));
System.out.println("Scanning now.Please waiting......");
// Read directory content
try {
String temp = "";
do {
temp = _dataIn.readLine();
if (temp == null)
break;
fileNames.add(temp);
} while (temp != null);
} catch (InterruptedIOException ex) {
System.out.println("Caught InterruptedIOException1:" + ex);
}
// Read end infomation
try {
do {
String temp = _in.readLine();
_log.append(temp + "r");
System.out.println(temp);
} while (_in.ready());
} catch (InterruptedIOException ex) {
System.out.println("Caught InterruptedIOException2:" + ex);
}
_result = fileNames;
}
public Vector getResult() {
return _result;
}
public StringBuffer getLog() {
return _log;
}
public static Vector parse() throws FileNotFoundException, IOException {
Vector result = new Vector();
FileInputStream fileIn = new FileInputStream("result.txt");
BufferedReader fReader = new BufferedReader(new InputStreamReader(
fileIn));
// int size=fileIn.available();
// System.out.println(String.valueOf(size));
String dir = "/";
System.out.println("Parsing now.Please wait......");
while (fReader.ready()) {
String temp = fReader.readLine();
StringTokenizer tt = new StringTokenizer(temp);
if (tt.countTokens() == 1) {
if (temp.startsWith("/"))
dir = temp.replace(':', ' ').trim() + "/";
else
dir = "/" + temp.replace(':', ' ').trim() + "/"; // parseResult
// .add(
// dir);
// continue
// ;
}
if ((!temp.equals(""))
&& (temp.startsWith("-") || temp.startsWith("b")
|| temp.startsWith("c") || temp.startsWith("l"))) {
for (int i = 0; i < 4; i++)
tt.nextToken();
String description = "";
for (int i = 0; i < 4; i++)
description = description + tt.nextToken() + " ";
String cont = "";
do {
cont = cont + tt.nextToken() + " ";
} while (tt.hasMoreTokens());
result.add(new FileInfo(dir + cont.trim(), description.trim()));
continue;
} else if (temp.startsWith("d")) {
for (int i = 0; i < 4; i++)
tt.nextToken();
String description = "";
for (int i = 0; i < 4; i++)
description = description + tt.nextToken() + " ";
String cont = "";
do {
cont = cont + tt.nextToken() + " ";
} while (tt.hasMoreTokens());
result.add(new FileInfo(dir + cont.trim() + "/", description
.trim()));
continue;
} else if (temp.equals("")) {
continue;
}
}
return result;
}
protected void logout() throws IOException {
comm("QUIT");
}
protected void printResult(String resultFile) throws IOException {
PrintStream out = new PrintStream(new FileOutputStream(resultFile));
int len = _result.size();
for (int i = 0; i < len; i++) {
out.println(_result.elementAt(i));
}
out.close();
}
protected boolean comm(String command) throws IOException {
boolean success = false;
_out.println(command);
System.out.println("<COMMAND>" + command);
_log.append("<COMMAND>" + command + "r");
_reply = readReply();
if (command.startsWith("USER")) {
success = _reply.startsWith("331") ? true : false;
} else if (command.startsWith("PASS")) {
success = _reply.startsWith("230") ? true : false;
try {
readReply();
} catch (InterruptedIOException ex) {
}
} else if (command.equals("TYPE A")) {
success = _reply.startsWith("200") ? true : false;
} else if (command.startsWith("PORT")) {
success = _reply.startsWith("200") ? true : false;
} else if (command.startsWith("LIST")) {
success = _reply.startsWith("150") ? true : false;
} else if (command.startsWith("NLST")) {
success = _reply.startsWith("150") ? true : false;
} else if (command.equals("PWD")) {
success = _reply.startsWith("257") ? true : false;
} else if (command.startsWith("STAT")) {
success = _reply.startsWith("211") ? true : false;
} else if (command.equals("SYST")) {
success = _reply.startsWith("215") ? true : false;
} else if (command.startsWith("SITE")) {
success = _reply.startsWith("214") ? true : false;
} else if (command.equals("QUIT")) {
success = _reply.startsWith("221") ? true : false;
}
return success;
}
public static void printVector(Vector vct) {
int size = vct.size();
for (int i = 0; i < size; i++) {
System.out.println(vct.elementAt(i));
}
}
protected String readReply() throws IOException, InterruptedIOException {
StringBuffer reply = new StringBuffer();
do {
reply.append(_in.readLine() + "r");
} while (_in.ready());
System.out.println(reply);
_log.append(reply);
_reply = reply.toString();
return _reply;
}
private int getDataPort() {
int port = _sc.getLocalPort() + 1;
return port;
}
private void tellPort() throws IOException {
try {
int port = getDataPort();
System.out.println("Get data port:" + port);
int x = (int) Math.round((port / 256) - 0.5);
int y = port - x * 256;
try {
ss = new ServerSocket(port);
} catch (IOException ex) {
System.out
.println("IOException in Method tellPort() in Class ftpc--"
+ ex);
}
comm("PORT " + _hostAddress + "," + x + "," + y);
} finally {
}
}
public static void main(String arg[]) {
try {
FTPSearch client = new FTPSearch("202.114.2.2");
client.search();
client.parse();
} catch (Exception ex) {
System.out.println(ex);
}
}
}
  

public class demo {
public static void main(String arg[]) {
try {
FTPSearch client = new FTPSearch("192.168.0.1");
client.search();
java.util.Vector temp = client.parse();
int size = temp.size();
for (int i = 0; i < size; i++) {
FileInfo ttt = ((FileInfo) temp.elementAt(i));
System.out.println(ttt.name + " " + ttt.description);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-228394-1-1.html 上篇帖子: 开源组件sunFtp的ftp上传下载 下篇帖子: ftp协议详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表