java把文件生成到ftp_JAVA写文件到FTP的两种方法(转) | 学步园

importsun.net.ftp.FtpClient;importjava.net.Socket;importjava.io.IOException;publicclassPasvFtpClientextendsFtpClient{/*** FTP服务器的地址*/privateString serverAddr;/*** 连接到FTP服务器的Socket*/privateSocket socket;/*** 仿造父类定义的静态变量*/protectedfinalstaticintFTP_ERROR=3;/*** 仿造父类定义的静态变量*/protectedfinalstaticintFTP_SUCCESS=1;publicPasvFtpClient(String s)throwsIOException{super(s);

serverAddr=s;

socket=null;

}publicPasvFtpClient(String s,inti)throwsIOException{super(s,i);

serverAddr=s;

socket=null;

}publicPasvFtpClient(){super();

socket=null;

}/*** 复写的主要部分,父类采用PORT模式,这里改为PASV模式

*@params 传入的FTP命令

*@return连接到FTP服务器的Socket

*@throwsIOException*/protectedSocket openDataConnection(String s)throwsIOException{if(socket==null){

String s1="PASV";if(issueCommand(s1)==FTP_ERROR){

MyFtpProtocolException ftpprotocolexception=newMyFtpProtocolException("PASV");throwftpprotocolexception;

}

String responseStr=this.getResponseString();intlocation=responseStr.lastIndexOf(",");intn=Integer.parseInt(responseStr.substring(location+1,

responseStr.indexOf(")")));

responseStr=responseStr.substring(0,location);

location=responseStr.lastIndexOf(",");intm=Integer.parseInt(responseStr.substring(location+1,

responseStr.length()));

socket=newSocket(serverAddr,m*256+n);

}if(issueCommand(s)==FTP_ERROR){

MyFtpProtocolException ftpprotocolexception1=newMyFtpProtocolException(s);throwftpprotocolexception1;

}returnsocket;

}/*** 关闭与FTP服务器的连接

*@throwsIOException*/publicvoidcloseServer()throwsIOException{

socket.close();

socket=null;super.closeServer();

}/*** 打开与FTP服务器的连接

*@params FTP服务器地址

*@parami FTP服务器端口

*@throwsIOException*/publicvoidopenServer(String s,inti)throwsIOException{super.openServer(s,i);

serverAddr=s;

}

}/*** 自定义的FTP异常类*/classMyFtpProtocolExceptionextendsIOException{

MyFtpProtocolException(String s){super(s);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值