📄 ftpclientframe.java
字号:
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
//显示当前目录
public void doPwd()
{
try{
ctrlOutput.println("PWD ") ;// PWD命令
ctrlOutput.flush() ;
//textarea.append(ctrlInput.readLine()) ;
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
//doCdup
//上一层目录
public void doCdup()
{
try{
ctrlOutput.println("CDUP ") ;// CDUP命令
ctrlOutput.flush() ;
//textarea.append(ctrlInput.readLine()) ;
doLs();
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
// doAscii方法
// 设置文本传输模式
public void doAscii()
{
try{
ctrlOutput.println("TYPE A") ;// A模式
ctrlOutput.flush() ;
//textarea.append(ctrlInput.readLine()) ;
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
// doGet方法
// 取得服务器上的文件
public void doGet()
{
String fileName = "" ;
try{
int n ;
byte[] buff = new byte[1024] ;
FileOutputStream outfile;
// 指定报务器上文件的名
fileName = list.getSelectedItem() ;
// 在客户端上准备存储其内容的文件
try{
outfile = new FileOutputStream(fileName) ;
//BufferedOutputStream outfile = new BufferedOutputStream(new FileOutputStream(fileName));
}catch(Exception e){
textarea.append("文件打不开"+"\n") ; ;
textarea.append(fileName+"\n") ;
return ;
}
// 构造传输文件用的数据流
Socket dataSocket = dataConnection("RETR " + fileName) ;
BufferedInputStream dataInput
= new BufferedInputStream(dataSocket.getInputStream()) ;
// 从服务器上读取数据并存储到文件中
while((n = dataInput.read(buff,0,1024)) != 0){
outfile.write(buff,0,n) ;
}
//textarea.append(ctrlInput.readLine()) ;
dataSocket.close() ;
dataInput.close();
outfile.close() ;
}catch(Exception e)
{
//e.printStackTrace();
//System.exit(1);
textarea.append("文件不存在") ;
textarea.append(fileName) ;
return ;
}
}
/********************************************************************************/
// doPut方法
// 向服务器发送文件
public void doPut()
{
String fileName = "",filedir= "" ;
try
{
int n ;
byte[] buff = new byte[1024] ;
FileInputStream sendfile = null ;
// 指定文件名:
fileName = fileopendlg.getFile();
filedir = fileopendlg.getDirectory();
// 准备读出客户端的文件
try
{
sendfile = new FileInputStream(filedir+fileName) ;
}
catch(Exception e)
{
System.out.println("文件不存在") ;
System.out.println(filedir+fileName) ;
return ;
}
// 准备发送数据用的流
Socket dataSocket = dataConnection("STOR " + fileName) ;
OutputStream outstr = dataSocket.getOutputStream() ;
// 读出文件,经由网络发送给服务器
while((n = sendfile.read(buff)) > 0)
{
outstr.write(buff,0,n) ;
}
//textarea.append(ctrlInput.readLine()) ;
dataSocket.close() ;
sendfile.close() ;
}
catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
//删除服务器上的指定文件
public void doDele()
{
String dirName = "" ;
try{
dirName = list.getSelectedItem() ;
ctrlOutput.println("DELE " + dirName) ;// CWD命令
ctrlOutput.flush() ;
// textarea.append(ctrlInput.readLine()) ;
doLs();
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
////建立目录
public void doMkd()
{
String dirName = "" ;
try{
dirName = mkddlg.dirname ;
ctrlOutput.println("MKD " + dirName) ;// CWD命令
ctrlOutput.flush() ;
// textarea.append(ctrlInput.readLine()) ;
doLs();
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
// doBinary方法
// 设置二进制传输模式
public void doBinary()
{
try{
ctrlOutput.println("TYPE I") ;// I模式
ctrlOutput.flush() ;
// textarea.append(ctrlInput.readLine()) ;
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
//强行关闭当前的传送数据的Socket
public void doAbor()
{
try{
ctrlOutput.println("ABOR ") ;// ABOR命令
ctrlOutput.flush() ;
// textarea.append(ctrlInput.readLine()) ;
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
public void doQuit()
{
try{
ctrlOutput.println("QUIT ") ;// 发送 QUIT 命令
ctrlOutput.flush() ;
// textarea.append(ctrlInput.readLine()) ;
logdlg.textuser.setEditable(true);
logdlg.textpass.setEditable(true);
logdlg.textip.setEditable(true);
}catch(Exception e12)
{
e12.printStackTrace();
//System.exit(1);
}
}
/********************************************************************************/
// dataConnection方法
// 构造与服务器交换数据用的socket
// 再用 PORT 命令将端口号通知给服务器
public Socket dataConnection(String ctrlcmd)
{
String cmd = "PORT " ; //存贮发送PORT命令的数据 所用的变量
int i ;
Socket dataSocket = null ;
try{
// 取得自己用的地址
byte[] address = InetAddress.getLocalHost().getAddress() ;
// 用适当的端口号构造服务器 Socket
ServerSocket serverDataSocket = new ServerSocket(0,1) ;
// 准备传送port 命令用的数据
for(i = 0; i < 4; ++i)
cmd = cmd + (address[i] & 0xff) + "," ;
cmd = cmd + (((serverDataSocket.getLocalPort()) / 256) & 0xff)
+ ","
+ (serverDataSocket.getLocalPort() & 0xff) ;
// 利用控制用的流发送PORT命令
ctrlOutput.println(cmd) ;
ctrlOutput.flush() ;
// 向服务器发送处理对象命令(LIST,RETR及STOR)
ctrlOutput.println(ctrlcmd) ;
ctrlOutput.flush() ;
// 受理服务器的连接
dataSocket = serverDataSocket.accept() ;
serverDataSocket.close() ;
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
return dataSocket ;
}
/********************************************************************************/
public static void main(String[] args)
{
FtpClientFrame clientframe = new FtpClientFrame();
clientframe.show();
}
}
/********************************************************************************/
/* LoginDlg *******************************************************/
/********************************************************************************/
class LoginDlg extends Dialog implements ActionListener
{
String username,password,ip;
JButton OK,cancel;
JTextField textuser,textpass,textip;
LoginDlg(Frame f,String s,boolean b)
{
super(f,s,b);
OK = new JButton("登陆");
//cancel = new JButton("反登陆");
textuser = new JTextField("帐号",10);
textpass = new JTextField("密码",10);
textip = new JTextField("FTP IP号",10);
setLayout(new FlowLayout());
add(textip);
add(textuser);
add(textpass);
add(OK);
OK.addActionListener(this);
//cancel.addActionListener(this);
setBounds(60,60,150,150);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1){
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
username = textuser.getText();
password = textpass.getText();
setVisible(false);
textuser.setText("帐号");
textpass.setText("密码");
}
}
/********************************************************************************/
/* HelpDlg ****************************************************/
/********************************************************************************/
class HelpDlg extends Dialog
{
JTextArea textarea;
HelpDlg(Frame f,String s,boolean b)
{
super(f,s,b);
JLabel jb1 = new JLabel("Ls:列出文件与目录..................");
JLabel jb2 = new JLabel("cd:打开一个目录......................");
JLabel jb3 = new JLabel("pwd:当前的目录.......................");
JLabel jb4 = new JLabel("cdup:返回上一层目录.................");
JLabel jb5 = new JLabel("mkd:新建一个文件夹...................");
JLabel jb6 = new JLabel("get:下载一个文件............... ....");
JLabel jb7 = new JLabel("put:上载一个文件....................");
JLabel jb8 = new JLabel("asc:转asc模式.......................");
JLabel jb9 = new JLabel("bin:转bin模式.......................");
JLabel jb10= new JLabel("abor:中止当前数据传输...............");
JLabel jb11= new JLabel("dele:删除一个文件或目录.............");
JLabel jb12= new JLabel("quit:退出FTP.......................");
JLabel jba= new JLabel(".....................................................");
JLabel jbb= new JLabel(" author: ZhiXiong Yang.................");
JLabel jbc= new JLabel("......IMU.CS.DET........................");
JLabel jbd= new JLabel(".....@2006/5/19.@......................");
setLayout(new FlowLayout());
add(jb1);
add(jb2);
add(jb3);
add(jb4);
add(jb5);
add(jb6);
add(jb7);
add(jb8);
add(jb9);
add(jb10);
add(jb11);
add(jb12);
add(jba);
add(jbb);
add(jbc);
add(jbd);
setBounds(60,60,200,400);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1){
setVisible(false);
}
});
}
}
/********************************************************************************/
/* MkdDlg ****************************************************/
/********************************************************************************/
class MkdDlg extends Dialog implements ActionListener
{
String dirname;
JButton OK;
JTextField textdir;
MkdDlg(Frame f,String s,boolean b)
{
super(f,s,b);
OK = new JButton("OK");
textdir = new JTextField("目录名或文件名",10);
setLayout(new FlowLayout());
add(textdir);
add(OK);
OK.addActionListener(this);
setBounds(60,60,100,100);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1){
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
dirname = textdir.getText();
setVisible(false);
}
}
/********************************************************************************/
/* CtrlListen **********************************************************/
/********************************************************************************/
class CtrlListen implements Runnable{
BufferedReader ctrlInput = null ;
// 构造器,指定读取地点
public CtrlListen(BufferedReader in){
ctrlInput = in ;
}
public void run(){
while(true){
try{ // 按行读入并写到标准输出上
//System.out.println(ctrlInput.readLine()) ;
FtpClientFrame.textarea.append(ctrlInput.readLine()+"\n");
} catch (Exception e){
System.exit(1) ;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -