?? ftp.java
字號:
import java.io.BufferedInputStream;
/*
* Ftp.java
*
* Created on 2007年4月21日, 上午11:15
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author user
*/
// 導入庫
import java.net.*;
import java.io.*;
public class Ftp {
/** Creates a new instance of Ftp */
// 準備
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() ;
}
// showMenu方法
// 輸出ftp的命令菜單
public void showMenu()
{
System.out.println(">Command?") ;
System.out.print("2 ls") ;
System.out.print(" 3 cd") ;
System.out.print(" 4 get") ;
System.out.print(" 5 put") ;
System.out.print(" 6 ascii") ;
System.out.print(" 7 binary") ;
System.out.println(" 9 quit") ;
}
// getCommand方法
// 讀取用戶指定的命令序號
public String getCommand()
{
String buf = "" ;
BufferedReader lineread= new BufferedReader(new InputStreamReader(System.in)) ;
while(buf.length() != 1){// 循環接收一個字符的輸入
try{
buf = lineread.readLine() ;
}catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
return (buf) ;
}
// doLogin方法
// 登錄到ftp服務器
public void doLogin()
{
String loginName = "" ;
String password = "" ;
BufferedReader lineread= new BufferedReader(new InputStreamReader(System.in)) ;
try{
System.out.println("input username") ;
loginName = lineread.readLine() ;
// 利用USER命令登錄
ctrlOutput.println("USER " + loginName) ;
ctrlOutput.flush() ;
// 利用PASS命令輸入口令
System.out.println("input passwd") ;
password = lineread.readLine() ;
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);
}
}
// doCd方法
// 切換目錄
public void doCd()
{
String dirName = "" ;
BufferedReader lineread = new BufferedReader(new InputStreamReader(System.in)) ;
try{
System.out.println("input content name:") ;
dirName = lineread.readLine() ;
ctrlOutput.println("CWD " + dirName) ;// CWD命令
ctrlOutput.flush() ;
}catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// doLs方法
// 取得目錄信息
public void doLs()
{
try{
int n ;
byte[] buff = new byte[1024] ;
// 建立數據連接
Socket dataSocket = dataConnection("LIST") ;
// 準備讀取數據用的流
BufferedInputStream dataInput= new BufferedInputStream(dataSocket.getInputStream()) ;
// 讀取目錄信息
while((n = dataInput.read(buff)) > 0){
System.out.write(buff,0,n) ;
}
dataSocket.close() ;
}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 ;
}
// doAscii方法
// 設置文本傳輸模式
public void doAscii()
{
try{
ctrlOutput.println("TYPE A") ;// A模式
ctrlOutput.flush() ;
}catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// doBinary方法
// 設置二進制傳輸模式
public void doBinary()
{
try{
ctrlOutput.println("TYPE I") ;// I模式
ctrlOutput.flush() ;
}catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// doGet方法
// 取得服務器上的文件
public void doGet()
{
String fileName = "" ;
BufferedReader lineread= new BufferedReader(new InputStreamReader(System.in)) ;
try{
int n ;
byte[] buff = new byte[1024] ;
// 指定服務器上的文件名
System.out.println("input file name of server") ;
fileName = lineread.readLine() ;
// 在客戶端上準備接收用的文件
FileOutputStream outfile = new FileOutputStream(fileName) ;
// 構造傳輸文件用的數據流
Socket dataSocket = dataConnection("RETR " + fileName) ;
BufferedInputStream dataInput= new BufferedInputStream(dataSocket.getInputStream()) ;
// 接收來自服務器的數據,寫入本地文件
while((n = dataInput.read(buff)) > 0){
outfile.write(buff,0,n) ;
}
dataSocket.close() ;
outfile.close() ;
}catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// doPut方法
// 向服務器發送文件
public void doPut()
{
String fileName = "" ;
BufferedReader lineread
= new BufferedReader(new InputStreamReader(System.in)) ;
try{
int n ;
byte[] buff = new byte[1024] ;
FileInputStream sendfile = null ;
// 指定文件名
System.out.println("input file name") ;
fileName = lineread.readLine() ;
// 準備讀出客戶端上的文件
try{
sendfile = new FileInputStream(fileName) ;
}catch(Exception e){
System.out.println("file not existed") ;
return ;
}
// 準備發送數據的流
Socket dataSocket = dataConnection("STOR " + fileName) ;
OutputStream outstr = dataSocket.getOutputStream() ;
// 讀出文件,并利用網絡發送給服務器
while((n = sendfile.read(buff)) > 0){
outstr.write(buff,0,n) ;
}
dataSocket.close() ;
sendfile.close() ;
}catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
// execCommand方法
// 執行與各命令相應的處理
public boolean execCommand(String command)
{
boolean cont = true ;
switch(Integer.parseInt(command)){
case 2 : // 顯示服務器目錄信息
doLs() ;
break ;
case 3 : // 切換服務器的工作目錄
doCd() ;
break ;
case 4 : // 取得服務器上的文件
doGet() ;
break ;
case 5 : // 向服務器發送文件
doPut() ;
break ;
case 6 : // 文件傳輸模式
doAscii() ;
break ;
case 7 : // 二進制傳輸模式
doBinary() ;
break ;
case 9 : // 處理結束
doQuit() ;
cont = false ;
break ;
default : //其他輸入
System.out.println("please choose") ;
}
return(cont) ;
}
// main_proc方法
// 輸出ftp的命令菜單,調用各種處理方法
public void main_proc() throws IOException
{
boolean cont = true ;
try {
// 進行登錄
doLogin() ;
while(cont){
// 輸出菜單
showMenu() ;
// 接收并執行命令
cont = execCommand(getCommand()) ;
}
}
catch(Exception e){
System.err.print(e);
System.exit(1);
}
}
// 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) ;
}
}
// main方法
// 建立TCP連接,開始處理
public static void main(String[] arg){
try {
Ftp f = null;
if(arg.length < 1){
System.out.println("usage: java Ftp <host name>") ;
return ;
}
f = new Ftp();
f.openConnection(arg[0]); // 控制連接建立
f.getMsgs() ; // 啟動接收線程
f.main_proc(); // ftp 處理
f.closeConnection() ; // 關閉連接
System.exit(0) ; // 結束程序
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
// 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()) ;
} catch (Exception e){
System.exit(1) ;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -