@Override public void afterPropertiesSet() throws Exception { if (ftpClient == null) { ftpClient = new FTPClient(); } ftpClient.configure(config); ftpClient.addProtocolCommandListener( new ProtocolCommandListener() { @Override public void protocolCommandSent(ProtocolCommandEvent event) { log.info("Send FTP command: " + event.getCommand()); } @Override public void protocolReplyReceived(ProtocolCommandEvent event) { log.info("Received FTP command reply: " + event.getReplyCode()); } }); }
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())); } } } }
/** * Renames the file denoted by this abstract pathname. * * <p>Whether or not this method can move a file from one filesystem to another is * platform-dependent. The return value should always be checked to make sure that the rename * operation was successful. * * @param dest The new abstract pathname for the named file * @throws IllegalArgumentException If parameter <code>dest</code> is not a <code>GeneralFile * </code>. * @throws NullPointerException - If dest is null */ public boolean renameTo(GeneralFile dest) throws IllegalArgumentException, NullPointerException { try { if (dest instanceof FTPFile) { if (ftpClient.equals(((FTPFile) dest).ftpClient)) { ftpClient.rename(getPath(), dest.getPath()); } else { // TODO some ftp to ftp copy... if (!dest.exists()) { copyTo(dest); delete(); } else return false; } } else { if (!dest.exists()) { copyTo(dest); delete(); } else return false; } } catch (IOException e) { return false; } catch (FTPException e) { return false; } return true; }
private void initFTPFile() { try { String path = getURL().getPath(); // 如果且以"/"结尾,去掉"/" if (path.endsWith("/")) { path = path.substring(0, path.lastIndexOf('/')); } // 资源在服务器中所属目录 String checkPath = path.substring(0, path.lastIndexOf('/')); // 如果所属目录为根目录 if (checkPath.length() == 0) { checkPath = "/"; } String fileName = path.substring(path.lastIndexOf('/')); fileName = fileName.substring(fileName.lastIndexOf('/') + 1); ftpClient.enterLocalPassiveMode(); // 从上级目录的子目录中过滤出当前资源 FTPFile[] files = ftpClient.listFiles(recode(checkPath), new FtpFileFilterByName(fileName)); if (files != null && files.length == 1) { ftpFile = files[0]; } else { throw new TinySysRuntimeException("查找资源失败,url=" + getURL()); } } catch (Exception e) { throw new VFSRuntimeException(e); } }
@Override public void destroy() throws Exception { if (ftpClient.isConnected()) { ftpClient.logout(); try { ftpClient.disconnect(); } catch (IOException e) { log.warn("Failed to disconnect from FTP server", e); } log.info("Successfully closed connection to FTP server"); } }
/** * Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a * directory, then the directory must be empty in order to be deleted. */ public boolean delete() { try { if (isDirectory()) { ftpClient.deleteDir(getPath()); } else { ftpClient.deleteFile(getPath()); } } catch (IOException e) { return false; } catch (FTPException e) { return false; } return true; }
@Override public void send(Message message, TestContext context) { FtpMessage ftpMessage; if (message instanceof FtpMessage) { ftpMessage = (FtpMessage) message; } else { ftpMessage = new FtpMessage(message); } String correlationKeyName = getEndpointConfiguration().getCorrelator().getCorrelationKeyName(getName()); String correlationKey = getEndpointConfiguration().getCorrelator().getCorrelationKey(ftpMessage); correlationManager.saveCorrelationKey(correlationKeyName, correlationKey, context); log.info( String.format( "Sending FTP message to: ftp://'%s:%s'", getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort())); if (log.isDebugEnabled()) { log.debug("Message to be sent:\n" + ftpMessage.getPayload().toString()); } try { connectAndLogin(); int reply = ftpClient.sendCommand(ftpMessage.getCommand(), ftpMessage.getArguments()); if (!FTPReply.isPositiveCompletion(reply) && !FTPReply.isPositivePreliminary(reply)) { throw new CitrusRuntimeException( String.format( "Failed to send FTP command - reply is: %s:%s", reply, ftpClient.getReplyString())); } log.info( String.format( "FTP message was successfully sent to: '%s:%s'", getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort())); correlationManager.store( correlationKey, new FtpMessage(ftpMessage.getCommand(), ftpMessage.getArguments()) .replyCode(reply) .replyString(ftpClient.getReplyString())); } catch (IOException e) { throw new CitrusRuntimeException("Failed to execute ftp command", e); } }
public List<FileObject> getChildren() { if (!isFolder()) { return null; } if (getChildren() == null) { try { String pathname = getAbsolutePath(); FTPFile[] files = ftpClient.listFiles(recode(pathname)); List<FileObject> fileObjects = new ArrayList<FileObject>(); for (FTPFile file : files) { FtpFileObject fileObject = new FtpFileObject(getSchemaProvider()); fileObject.setParent(this); fileObject.ftpFile = file; fileObject.ftpClient = this.ftpClient; fileObjects.add(fileObject); } return fileObjects; } catch (IOException e) { throw new VFSRuntimeException(e); } } return getChildren(); }
@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); }
/** * Returns the length of the file denoted by this abstract pathname. * * @return The length, in bytes, of the file denoted by this abstract pathname, or <code>0L</code> * if the file does not exist */ public long length() { try { return ftpClient.size(getPath()); } catch (IOException e) { return 0; } catch (FTPException e) { return 0; } }
/** Creates the directory named by this abstract pathname. */ public boolean mkdir() { try { ftpClient.makeDir(getPath()); return true; } catch (IOException e) { return false; } catch (FTPException e) { return false; } }
/** * Returns the time that the file denoted by this abstract pathname was last modified. * * @return A <code>long</code> value representing the time the file was last modified, measured in * system-dependent way. */ public long lastModified() { try { Date date = ftpClient.lastModified(getPath()); return date.getTime(); } catch (IOException e) { return 0; } catch (FTPException e) { return 0; } }
/** * Tests whether the file denoted by this abstract pathname exists. * * @return <code>true</code> if and only if the file denoted by this abstract pathname exists; * <code>false</code> otherwise */ public boolean exists() { try { ftpClient.exists(getPath()); } catch (IOException e) { return false; } catch (FTPException e) { return false; } return true; }
@Override public void put(File localFile, String remoteFileName, boolean append) throws IOException, ServerException, ClientException { if (gSession.transferMode == GridFTPSession.MODE_EBLOCK) { DataSource source = new FileRandomIO(new RandomAccessFile(localFile, "r")); put(remoteFileName, source, null, append); } else { super.put(localFile, remoteFileName, append); } }
@Override public void get(String remoteFileName, File localFile) throws IOException, ClientException, ServerException { if (gSession.transferMode == GridFTPSession.MODE_EBLOCK) { DataSink sink = new FileRandomIO(new RandomAccessFile(localFile, "rw")); get(remoteFileName, sink, null); } else { super.get(remoteFileName, localFile); } }
public void close() throws Exception { if (isClosed) { System.out.println("SSH source already closed!"); return; } System.out.println("Closing SSH log source.."); isClosed = true; ftpReadThread.interrupt(); if (reader != null) try { reader.close(); } catch (IOException e) { e.printStackTrace(); } if (client != null && client.isConnected()) { System.out.println("try to disconnect SSH channel"); client.disconnect(true); System.out.println("FTP disconnected"); } buffer.clear(); }
/** * Copies this file to another file. This object is the source file. The destination file is given * as the argument. If the destination file, does not exist a new one will be created. Otherwise * the source file will be appended to the destination file. Directories will be copied * recursively. * * @param file The file to receive the data. * @throws NullPointerException If file is null. * @throws IOException If an IOException occurs. */ public void copyTo(GeneralFile file, boolean forceOverwrite) throws IOException { if (file == null) { throw new NullPointerException(); } if (isDirectory()) { // recursive copy GeneralFile[] fileList = listFiles(); file.mkdir(); if (fileList != null) { for (int i = 0; i < fileList.length; i++) { fileList[i].copyTo( FileFactory.newFile( file.getFileSystem(), file.getAbsolutePath(), fileList[i].getName()), forceOverwrite); } } } else { if (file.isDirectory()) { // change the destination from a directory to a file file = FileFactory.newFile(file, getName()); } try { if (file instanceof LocalFile) { ftpClient.get(getPath(), ((LocalFile) file).getFile()); } else if (file instanceof FTPFile) { ftpClient.transfer( getPath(), ((FTPFile) file).getFTPClient(), file.getPath(), !forceOverwrite, null); } else { super.copyTo(file); } } catch (FTPException e) { IOException io = new IOException(); io.initCause(e); throw io; } } }
/** * Because there are so many connect() methods, the _connectAction_() method is provided as a * means of performing some action immediately after establishing a connection, rather than * reimplementing all of the connect() methods. * * @throws IOException If it throw by _connectAction_. * @see org.apache.commons.net.SocketClient#_connectAction_() */ @Override protected void _connectAction_() throws IOException { // Implicit mode. if (isImplicit) { sslNegotiation(); } super._connectAction_(); // Explicit mode. if (!isImplicit) { execAUTH(); sslNegotiation(); } }
/** * Copies this file to another file. This object is the source file. The destination file is given * as the argument. If the destination file, does not exist a new one will be created. Otherwise * the source file will be appended to the destination file. Directories will be copied * recursively. * * @param file The file to receive the data. * @throws NullPointerException If file is null. * @throws IOException If an IOException occurs. */ public void copyFrom(GeneralFile file, boolean forceOverwrite) throws IOException { if (file == null) { throw new NullPointerException(); } if (file.isDirectory()) { // recursive copy GeneralFile[] fileList = file.listFiles(); mkdir(); if (fileList != null) { for (int i = 0; i < fileList.length; i++) { FileFactory.newFile(this, fileList[i].getName()).copyFrom(fileList[i], forceOverwrite); } } } else { if (isDirectory()) { // change the destination from a directory to a file GeneralFile subFile = FileFactory.newFile(this, file.getName()); subFile.copyFrom(file); return; } try { if (file instanceof LocalFile) { ftpClient.put(((LocalFile) file).getFile(), getPath(), !forceOverwrite); } else if (file instanceof FTPFile) { ftpClient.transfer(file.getPath(), ftpClient, getPath(), !forceOverwrite, null); } else { super.copyTo(file); } } catch (FTPException e) { IOException io = new IOException(); io.initCause(e); throw io; } } }
/** * Returns an array of strings naming the files and directories in the directory denoted by this * abstract pathname. * * <p>There is no guarantee that the name strings in the resulting array will appear in any * specific order; they are not, in particular, guaranteed to appear in alphabetical order. * * <p>If this GeneralFile object denotes a file, the results are unspecified. * * @return An array of strings naming the files and directories in the directory denoted by this * abstract pathname. */ public String[] list() { try { // TODO which list? Vector list = ftpClient.list(getPath()); Object[] listO = list.toArray(); String[] listS = new String[listO.length]; for (int i = 0; i < listO.length; i++) { listS[i] = listO[i].toString(); } return listS; } catch (IOException e) { return null; } catch (FTPException e) { return null; } }
public OutputStream getOutputStream() { if (isFolder()) { return null; } OutputStream os = null; try { String remote = getAbsolutePath(); remote = recode(remote); os = ftpClient.storeFileStream(remote); } catch (IOException e) { throw new VFSRuntimeException(e); } return os; }
public InputStream getInputStream() { if (isFolder()) { return null; } InputStream is = null; try { String remote = getAbsolutePath(); remote = recode(remote); is = ftpClient.retrieveFileStream(remote); } catch (IOException e) { throw new VFSRuntimeException("获取输入流异常", e); } return is; }
private void init() { LOGGER.debug("init"); ftpClient = new FTPClient(); ftpClient.setControlEncoding("UTF-8"); }
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"); }
private void checkConnection() { if (!ftpClient.isConnected()) connect(); }
/** * Closes the connection to the FTP server and restores connection parameters to the default * values. * * <p>Calls {@code setSocketFactory(null)} and {@code setServerSocketFactory(null)} to reset the * factories that may have been changed during the session, e.g. by {@link #execPROT(String)} * * @exception IOException If an error occurs while disconnecting. * @since 3.0 */ @Override public void disconnect() throws IOException { super.disconnect(); setSocketFactory(null); setServerSocketFactory(null); }