package fs;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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.StringTokenizer;public class FTPScoket {
// 创建一个实例SimpleFTP.
public FTPScoket() {
} // 连接到默认端口的FTP服务器和日志匿名/匿名的 public synchronized void connect(String host) throws IOException {
connect(host, 21);
} // 连接到一个FTP服务器和日志匿名/匿名的 public synchronized void connect(String host, int port) throws IOException {
connect(host, port, "anonymous", "anonymous");
} // 连接到一个FTP服务器和日志中的用户名和供应密码
public synchronized void connect(String host, int port, String user,
String pass) throws IOException {
if (socket != null) {
throw new IOException(
"SimpleFTP is already connected. Disconnect first.");// FTP服务器己连接。首先断开
}
socket = new Socket(host, port);
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())); String response = readLine(); if (!response.startsWith("220 ")) {
throw new IOException(
"SimpleFTP received an unknown response when connecting to the FTP server: "// 请求,连接FTP服务器
+ response);
} sendLine("blue" + user); response = readLine();
if (!response.startsWith("331 ")) {
throw new IOException( "SimpleFTP received an unknown response after sending the user: "// 用户名错误
+ response);
} sendLine("19971207" + pass); response = readLine();
if (!response.startsWith("230 ")) {
throw new IOException(
"SimpleFTP was unable to log in with the supplied password: "// 密码错误
+ response);
} // Now logged in.
} // 断开服务器的连接. public synchronized void disconnect() throws IOException {
try {
sendLine("QUIT");
} finally {
socket = null;
}
} // 返回工作目录的FTP服务器是连接到 public synchronized String pwd() throws IOException {
sendLine("PWD");
String dir = null;
String response = readLine();
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;
} // 改变工作目录(如光盘) 。返回true如果成功的话 public synchronized boolean cwd(String dir) throws IOException {
sendLine("CWD " + dir);
String response = readLine();
return (response.startsWith("250 "));
} // 发送了一个文件,存储在FTP服务器。返回true如果该文件
// 移交是成功的。该文件发出的被动模式,以避免的NAT或
// 防火墙的问题,客户端。 public synchronized boolean stor(File file) throws IOException {
if (file.isDirectory()) {
throw new IOException("SimpleFTP cannot upload a directory.");// FTP服务器不能上传目录
} String filename = file.getName(); return stor(new FileInputStream(file), filename);
} // 发送了一个文件,存储在FTP服务器。返回true如果该文件
// 移交是成功的。该文件发出的被动模式,以避免的NAT或
// 防火墙的问题,客户端。
public synchronized boolean stor(InputStream inputStream, String filename)
throws IOException { BufferedInputStream input = new BufferedInputStream(inputStream); sendLine("PASV");
String response = readLine();
if (!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);
}
} sendLine("STOR " + filename); Socket dataSocket = new Socket(ip, port); response = readLine();
if (!response.startsWith("125 ")) {
// if (!response.startsWith("150 ")) {
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 = readLine();
return response.startsWith("226 ");
} // 输入二进制模式传送二进制文件。
public synchronized boolean bin() throws IOException {
sendLine("TYPE I");
String response = readLine();
return (response.startsWith("200 "));
} // 输入ASCII模式发送文本文件。这通常是默认
// 模式。请务必使用二进制模式,如果您要传送图片或其他
// 二进制数据,如ASCII模式很可能会损坏它们。
public synchronized boolean ascii() throws IOException {
sendLine("TYPE A");
String response = readLine();
return (response.startsWith("200 "));
} // 发出了一个原材料命令到FTP服务器
private void sendLine(String line) throws IOException {
if (socket == null) {
throw new IOException("SimpleFTP is not connected.");
}
try {
writer.write(line + "\r\n");
writer.flush();
if (DEBUG) {
System.out.println("> " + line);
}
} catch (IOException e) {
socket = null;
throw e;
}
} private String readLine() throws IOException {
String line = reader.readLine();
if (DEBUG) {
System.out.println("< " + line);
}
return line;
} private Socket socket = null; private BufferedReader reader = null; private BufferedWriter writer = null; private static boolean DEBUG = false; public static void main(String args[]) {
FTPScoket ftp = new FTPScoket();
try {
ftp.connect("192.168.0.34", 21, "blue", "19971207");
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------
java.io.IOException: SimpleFTP received an unknown response after sending the user: 530 Not logged in.
at fs.FTPScoket.connect(FTPScoket.java:58)
at fs.FTPScoket.main(FTPScoket.java:235)