JAVA写文件到FTP的几种方法

本文介绍了如何使用URL和FtpClient类通过FTP协议进行文件上传操作,包括URL构造、登录验证、目录切换及文件写入流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值