📄 ftpclientfram.java
字号:
package ftpclient;
/**
* @author 作者 E-mail:
* @version 创建时间:2008-1-18 上午12:46:21
* 类说明
*/
/* Created on 2007年6月5日, 下午12:27
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.io.BufferedInputStream;
/**
*
* @author Administrator
*/
public class FTPClientFram extends JFrame{
//编写界面所需的组建
JPanel contentPane;
Label labelPrompt = new Label(); //状态提示
Label labelHost = new Label();
TextField textFieldHost = new TextField(); //主机地址
Label labelUser = new Label();
TextField textFieldUser = new TextField(); //用户名
Label labelPassword = new Label();
TextField textFieldPassword = new TextField(); //密码
Button buttonLink = new Button(); //连接按钮
Button buttonDisconnect = new Button(); //断开按钮
Label labelFileShow = new Label();
TextArea textAreaContent = new TextArea(); //显示文件和目录的文本域
Label labelFile = new Label();
TextField textFieldFile = new TextField(); //要下载或上传的文件名输入框
Label labelDir = new Label();
TextField textFieldDir = new TextField(); //保存或上传文件的本机目录名(未实现)
Button buttonDownload = new Button(); //下载按钮
Button buttonPut = new Button(); //上传按钮
Label labelFWQDir = new Label();
TextField textFieldFWQDir = new TextField();//进入服务器的目录输入框
Button buttonCd = new Button();
Socket ctrlSocket;//控制用Socket
public PrintWriter ctrlOutput;//控制输出用的流
public BufferedReader ctrlInput;// 控制输(读)入用的流
final int CTRLPORT = 21 ;// ftp 的控制用端口
// openConnection方法
//由地址和端口号构造Socket,形成控制用的流
public void openConnection(String host)
throws IOException,UnknownHostException {
ctrlSocket = new Socket(host, CTRLPORT);
ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream());
ctrlInput= new BufferedReader(new InputStreamReader(ctrlSocket.getInputStream()));
}
// closeConnection方法
//关闭控制用的Socket
public void closeConnection()
throws IOException {
ctrlSocket.close() ;
}
// getMsgs方法
// 启动从控制流收信的线程
public void getMsgs(){
try {
CtrlListen listener = new CtrlListen(ctrlInput) ;
Thread listenerthread = new Thread(listener) ;
listenerthread.start() ;
}catch(Exception e){
e.printStackTrace() ;
System.exit(1) ;
}
}
// doLogin方法
// 登录到ftp服务器
public void Login() {
String loginName = "" ;
String password = "" ;
try{
//得到用户名及密码,若为空则设为匿名登陆
if(textFieldUser.getText().equals("") && textFieldPassword.getText().equals("")){
loginName = "anonymous";
password = "anonymous";
textFieldUser.setText("anonymous");
textFieldPassword.setText("anonymous");
}
else{
loginName = textFieldUser.getText();
password = textFieldPassword.getText();
}
// 利用USER命令登录
ctrlOutput.println("USER " + loginName) ;
ctrlOutput.flush() ;
// 利用PASS命令输入口令
ctrlOutput.println("PASS " + password) ;
ctrlOutput.flush() ;
}catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
// doQuit方法
// 从ftp服务器注销
public void doQuit() {
try{
ctrlOutput.println("Quit ") ;// 发送Quit命令
ctrlOutput.flush() ;
}catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
// dataConnection方法
// 构造与服务器交换数据用的Socket
// 再用PORT命令将端口通知服务器
public Socket dataConnection(String ctrlcmd) {
String cmd = "PORT " ; //PORT存放用PORT命令传递数据的变量
int i ;
Socket dataSocket = null ;// 传送数据用Socket
try{
// 得到自己的地址
byte[] address = InetAddress.getLocalHost().getAddress() ;
// 用适当的端口号构造服务器
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 void ListFile() {
try{
int n ;
int ch;
StringBuffer buf = new StringBuffer();
// 建立数据连接
Socket dataSocket = dataConnection("LIST") ;
// 准备读取数据用的流
BufferedInputStream dataInput= new BufferedInputStream(dataSocket.getInputStream()) ;
// 读取目录信息
while((ch = dataInput.read()) >= 0){
buf.append((char)ch);
}
textAreaContent.append(buf.toString());
dataSocket.close() ;
}catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/** Creates a new instance of java_ftp */
public FTPClientFram(){
try{
Init();
}
catch(Exception e){
e.printStackTrace();
}
}
//Init()方法
//初始化程序界面
private void Init() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
labelPrompt.setBounds(new Rectangle(25,5,180,22));
labelHost.setText("主机名:");
labelHost.setBounds(new Rectangle(25,35,50,22));
textFieldHost.setBounds(new Rectangle(78,35,280,22));
labelUser.setText("用户名:");
labelUser.setBounds(new Rectangle(25,70, 50, 22));
textFieldUser.setBounds(new Rectangle(78,70,114,22));
labelPassword.setText("密码:");
labelPassword.setBounds(new Rectangle(205,70, 35,22));
textFieldPassword.setBounds(new Rectangle(244,70,114,22));
textFieldPassword.setEchoChar('*');
buttonLink.setLabel("连接");
buttonLink.setBounds(new Rectangle(375,35, 70, 22));
buttonLink.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
buttonLink_actionPerformed(e);
}
});
buttonLink.setEnabled(true);
buttonDisconnect.setLabel("断开");
buttonDisconnect.setBounds(new Rectangle(375,70, 70,22));
buttonDisconnect.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
buttonDisconnect_actionPerformed(e);
}
});
buttonDisconnect.setEnabled(false);
labelFileShow.setText("服务器目录列表");
labelFileShow.setBounds(new Rectangle(25,105, 140,22));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -