📄 ftpclientworker.java
字号:
package Client;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class FTPClientWorker {
private static Socket clientSocket=null;
private static ObjectInputStream in=null;
private static ObjectOutputStream out=null;
private static boolean loginFlag=false;
private static Message msgClient=new Message();
public FTPClientWorker(String ip,String port) throws IOException
{
try {
clientSocket=new Socket(ip,Integer.parseInt(port));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//out.flush();
}
public static boolean process() throws IOException
{
//CLIENT端输入登陆信息
String LoginCmd=null;
System.out.println("请用login [username] [password]格式登陆:");
BufferedReader buff1=new BufferedReader(new InputStreamReader(System.in));
LoginCmd=buff1.readLine();
msgClient=ClientCommandParser.parse(LoginCmd);
System.out.println("正在向服务器提交用户信息.....");
out=new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(msgClient);
out.flush();
//接收SERVER发来的信息
String inCmd=null;
ArrayList inParas = new ArrayList();
String inType = null;
try {
in=new ObjectInputStream(clientSocket.getInputStream());
msgClient=(Message)in.readObject();
inCmd = msgClient.command;
inParas = msgClient.paras;
inType = msgClient.type;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(inCmd.equals("loginOk"))
{
loginFlag=true;
System.out.println("登陆成功!");
System.out.println("您的SessionId是: "+ msgClient.paras.get(0));
System.out.println("您所在文件夹的目录是: ");
int i=1;
while(!msgClient.paras.get(i).equals("end"))
{
System.out.println("ftp>"+msgClient.paras.get(i));
i++;
}
}
if(inCmd.equals("loginFail"))
{
System.out.println("登陆失败!");
System.out.println("请用login [username] [password]格式重新登陆:");
BufferedReader buff2=new BufferedReader(new InputStreamReader(System.in));
LoginCmd=buff2.readLine();
//Message msgClient2=new Message();
msgClient = ClientCommandParser.parse(LoginCmd);
System.out.println("正在向服务器提交用户信息.....");
out.writeObject(msgClient);
out.flush();
}
while(loginFlag)
{
System.out.println("请选择您的操作:");
System.out.println("cd [SessionId] [filename]进入下一层文件夹");
System.out.println("cd.. [SessionId] 返回上级目录");
System.out.println("get [SessionId] [filename] 下载指定文件");
System.out.println("put [SESSIONID] [filename(绝对路径)] 上传指定文件到当前目录");
System.out.println("logout [SESSIONID] 注销用户");
System.out.println("close 关闭当前连接");
System.out.println("exit 退出程序");
//change CMD to object-Message and socket Out
BufferedReader buffcase=new BufferedReader(new InputStreamReader(System.in));
String choice=buffcase.readLine();
if(choice.indexOf("put")!=0) //all CMD parse except "put"
{
msgClient = ClientCommandParser.parse(choice);
System.out.println(msgClient.command);
System.out.println(msgClient.type);
System.out.println(msgClient.paras.get(0));
}
else //put 需要特别处理
{
System.out.println("now Deal with 'put' in client");
String[] temp=choice.split(" ");
msgClient.command = temp[0];//add CMD-put
msgClient.paras.add(0,temp[1]);//add pars1-sessionId
msgClient.paras.add(1,temp[2]);//add pars2-fileName
File f = new File(temp[2].toString());
msgClient.paras.add(2,f.getName());//add pars3-fileLength
FileInputStream fw = null;
byte [] fi=new byte[(int) f.length()];
try {
fw = new FileInputStream(f);
fw.read(fi);
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
msgClient.paras.add(3,fi);//add pars4-fileContent[output Stream]
msgClient.paras.add(4,"data end");//add pars5-end
msgClient.type = "DATA";
}
out.writeObject(msgClient);
out.flush();
//receive server message
//cd & cd..
// if((choice.indexOf("cd")==0)||(choice.indexOf("cd..")==0))
// {
if(inCmd.equals("cd")||inCmd.equals("cd.."))
{
System.out.println("当前文件目录为:");
// try {
// msgClient=(Message)in.readObject();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
int i=0;
while(!inParas.get(i).equals("end"))
{
System.out.println("ftp>"+inParas.get(i));
i++;
}
}
//get
else if(inCmd.equals("get"))
{
// try {
// msgClient=(Message)in.readObject();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
String downloadFilename = inParas.get(0).toString();
String downloadFileLength = inParas.get(1).toString();
FileOutputStream fw=new FileOutputStream(downloadFilename);
byte[] content=new byte[Integer.parseInt(downloadFileLength)];
int i = 0;
if(!inParas.get(i).toString().equals("data end"))
{
content = (byte[]) inParas.get(3);//[receive byte[]]
i++;
}
fw.write(content);
fw.close();
}
//put
else if(inCmd.equals("putOk")||inCmd.equals("putFail"))
{
// try {
// msgClient=(Message)in.readObject();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
if(inCmd.equals("putOk"))
{
System.out.println("文件上传成功!");
}
else
{
System.out.println("文件上传失败!");
}
}
//logout
else if(inCmd.equals("logoutOK")||inCmd.equals("logoutFail"))
{
// try {
// msgClient=(Message)in.readObject();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
if(inCmd.equals("logoutOk"))
{
loginFlag = false;
System.out.println("您已经注销成功!");
System.out.println("您可以继续登陆");
process();
}
else
{
System.out.println("注销失败!");
}
}
//close
else if(inCmd.equals("close"))
{
loginFlag = false;
// out.writeObject(choice);
// out.flush();
in.close();
out.close();
clientSocket.close();
System.out.println("若您希望新建一个连接,请输入open命令");
String openCmd=null;
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
try {
openCmd=buff.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(openCmd.equals("open"))
{
FTPClient.main(null);
}
}
else if(choice.indexOf("exit")==0)
{
loginFlag = false;
out.writeObject(choice);
out.flush();
in.close();
out.close();
clientSocket.close();
System.exit(0);
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -