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); } }
/** * Opens a new connection and performs login with user name and password if set. * * @throws IOException */ protected void connectAndLogin() throws IOException { if (!ftpClient.isConnected()) { ftpClient.connect(getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort()); log.info("Connected to FTP server: " + ftpClient.getReplyString()); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new CitrusRuntimeException("FTP server refused connection."); } log.info("Successfully opened connection to FTP server"); if (getEndpointConfiguration().getUser() != null) { log.info(String.format("Login as user: '******'", getEndpointConfiguration().getUser())); boolean login = ftpClient.login( getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword()); if (!login) { throw new CitrusRuntimeException( String.format( "Failed to login to FTP server using credentials: %s:%s", getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword())); } } } }
@Override public void connect() { LOGGER.info("Connecting to " + username() + "@" + host() + ":" + String.valueOf(port())); reconnect = true; try { ftpClient.connect(host(), port()); } catch (IOException e) { throw new VirtualFileException(e); } if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { disconnect(); return; } login(); setFileType(FTP.BINARY_FILE_TYPE); }
public void startRead() throws Exception { // checkClosed(); readedLines = 0; paused = false; buffer = new ArrayList<String>(); client = new FTPClient(); client.setType(FTPClient.TYPE_BINARY); client.connect(host.getHost(), host.getPort()); client.login(host.getUser(), host.getPassword()); ftpFileSize = client.fileSize(logFile.getPath()); isClosed = false; final PipedOutputStream pos = new PipedOutputStream(); reader = new BufferedReader(new InputStreamReader(new PipedInputStream(pos), host.defaultEncoding)); // FTP read thread ftpReadThread = new Thread( new Runnable() { @Override public void run() { System.out.println("4"); java.util.Date md = null; try { String nextLine; while (!isClosed) { System.out.println("Ftp read thread run"); if (!isPaused()) { md = client.modifiedDate(logFile.getPath()); System.out.println("Modification date :" + md + "; and size: " + ftpFileSize); if (client.fileSize(logFile.getPath()) != ftpFileSize) { client.download(logFile.getPath(), pos, ftpFileSize, null); ftpFileSize = client.fileSize(logFile.getPath()); if ((nextLine = reader.readLine()) != null && !isClosed) { // buffer.add(String.format("%6d: %s", // (buffer.size()+1), nextLine)); buffer.add(nextLine); } } } Thread.sleep(500); } } catch (Exception e) { try { close(); } catch (Exception e1) { e1.printStackTrace(); } e.printStackTrace(); } } }); // Connection monitoring thread new Thread( new Runnable() { public void run() { System.out.println("6"); while (client.isConnected() && !isClosed) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } if (!isClosed) { System.out.println("Connection failed!"); ftpReadThread.interrupt(); try { close(); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("Stopped FTP connection monitor thread."); } } }, "Conn monitor") .start(); System.out.println("1"); // readThread.start(); Thread.sleep(500); System.out.println("2"); ftpReadThread.start(); System.out.println("3"); }