// Ftp.java ftpプログラム // このプログラムはftpサーバーと接続してファイル転送をします。 // 使い方:java Ftp サーバーアドレス 例:java Ftp ftp4.broadband.co.jp import java.net.* ; import java.io.* ; public class Ftp { Socket ctrlSocket ; public PrintWriter ctrlOutput ; public BufferedReader ctrlInput ; final int CTRLPORT = 21 ; //----------------------------------------------------------------------------------------- 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())) ; } //----------------------------------------------------------------------------------------- public void closeConnection() throws IOException { ctrlSocket.close(); } //----------------------------------------------------------------------------------------- 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.print(" 9 quit") ; } //----------------------------------------------------------------------------------------- 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) ; } //----------------------------------------------------------------------------------------- public void doLogin() { String loginName = "" ; String password = "" ; BufferedReader Lineread = new BufferedReader(new InputStreamReader(System.in)) ; try { System.out.println("ログイン名を入力してください") ; loginName = Lineread.readLine() ; ctrlOutput.println("USER " + loginName) ; ctrlOutput.flush() ; System.out.println("パスワードを入力してください") ; password = Lineread.readLine() ; ctrlOutput.println("PASS " + password) ; ctrlOutput.flush() ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } } //----------------------------------------------------------------------------------------- public void doQuit() { try { ctrlOutput.println("QUIT ") ; ctrlOutput.flush() ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } } //----------------------------------------------------------------------------------------- public void doCd() { String dirName = "" ; BufferedReader Lineread = new BufferedReader(new InputStreamReader(System.in)) ; try { System.out.println("ディレクトリ名を入力してください") ;dirName = Lineread.readLine() ; ctrlOutput.println("CWD " + dirName) ; ctrlOutput.flush() ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } } //----------------------------------------------------------------------------------------- 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) ; } } //----------------------------------------------------------------------------------------- public Socket dataConnection(String ctrlcmd) { String cmd = "PORT " ; int i ; Socket dataSocket = null ; try { byte[] address = InetAddress.getLocalHost().getAddress() ; ServerSocket serverDataSocket = new ServerSocket(0, 1) ; for(i = 0 ; i < 4 ; ++i) { cmd = cmd + (address[i] & 0xff) + "," ; } cmd = cmd + (((serverDataSocket.getLocalPort()) /256) & 0xff) + "," + (serverDataSocket.getLocalPort() & 0xff) ; ctrlOutput.println(cmd) ; ctrlOutput.flush() ; ctrlOutput.println(ctrlcmd) ; ctrlOutput.flush() ; dataSocket = serverDataSocket.accept() ; serverDataSocket.close() ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } return dataSocket ; } //----------------------------------------------------------------------------------------- public void doAscii() { try { ctrlOutput.println("TYPE A") ; ctrlOutput.flush() ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } } //----------------------------------------------------------------------------------------- public void doBinary() { try { ctrlOutput.println("TYPE I") ; ctrlOutput.flush() ; } catch(Exception e) { e.printStackTrace(); System.exit(1) ; } } //----------------------------------------------------------------------------------------- public void doGet() { String fileName = "" ; BufferedReader Lineread = new BufferedReader(new InputStreamReader(System.in)) ; try { int n ; byte[] buff = new byte[1024] ; System.out.println("ファイル名を入力してください") ; 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) ; } } //----------------------------------------------------------------------------------------- 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("ファイル名を入力してください") ; fileName = Lineread.readLine() ; try { sendfile = new FileInputStream(fileName) ; } catch(Exception e) { System.out.println("ファイルがありません") ; return ; } Socket dataSocket = dataConnection("STRO " + 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) ; } } //----------------------------------------------------------------------------------------- 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("番号を選択して下さい") ; } return (cont) ; } //----------------------------------------------------------------------------------------- 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) ; } } //----------------------------------------------------------------------------------------- public void getMsgs() { try { CtrlListen listener = new CtrlListen(ctrlInput) ; Thread listenerthread = new Thread(listener) ; listenerthread.start() ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } } //----------------------------------------------------------------------------------------- public static void main(String[] args) { try { Ftp f = null ; if(args.length< 1) { System.out.println("usage: java Ftp ") ; return ; } f = new Ftp() ; f.openConnection(args[0]) ; f.getMsgs() ; f.main_proc() ; f.closeConnection() ; System.exit(0) ; } catch(Exception e) { e.printStackTrace() ; System.exit(1) ; } } } //----------------------------------------------------------------------------------------- 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) { e.printStackTrace() ; System.exit(1) ; } } } }