Ejemplo n.º 1
1
 private void connectFtpServer() {
   try {
     ftpClient = new FTPClient();
     FTPClientConfig ftpClientConfig = new FTPClientConfig();
     ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
     ftpClient.configure(ftpClientConfig);
     URL url = getURL();
     if (url.getPort() <= 0) {
       ftpClient.connect(url.getHost());
     } else {
       ftpClient.connect(url.getHost(), url.getPort());
     }
     if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
       throw new VFSRuntimeException("连接失败!");
     }
     if (url.getUserInfo() != null) {
       String userInfo[] = url.getUserInfo().split(":");
       String userName = null;
       String password = null;
       if (userInfo.length >= 1) {
         userName = userInfo[0];
       }
       if (userInfo.length >= 2) {
         password = userInfo[1];
       }
       if (!ftpClient.login(userName, password)) {
         throw new VFSRuntimeException("登录失败:" + url.toString());
       }
       if (!ftpClient.setFileType(FTP.BINARY_FILE_TYPE)) {
         throw new VFSRuntimeException("设置二进制类型失败");
       }
       ftpClient.setBufferSize(BUF_SIZE);
       ftpClient.setControlEncoding("utf-8");
     }
   } catch (Exception e) {
     throw new VFSRuntimeException(e);
   }
 }
Ejemplo n.º 2
0
  public static FTPClient getFTPClient() {
    FTPClient client = ftpClientThreadLocal.get();
    if (client != null && client.isConnected()) {
      return client;
    }
    ftpClientThreadLocal.remove();
    FTPClient ftpClient = new FTPClient(); // 创建ftpClient
    ftpClient.setControlEncoding("UTF-8"); // 设置字符编码
    Boolean isConnect = connectFtp(ftpClient);

    ftpClient.enterLocalPassiveMode();
    try {
      ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
      ftpClient.setSoTimeout(1000 * 30);
    } catch (Exception e) {
      e.printStackTrace();
    }
    // 得到返回答复码
    int reply = ftpClient.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
      try {
        ftpClient.disconnect();
      } catch (IOException e) {
        e.printStackTrace();
      }

    } else {
      ftpClientThreadLocal.set(ftpClient);
    }
    return ftpClient;
  }
Ejemplo n.º 3
0
  /**
   * 서버와 연결에 필요한 값들을 가져와 초기화 시킴
   *
   * @param host 서버 주소
   * @param userName 접속에 사용될 아이디
   * @param password 비밀번호
   * @param port 포트번호
   */
  public void init(String host, String userName, String password, int port) {
    client = new FTPClient();
    client.setControlEncoding("UTF-8"); // 한글 encoding....

    FTPClientConfig config = new FTPClientConfig();
    client.configure(config);
    try {
      client.connect(host, port);
      client.login(userName, password);
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  public boolean makeFtpFile(String host, int port, String username, String pwd, String filepath) {
    FTPClient ftpClient = new FTPClient();
    boolean flag = false;
    try {
      ftpClient.connect(host, port);
    } catch (SocketException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    ftpClient.setControlEncoding("utf-8");
    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);

    conf.setServerLanguageCode("zh");
    System.out.println("返回码:" + ftpClient.getReplyCode());
    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
      try {
        if (ftpClient.login(username, pwd)) {
          try {
            String str = new String(filepath);
            flag = ftpClient.makeDirectory(new String(str.getBytes("GBK"), "iso-8859-1"));
            System.out.println("ftp创建文件夹:" + flag);
            ftpClient.changeWorkingDirectory(str);
          } catch (IOException e) {
            System.out.println("连接FTP出错:" + e.getMessage());
          }
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    try {
      ftpClient.disconnect();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return flag;
  }
Ejemplo n.º 5
0
 private void init() {
   LOGGER.debug("init");
   ftpClient = new FTPClient();
   ftpClient.setControlEncoding("UTF-8");
 }