Java 中两种ftp操作方式
最近看到几篇关于java中操作ftp的文章,所以想写一个总结贴,标记一下,方便以后应用。首先,我们可以应用java中的ftp库,java中提供了一个ftpclient的类,提供了很多操作ftp的方法。
先上代码,一看便知,很简单,应用时只要传递相应的server,user,password等就行了。
import java.io.IOException;import sun.net.*;import sun.net.ftp.*;public class FTP {public static void main(String[] args) {String ftpserver="server url"; String user="user"; String pwd="pwd"; String path="path";try {FtpClient ftpClient=new FtpClient();ftpClient.openServer(ftpserver);echo(ftpClient.getResponseString());ftpClient.login(user, pwd); echo(ftpClient.getResponseString());if (path.length() != 0) {ftpClient.cd(path);echo(ftpClient.getResponseString());}TelnetInputStream tis = ftpClient.list();echo(ftpClient.getResponseString());int c;while ((c = tis.read())!= -1) {System.out.print((char)c);}echo(ftpClient.getResponseString());tis.close();echo(ftpClient.getResponseString());ftpClient.closeServer();echo(ftpClient.getResponseString());}catch(IOException ex){ex.printStackTrace();}}static void echo(String response){System.out.println("Response-----> " + response); }}
在其内部都是发送socket请求的,只是帮我们封装好了。我们只要用相应的方法就行了。
在每个方面调用后面我都打印了从服务器返回的内容,这样可以方便我们ftp协议的理解。
读者可以拷贝到本地运行一下,看看ftp到底是如何运行的。
为了更清楚的理解ftp协议,下来使用原始的socket来操作ftp。
ftp服务器一般是在21端口监听的一个服务,我们只要向这个端口发送相应的请求就可以了。
请求我们必须参考ftp协议的规定,发送正确的格式。
下面这段代码是我收藏csdn上哪位博主的,基本上对ftp交互的这个过程中,协议的使用都写全了。
import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;public class FTPClient{public static void main(String[] args) throws Exception {Socket socket = new Socket("url",21);//get the is and os, how to work about them.InputStream is = socket.getInputStream();OutputStream os = socket.getOutputStream();//read info from the input stream, it is the response from server.byte[] buffer = new byte;int length = is.read(buffer);String s = new String(buffer, 0, length);System.out.println(s);//send the userString str = "USER user/n";os.write(str.getBytes());//get the response.length = is.read(buffer);s = new String(buffer, 0, length);System.out.println(s);//send the password.str = "PASS password/n";os.write(str.getBytes());//get the response.length = is.read(buffer);s = new String(buffer, 0, length);System.out.println(s);//send command.str = "CWD path/n";os.write(str.getBytes());//get the response.length = is.read(buffer);s = new String(buffer, 0, length);System.out.println(s);//set modestr = "EPSV ALL/n";os.write(str.getBytes());//get the response.length = is.read(buffer);s = new String(buffer, 0, length);System.out.println(s);//得到被动监听信息str = "EPSV/n";os.write(str.getBytes());//得到返回值length = is.read(buffer);s = new String(buffer, 0, length);System.out.println(s);//取得FTP被动监听的端口号System.out.println("debug----------------");String portlist=s.substring(s.indexOf("(|||")+4,s.indexOf("|)"));System.out.println(portlist);//实例化ShowList线程类,链接FTP被动监听端口号ShowList sl=new ShowList();sl.port=Integer.parseInt(portlist);sl.start();//执行LIST命令str = "LIST/n";os.write(str.getBytes());//得到返回值 length = is.read(buffer);s = new String(buffer, 0, length);System.out.println(s);//关闭链接is.close();os.close();socket.close();}}// 得到被动链接信息类,这个类是多线程的class ShowList extends Thread {public int port = 0;public void run() {try {Socket socket = new Socket("192.168.0.1", this.port);InputStream is = socket.getInputStream();OutputStream os = socket.getOutputStream();byte[] buffer = new byte;int length = is.read(buffer);String s = new String(buffer, 0, length);System.out.println(s);// 关闭链接is.close();os.close();socket.close();} catch (Exception ex) {}}}
如有兴趣的同学,可以好好看看ftp的协议过程,这样会更深理解。
版权声明:本文为博主原创文章,未经博主允许不得转载。
页:
[1]