import java.io.*;
import java.net.*;
import java.util.*;
public class FClient {
private static FClient f;
private Socket Sock;
private String strip;
private int Port;
private PrintStream out;
private BufferedReader in;
private PrintWriter dataout;
public FClient(String str, int port) throws IOException {
strip = str;
Port = port;
Sock = new Socket(strip, Port);
}
public static void main(String args[]) throws IOException {
String strSendCmd;
String strRec;
try {
String strip = "192.168.0.33";
FClient ftpClient = new FClient(strip,21);
f = ftpClient;
//初始化 控制通道
ftpClient.InitSockStream(ftpClient.Sock);
//获得控制通道的信息
System.out.println(ftpClient.in.readLine());
//登陆到ftp
ftpClient.BuildConnection(ftpClient);
//获取服务器端的文件目录
ArrayList fileList = ftpClient.GetFileList(ftpClient);
//处理用户的指令
ftpClient.ftpCmd();
} catch (IOException E) {
System.out.println(E.toString());
}
}
// 初始化控制通道
public void InitSockStream(Socket Sock) throws IOException {
out = new PrintStream(Sock.getOutputStream());
in = new BufferedReader(new InputStreamReader(Sock.getInputStream()));
}
//登陆到ftp
public void BuildConnection(FClient ftpClient) throws IOException {
String strSendCmd;
//输入用户名
strSendCmd = "USER zealear";
ftpClient.SendCommand(strSendCmd);
//输入密码
strSendCmd = "PASS sysu";
ftpClient.SendCommand(strSendCmd);
//输入格式控制模式
//strSendCmd = "TYPE A";
//ftpClient.SendCommand(strSendCmd);
}
//获得服务器上的目录或者文件列表
public ArrayList GetFileList(FClient ftpClient) throws IOException {
//设置模式PASV or PORT
ArrayList FileList = new ArrayList();
String strSendCmd = "PASV";
String strRec = ftpClient.SendCommand(strSendCmd);
Socket Client = ftpClient.CreateDataSocket(ftpClient.GetPort(strRec));//////////////////////////
strSendCmd = "NLST";
ftpClient.SendCommand(strSendCmd);
PrintStream outData;
BufferedReader inData;
//获得数据通道传出的数据
outData = new PrintStream(Client.getOutputStream());
inData = new BufferedReader(new InputStreamReader(Client
.getInputStream()));
String s;
while ((s = inData.readLine()) != null) {
System.out.println(s);
FileList.add(s);
}
ftpClient.SockClose(Client);//关闭数据通道
System.out.println(ftpClient.in.readLine()); //获得控制通道的信息
return FileList;
}
//在PORT或者PASV模式的返回值中获取端口
public String[] GetPort(String ss) {
int iPort = ss.indexOf("(");
int ePort = ss.lastIndexOf(")");
String tt = ss.substring(iPort + 1, ePort);////////////////////////////////////////////////////
String[] kk = null;
kk = tt.split(",");
return kk;
}
//创建数据连接socket
public Socket CreateDataSocket(String[] strArgu) throws IOException {
int Port = Integer.parseInt(strArgu[4]) * 256
+ Integer.parseInt(strArgu[5]);
String StrIP = strArgu[0] + "." + strArgu[1] + "." + strArgu[2] + "."
+ strArgu[3];
Socket Client = new Socket(StrIP, Port);
return Client;
}
//处理用户输入
public String userIn() throws IOException {
String strCmd;
InputStreamReader ir;
BufferedReader in;
ir = new InputStreamReader(System.in);
in = new BufferedReader(ir);
System.out.println("Enter:");
strCmd = in.readLine();
return strCmd;
}
//向控制通道发送命令
public String SendCommand(String strcmd) throws IOException {
out.println(strcmd);//发送控制命令
out.flush();
String strRec = in.readLine();
System.out.println(strRec);//输出控制器返回的消息
return strRec;
}
//用户的输入指令,包括:下载、上传文件
public void ftpCmd() throws IOException {
while (true) {
String userIn = f.userIn();
String[] ss = userIn.split(" ");
if (userIn.equalsIgnoreCase("close")) {
//退出ftp
String strRec = f.SendCommand("QUIT");
f.SockClose(f.Sock);//关闭控制通道
break;
} else if (ss[0].equalsIgnoreCase("get") && ss.length == 2) {
//下载文件到本地
} else if (ss[0].equalsIgnoreCase("put") && ss.length == 2) {
// 上传文件到服务器
f.up(f);
} else {
System.out.println("请输入合法的字符");
}
}
}
// 下载文件
public void down(FClient f) throws IOException{
String strRec = f.SendCommand("PASV");
Socket Client = f.CreateDataSocket(f.GetPort(strRec));
//.....
}
//上传文件
public void up(FClient f) throws IOException{
f.SendCommand("TYPE A");
String strRec = f.SendCommand("PASV");
Socket Client = f.CreateDataSocket(f.GetPort(strRec));
f.SendCommand("STOR haha.txt");
RandomAccessFile infile=new RandomAccessFile("D:/haha.txt","r");
PrintWriter dataout = new PrintWriter(Client.getOutputStream(),true);
String ss=infile.readLine();
while(ss!=null){
dataout.println(ss);
dataout.flush();
System.out.println(ss);
ss=infile.readLine();
}
}
//关闭socket
public void SockClose(Socket sock) throws IOException {
if (sock != null) {
sock.close();
sock = null;
}
}
}
上面的IP地址、用户名和密码、以及要上传的文件名和它的路径。红色的字体可以改成你想要的。
我在测试的时候,为什么那个haha.txt文件很小(〈40K)左右的时候就无法传出去,只有文件很大的时候才能传,而且传出去的文件也是不完整的。。。。如果有中文字体还出现乱码。。。大家一起想想办法吧。。。