xinhu1300 发表于 2015-5-28 12:48:51

JAVA写文件到FTP的两种方法(转)

  http://www.blogjava.net/JaVaa/

  1.使用URL:

URL url=new URL("ftp://javaa:javaa@172.168.2.222:21/test/javaa.txt");
PrintWriter pw=new PrintWriter(url.openConnection().getOutputStream());
pw.write("this is a test");
pw.flush();
pw.close();  上面是代码的片断,其中URL构造函数的参数可以用不同的访问协议(比如http,ftp等),"//"后跟着的是用户名和密码,两者用":"隔开,紧跟着是分隔符"@","@"以后的是IP地址和端口,然后是目录,最后才是我们要写入的文件名,其中目录是必须存在的,否则会抛出FileNotFoundException,文件可以是不存在的,不存在的时候就会新建文件,否则就会用新的内容覆盖以前的内容;

  2.使用FtpClient:


FtpClient ftpClient=new FtpClient();
ftpClient.openServer("172.168.2.222",21);//IP地址和端口
ftpClient.login("javaa","javaa");//用户名和密码,匿名登陆的话用户名为anonymous,密码为非空字符串
ftpClient.cd("test");//切换到test目录
PrintWriter pw=new PrintWriter(ftpClient.put("javaa.txt"));//写入的文件名
pw.write("this is a test");
pw.flush();
pw.close();

3.用PASV模式传送数据的FtpClient
import sun.net.ftp.FtpClient;
import java.net.Socket;
import java.io.IOException;

public class PasvFtpClient
    extends FtpClient{

/**
   * FTP服务器的地址
   */
private String serverAddr;
/**
   * 连接到FTP服务器的Socket
   */
private Socket socket;
/**
   * 仿造父类定义的静态变量
   */
protected final static int FTP_ERROR=3;
/**
   * 仿造父类定义的静态变量
   */
protected final static int FTP_SUCCESS=1;

public PasvFtpClient(String s) throws IOException{
    super(s);
    serverAddr=s;
    socket=null;
}

public PasvFtpClient(String s,int i) throws IOException{
    super(s,i);
    serverAddr=s;
    socket=null;
}

public PasvFtpClient(){
    super();
    socket=null;
}

/**
   * 复写的主要部分,父类采用PORT模式,这里改为PASV模式
   * @param s 传入的FTP命令
   * @return 连接到FTP服务器的Socket
   * @throws IOException
   */
protected Socket openDataConnection(String s) throws IOException{
    if (socket==null){
      String s1="PASV";
      if (issueCommand(s1)==FTP_ERROR){
      MyFtpProtocolException ftpprotocolexception=new MyFtpProtocolException(
            "PASV");
      throw ftpprotocolexception;
      }
      String responseStr=this.getResponseString();
      int location=responseStr.lastIndexOf(",");
      int n=Integer.parseInt(responseStr.substring(location+1,
          responseStr.indexOf(")")));
      responseStr=responseStr.substring(0,location);
      location=responseStr.lastIndexOf(",");
      int m=Integer.parseInt(responseStr.substring(location+1,
          responseStr.length()));
      socket=new Socket(serverAddr,m*256+n);
    }
    if (issueCommand(s)==FTP_ERROR){
      MyFtpProtocolException ftpprotocolexception1=new MyFtpProtocolException(s);
      throw ftpprotocolexception1;
    }
    return socket;
}

/**
   * 关闭与FTP服务器的连接
   * @throws IOException
   */
public void closeServer() throws IOException{
    socket.close();
    socket=null;
    super.closeServer();
}

/**
   * 打开与FTP服务器的连接
   * @param s FTP服务器地址
   * @param i FTP服务器端口
   * @throws IOException
   */
public void openServer(String s,int i) throws IOException{
    super.openServer(s,i);
    serverAddr=s;
}
}

/**
* 自定义的FTP异常类
*/
class MyFtpProtocolException
    extends IOException{
MyFtpProtocolException(String s){
    super(s);
}
}
页: [1]
查看完整版本: JAVA写文件到FTP的两种方法(转)