(二)、edtFTPj FileTransferClient

edtFTPj的FileTransferClient类简单易用,而且下载的组件包中文档丰富,参考使用,完全能满足自己需要。

下载地址为:http://www.enterprisedt.com/index.html

废话不多说,上代码:

Java代码 复制代码
  1. publicclassEdtFtpFreeTest{
  2. publicstaticvoidmain(String[]args){
  3. Stringftp_url="127.0.0.1";
  4. Stringftp_username="username";
  5. Stringftp_password="password";
  6. Stringftp_path="remotepath";
  7. //本地文件路径
  8. Stringfile_path="localpathname";
  9. //上传服务器上文件名
  10. StringremoteFile="remotefilename";
  11. FileTransferClientftp=newFileTransferClient();
  12. try{
  13. ftp.setRemoteHost(ftp_url);
  14. ftp.setUserName(ftp_username);
  15. ftp.setPassword(ftp_password);
  16. ftp.connect();
  17. ftp.setContentType(FTPTransferType.BINARY);
  18. ftp.changeDirectory(ftp_path);
  19. //直接上传
  20. ftp.uploadFile(file_path,remoteFile,WriteMode.RESUME);
  21. //构造文件流上传
  22. OutputStreamos=ftp.uploadStream(remoteFile);
  23. FileInputStreamis=newFileInputStream(file_path);
  24. byte[]bytes=newbyte[1024];
  25. intc;
  26. while((c=is.read(bytes))!=-1){
  27. os.write(bytes,0,c);
  28. }
  29. is.close();
  30. os.close();
  31. is=null;
  32. os=null;
  33. ftp.disconnect();
  34. }catch(FTPExceptione){
  35. e.printStackTrace();
  36. }catch(IOExceptione){
  37. e.printStackTrace();
  38. }
  39. }
  40. }
public class EdtFtpFreeTest { public static void main(String[] args) { String ftp_url = "127.0.0.1"; String ftp_username = "username"; String ftp_password = "password"; String ftp_path = "remote path"; // 本地文件路径 String file_path = "local path name"; //上传服务器上文件名 String remoteFile = "remote file name"; FileTransferClient ftp = new FileTransferClient(); try { ftp.setRemoteHost(ftp_url); ftp.setUserName(ftp_username); ftp.setPassword(ftp_password); ftp.connect(); ftp.setContentType(FTPTransferType.BINARY); ftp.changeDirectory(ftp_path); //直接上传 ftp.uploadFile(file_path, remoteFile, WriteMode.RESUME); //构造文件流上传 OutputStream os = ftp.uploadStream(remoteFile); FileInputStream is = new FileInputStream(file_path); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } is.close(); os.close(); is = null; os = null; ftp.disconnect(); } catch (FTPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值