simpleFtp 问题

来源:互联网 发布:js点击切换div内容 编辑:程序博客网 时间:2024/06/10 10:23

1、设置连接超时

socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(host, port);
socket.connect(socketAddress, 5000);

2、获取列表信息


mDataSocket = getConnection();
sendLine("LIST ");
String response = readLine();
// 构造传输文件用的数据流
BufferedInputStream bis = new BufferedInputStream(mDataSocket
.getInputStream());
// 接收来自服务器的数据,写入本地文件
int n;
byte[] buff = new byte[1024];
while ((n = bis.read(buff)) > 0) {
String str = new String(buff,0,n);
Log.d(TAG, ""+ str);
}



private Socket getConnection() throws IOException {
sendLine("PASV");
String response = readLine();
Log.d("FTP", "getConnection response:" + response);
if (response == null || !response.startsWith("227 ")) {
throw new
IOException("SimpleFTP could not request passive mode: "
+ response);
}


String ip = null;
int port = -1;
int opening = response.indexOf('(');
int closing = response.indexOf(')', opening + 1);
if (closing > 0) {
String dataLink = response.substring(opening + 1, closing);
StringTokenizer tokenizer = new StringTokenizer(dataLink, ",");
try {
ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "."
+ tokenizer.nextToken() + "." + tokenizer.nextToken();
port = Integer.parseInt(tokenizer.nextToken()) * 256
+ Integer.parseInt(tokenizer.nextToken());
} catch (Exception e) {
throw new IOException(
"SimpleFTP received bad data link information: "
+ response);
}
}
Socket dataSocket = new Socket(ip, port);
return dataSocket;
}

原创粉丝点击