FtpUtil分析和总结分析和总结.docxVIP

  • 0
  • 0
  • 约7.46千字
  • 约 9页
  • 2023-08-06 发布于湖北
  • 举报
package com.heary.demo01; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import .TelnetInputStream; import .TelnetOutputStream; import .ftp.FtpClient; /** ftp 上传,下载 @author heary 2011-07-30 * */ public class FtpUtil { private String ip = ; private String username = ; private String password = ; private int port = -1; private String path = ; FtpClient ftpClient = null; OutputStream os = null; FileInputStream is = null; public FtpUtil(String serverIP, String username, String password) { this.ip = serverIP; this.username = username; this.password = password; } public FtpUtil(String serverIP, int port, String username, String password) { this.ip = serverIP; this.username = username; this.password = password; this.port = port; } /** 连接 ftp 服务器 * @throws IOException */ public boolean connectServer(){ ftpClient = new FtpClient(); try { if(this.port != -1){ ftpClient.openServer(this.ip,this.port); }else{ ftpClient.openServer(this.ip); } ftpClient.login(this.username, this.password); if (this.path.length() != 0){ ftpClient.cd(this.path);// path 是 ftp 服务下主目录的子目录 } ftpClient.binary();// 用 2 进制上传、下载System.out.println(已登录到\ + ftpClient.pwd() + \目录); return true; }catch (IOException e){ e.printStackTrace(); return false; } } /** 断开与 ftp 服务器连接 * @throws IOException */ public boolean closeServer(){ try{ if (is != null) { is.close(); } if (os != null) { os.close(); } if (ftpClient != null) { ftpClient.closeServer(); } System.out.println(已从服务器断开); return true; }catch(IOException e){ e.printStackTrace(); return false; } } /** 检查文件夹在当前目录下是否存在 @param dir @return */ private boolean isDirExist(String dir){ String pwd = ; try { pwd = ftpClient.pwd(); ftpClient.cd(dir); ftpClient.cd(pwd); }catch(Exception e){ return false; } return true; } /** 在当前目录下创建文件夹 @param dir @return @throws

文档评论(0)

1亿VIP精品文档

相关文档