package com.ftp;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import android.util.Log;
/**
* FTP.
* @author 张进
* @email kaixwdi110@163.com
*/
public class SimpleFTP {
/**
* 服务器名.
*/
private String hostName;
/**
* 用户名.
*/
private String userName;
/**
* 密码.
*/
private String password;
/**
* 套接字.
*/
private Socket socket;
/**
* 发送命令.
*/
private BufferedWriter writer;
/**
* 接受命令.
*/
private BufferedReader reader;
/**
* 端口号.
*/
private static final int PORT = 21;
/**
* 执行成功.
*/
private static final boolean DEBUG = true;
/**
* 构造函数.
* @param host hostName 服务器名
* @param user userName 用户名
* @param pass password 密码
*/
public SimpleFTP(String host, String user, String pass) {
this.hostName = host;
this.userName = user;
this.password = pass;
}
/**
* 打开FTP.
* @throws IOException
*/
public synchronized void connect() throws IOException {
if (socket != null) {
throw new IOException("SimpleFTP is already connected. Disconnect first.");
}
socket = new Socket(hostName, PORT);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = readCommand();
if (!response.startsWith("220 ")) {
throw new IOException("SimpleFTP received an unknown response when connecting to the FTP server: " + response);
}
sendCommand("USER " + userName);
response = readCommand();
if (!response.startsWith("331 ")) {
throw new IOException("SimpleFTP received an unknown response after sending the user: " + response);
}
sendCommand("PASS " + password);
response = readCommand();
if (!response.startsWith("230 ")) {
throw new IOException("SimpleFTP was unable to log in with the supplied password: " + response);
}
}
/**
* 断开FTP.
* @throws IOException
*/
public synchronized void disconnect() throws IOException {
try {
sendCommand("QUIT");
} finally {
socket = null;
}
}
/**
* FTP工作目录.
* @return 工作目录
* @throws IOException
*/
public synchronized String pwd() throws IOException {
sendCommand("PWD");
String dir = null;
String response = readCommand();
if (response.startsWith("257 ")) {
int firstQuote = response.indexOf('\"');
int secondQuote = response.indexOf('\"', firstQuote + 1);
if (secondQuote > 0) {
dir = response.substring(firstQuote + 1, secondQuote);
}
}
return dir;
}
/**
* 改变FTP工作目录.
* @param dir 工作目录
* @return boolean
* @throws IOException
*/
public synchronized boolean cwd(String dir) throws IOException {
sendCommand("CWD " + dir);
String response = readCommand();
return response.startsWith("250 ");
}
/**
* 二进制发送文件.
* @return boolean
* @throws IOException
*/
private synchronized boolean bin() throws IOException {
sendCommand("TYPE I");
String response = readCommand();
return response.startsWith("200 ");
}
/**
* ASCII发送文件.
* @return boolean
* @throws IOException
*/
private synchronized boolean ascii() throws IOException {
sendCommand("TYPE A");
String response = readCommand();
return response.startsWith("200 ");
}
/**
* 上传文件.
* @param file 文件目录
* @return boolean
* @throws IOException
*/
// public synchronized boolean stor(File file) throws IOException {
// if (file.isDirectory()) {
// throw new IOException("SimpleFTP cannot upload a directory.");
// }
// String fileName = file.getName();
// return stor(new FileInputStream(file), fileName);
// }
/**
* 上传文件.
* @param inputStream 输出流
* @param fileName 文件名
* @return boolean
* @throws IOException
*/
public synchronized boolean stor(String localPath, String remotePath) throws IOException {
cwd(remotePath);
File file = new File(localPath);
String fileName = file.getName();
InputStream inputStream = new FileInputStream(file);
BufferedInputStream input = new BufferedInputStream(inputStream);
Socket dataSocket = getConnection();
sendCommand("STOR " + fileName);
String response = readCommand();
if (!response.startsWith("125 ")) {
throw new IOException("SimpleFTP was not allowed to send the file: " + response);
}
BufferedOutputStream output = new BufferedOutputStream(dataSocket.getOutputStream());
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
input.close();
response = readCommand();
return response.startsWith("226 ");
}
/**
* 下载文件.
* @param fileName 文件名.
* @param saveName 本地目录.
* @return boolean
* @throws IOException
*/
public synchronized boolean get(String localPath, String remotePath, String fileName) throws IOException {
String flow = null;
Socket dataSocket = getConnection();
cwd(remotePath);
sendCommand("RETR " + fileName);
String response = readCommand();
flow = response;
if (!response.startsWith("125 ")) {
throw new IOException("SimpleFTP was not allowed to get the file: " + response);
}
FileOutputStream outputStream = new FileOutputStream(new File(localPath));
BufferedInputStream inputStream = new BufferedInputStream(dataSocket.getInputStream());
int n;
byte[] buff = new byte[1024];
while ((n = inputStream.read(buff)) > 0) {
outputStream.write(buff, 0, n);
}
dataSocket.close();
outputStream.close();
response = readCommand();
flow += response;
System.out.println("flow: " + flow.getBytes().length);
return response.startsWith("226 ");
}
/**
* 列出FTP目录下文件.
* @return 文件集合.
* @throws IOException
*/
public synchronized List<String> listFile() throws IOException {
Socket dataSocket = getConnection();
sendCommand("LIST ");
List<String> result = new ArrayList<String>();
int n;
byte[] buff = new byte[65536];
BufferedInputStream inputStream = new BufferedInputStream(dataSocket.getInputStream());
while ((n = inputStream.read(buff)) > 0) {
System.out.write(buff, 0, n);
result.add(new Stri
- 1
- 2
- 3
- 4
前往页