Java Code Examples for org.apache.commons.net.ftp.FTPClient#getReplyString()
The following examples show how to use
org.apache.commons.net.ftp.FTPClient#getReplyString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FTPTransfer.java From localization_nifi with Apache License 2.0 | 6 votes |
public void sendCommands(final List<String> commands, final FlowFile flowFile) throws IOException { if (commands.isEmpty()) { return; } final FTPClient client = getClient(flowFile); for (String cmd : commands) { if (!cmd.isEmpty()) { int result; result = client.sendCommand(cmd); logger.debug(this + " sent command to the FTP server: " + cmd + " for " + flowFile); if (FTPReply.isNegativePermanent(result) || FTPReply.isNegativeTransient(result)) { throw new IOException(this + " negative reply back from FTP server cmd: " + cmd + " reply:" + result + ": " + client.getReplyString() + " for " + flowFile); } } } }
Example 2
Source File: NetUtils.java From ApprovalTests.Java with Apache License 2.0 | 6 votes |
private static void assertValidReplyCode(int code, FTPClient ftp) { if (FTPReply.isPositiveCompletion(code)) { //good SimpleLogger.variable("Good Completion code " + code); } else if (FTPReply.isPositiveIntermediate(code)) { // do nothing SimpleLogger.variable("Good Intermediate code " + code); } else if (FTPReply.isPositivePreliminary(code)) { // do nothing SimpleLogger.variable("Good Preliminary code " + code); } else { // bad throw new Error("Problem encountered with FTP Server, returned Code " + code + ", replied '" + ftp.getReplyString() + "'"); } }
Example 3
Source File: FTPTransfer.java From nifi with Apache License 2.0 | 6 votes |
public void sendCommands(final List<String> commands, final FlowFile flowFile) throws IOException { if (commands.isEmpty()) { return; } final FTPClient client = getClient(flowFile); for (String cmd : commands) { if (!cmd.isEmpty()) { int result; result = client.sendCommand(cmd); logger.debug(this + " sent command to the FTP server: " + cmd + " for " + flowFile); if (FTPReply.isNegativePermanent(result) || FTPReply.isNegativeTransient(result)) { throw new IOException(this + " negative reply back from FTP server cmd: " + cmd + " reply:" + result + ": " + client.getReplyString() + " for " + flowFile); } } } }
Example 4
Source File: FTPUploader.java From azure-gradle-plugins with MIT License | 6 votes |
/** * Upload a single file to FTP server with the provided FTP client object. * * @param sourceFilePath * @param targetFilePath * @param logPrefix * @throws IOException */ private void uploadFile(final FTPClient ftpClient, final String sourceFilePath, final String targetFilePath, final String logPrefix) throws IOException { logger.quiet(String.format(UPLOAD_FILE, logPrefix, sourceFilePath, targetFilePath)); final File sourceFile = new File(sourceFilePath); try (final InputStream is = new FileInputStream(sourceFile)) { ftpClient.changeWorkingDirectory(targetFilePath); ftpClient.storeFile(sourceFile.getName(), is); final int replyCode = ftpClient.getReplyCode(); final String replyMessage = ftpClient.getReplyString(); if (isCommandFailed(replyCode)) { logger.error(String.format(UPLOAD_FILE_REPLY, logPrefix, replyMessage)); throw new IOException("Failed to upload file: " + sourceFilePath); } else { logger.quiet(String.format(UPLOAD_FILE_REPLY, logPrefix, replyMessage)); } } }
Example 5
Source File: FTPOperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 6 votes |
@Override public ReadableByteChannel read() throws IOException { FTPClient ftp = null; try { ftp = connect(uri); InputStream is = ftp.retrieveFileStream(getPath(uri)); if (is == null) { throw new IOException(ftp.getReplyString()); } int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) { is.close(); throw new IOException(ftp.getReplyString()); } return Channels.newChannel(new FTPInputStream(is, ftp)); } catch (Throwable t) { disconnect(ftp); if (t instanceof IOException) { throw (IOException) t; } else { throw new IOException(t); } } }
Example 6
Source File: MainframeFTPClientUtils.java From spark-mainframe-connector with Apache License 2.0 | 5 votes |
public static void setWorkingDirectory(TransferableContext context, FTPClient ftp, String datasetName) throws IOException { String modifiedDatasetName = MainframeUtils.getDatasetName(context, datasetName); boolean replyCode = ftp.changeWorkingDirectory(modifiedDatasetName); if (!replyCode) { throw new IOException("Unable to change working directory to " + modifiedDatasetName + " for ftp transfer with ftp client = " + ftp + ". " + ftp.getReplyString()); } }
Example 7
Source File: FTPTransfer.java From nifi with Apache License 2.0 | 5 votes |
@Override public void deleteDirectory(final FlowFile flowFile, final String remoteDirectoryName) throws IOException { final FTPClient client = getClient(flowFile); final boolean success = client.removeDirectory(remoteDirectoryName); if (!success) { throw new IOException("Failed to remove directory " + remoteDirectoryName + " due to " + client.getReplyString()); } }
Example 8
Source File: FTPTransfer.java From nifi with Apache License 2.0 | 5 votes |
@Override public void deleteFile(final FlowFile flowFile, final String path, final String remoteFileName) throws IOException { final FTPClient client = getClient(flowFile); if (path != null) { setWorkingDirectory(path); } if (!client.deleteFile(remoteFileName)) { throw new IOException("Failed to remove file " + remoteFileName + " due to " + client.getReplyString()); } }
Example 9
Source File: FTPTransfer.java From nifi with Apache License 2.0 | 5 votes |
@Override public void rename(final FlowFile flowFile, final String source, final String target) throws IOException { final FTPClient client = getClient(flowFile); final boolean renameSuccessful = client.rename(source, target); if (!renameSuccessful) { throw new IOException("Failed to rename temporary file " + source + " to " + target + " due to: " + client.getReplyString()); } }
Example 10
Source File: FTPTransfer.java From nifi with Apache License 2.0 | 5 votes |
@Override public FlowFile getRemoteFile(final String remoteFileName, final FlowFile origFlowFile, final ProcessSession session) throws ProcessException, IOException { final FTPClient client = getClient(origFlowFile); InputStream in = null; FlowFile resultFlowFile = null; try { in = client.retrieveFileStream(remoteFileName); if (in == null) { final String response = client.getReplyString(); // FTPClient doesn't throw exception if file not found. // Instead, response string will contain: "550 Can't open <absolute_path>: No such file or directory" if (response != null && response.trim().endsWith("No such file or directory")) { throw new FileNotFoundException(response); } throw new IOException(response); } final InputStream remoteIn = in; resultFlowFile = session.write(origFlowFile, new OutputStreamCallback() { @Override public void process(final OutputStream out) throws IOException { StreamUtils.copy(remoteIn, out); } }); client.completePendingCommand(); return resultFlowFile; } finally { if(in != null){ try{ in.close(); }catch(final IOException ioe){ //do nothing } } } }
Example 11
Source File: FTPOperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
@Override public WritableByteChannel append() throws IOException { FTPClient ftp = null; try { ftp = connect(uri); Info info = info(uri, ftp); if ((info != null) && info.isDirectory()) { throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.exists_not_file"), uri)); //$NON-NLS-1$ } OutputStream os = ftp.appendFileStream(getPath(uri)); if (os == null) { throw new IOException(ftp.getReplyString()); } int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) { os.close(); throw new IOException(ftp.getReplyString()); } return Channels.newChannel(new FTPOutputStream(os, ftp)); } catch (Throwable t) { disconnect(ftp); if (t instanceof IOException) { throw (IOException) t; } else { throw new IOException(t); } } }
Example 12
Source File: FTPOperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
@Override public WritableByteChannel write() throws IOException { FTPClient ftp = null; try { ftp = connect(uri); Info info = info(uri, ftp); if ((info != null) && info.isDirectory()) { throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.exists_not_file"), uri)); //$NON-NLS-1$ } OutputStream os = ftp.storeFileStream(getPath(uri)); if (os == null) { throw new IOException(ftp.getReplyString()); } int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) { os.close(); throw new IOException(ftp.getReplyString()); } return Channels.newChannel(new FTPOutputStream(os, ftp)); } catch (Throwable t) { disconnect(ftp); if (t instanceof IOException) { throw (IOException) t; } else { throw new IOException(t); } } }
Example 13
Source File: FTPTransfer.java From localization_nifi with Apache License 2.0 | 5 votes |
@Override public InputStream getInputStream(final String remoteFileName, final FlowFile flowFile) throws IOException { final FTPClient client = getClient(null); InputStream in = client.retrieveFileStream(remoteFileName); if (in == null) { throw new IOException(client.getReplyString()); } return in; }
Example 14
Source File: FTPTransfer.java From localization_nifi with Apache License 2.0 | 5 votes |
@Override public void deleteDirectory(final String remoteDirectoryName) throws IOException { final FTPClient client = getClient(null); final boolean success = client.removeDirectory(remoteDirectoryName); if (!success) { throw new IOException("Failed to remove directory " + remoteDirectoryName + " due to " + client.getReplyString()); } }
Example 15
Source File: FTPTransfer.java From localization_nifi with Apache License 2.0 | 5 votes |
@Override public void deleteFile(final String path, final String remoteFileName) throws IOException { final FTPClient client = getClient(null); if (path != null) { setWorkingDirectory(path); } if (!client.deleteFile(remoteFileName)) { throw new IOException("Failed to remove file " + remoteFileName + " due to " + client.getReplyString()); } }
Example 16
Source File: FtpFileSystem.java From xenon with Apache License 2.0 | 5 votes |
private void checkClientReply(FTPClient client, String message) throws XenonException { int replyCode = client.getReplyCode(); String replyString = client.getReplyString(); if (replyCode >= 100 && replyCode < 300) { return; } throw new XenonException(ADAPTOR_NAME, message, new IOException(replyString)); }
Example 17
Source File: FtpCheck.java From commons-vfs with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { if (args.length < 3) { throw new IllegalArgumentException("Usage: FtpCheck user pass host dir"); } final String user = args[0]; final String pass = args[1]; final String host = args[2]; String dir = null; if (args.length == 4) { dir = args[3]; } final FTPClient client = new FTPClient(); client.connect(host); final int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new IllegalArgumentException("cant connect: " + reply); } if (!client.login(user, pass)) { throw new IllegalArgumentException("login failed"); } client.enterLocalPassiveMode(); final OutputStream os = client.storeFileStream(dir + "/test.txt"); if (os == null) { throw new IllegalStateException(client.getReplyString()); } os.write("test".getBytes(Charset.defaultCharset())); os.close(); client.completePendingCommand(); if (dir != null && !client.changeWorkingDirectory(dir)) { throw new IllegalArgumentException("change dir to '" + dir + "' failed"); } System.err.println("System: " + client.getSystemType()); final FTPFile[] files = client.listFiles(); for (int i = 0; i < files.length; i++) { final FTPFile file = files[i]; if (file == null) { System.err.println("#" + i + ": " + null); } else { System.err.println("#" + i + ": " + file.getRawListing()); System.err.println("#" + i + ": " + file.toString()); System.err.println("\t name:" + file.getName() + " type:" + file.getType()); } } client.disconnect(); }
Example 18
Source File: FTPServerTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
/** * Test for Passive Mode -> FTPCommand.Pasv command with external address functionality. * see MNT-16433 */ public void testFTPConnectExternalAddressSet() throws Exception { logger.debug("Start testFTPConnectExternalAddressSet"); try { // use a highly improbable IP to tests Passive Mode -> FTPCommand.Pasv command // this is supposed to be the address of a proxy in front of Alfrsco FTP server String improbableIPAddress = "127.255.255.42"; ftpConfigSection.setFTPExternalAddress(improbableIPAddress); FTPClient ftp = connectClient(); try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN); assertTrue("admin login not successful", login); // activate passive mode boolean sucess = ftp.enterRemotePassiveMode(); assertTrue(sucess); assertTrue("Client should be in passive mode now", ftp.getDataConnectionMode() == FTPClient.PASSIVE_REMOTE_DATA_CONNECTION_MODE); reply = ftp.getReplyCode(); //see https://www.ietf.org/rfc/rfc959.txt assertTrue("reply code should be 227", reply == 227); String replyLine = ftp.getReplyString(); assertTrue(replyLine != null); String encodedImprobableIPAddress = improbableIPAddress.replaceAll("\\.", ","); assertTrue("Pasv command should contain the set external address encoded", replyLine.contains(encodedImprobableIPAddress)); // now attempt to list the files and check that the command does not succeed FTPFile[] files = ftp.listFiles(); assertNotNull(files); assertTrue("list command should not succeed", files.length == 0); assertTrue("The passive host should be the one set earlier.", improbableIPAddress.equals(ftp.getPassiveHost())); } finally { safeDisconnect(ftp); } } finally { //always revert back to default, or the other tests will fail ftpConfigSection.setFTPExternalAddress(null); } }
Example 19
Source File: FTPService.java From cs-actions with Apache License 2.0 | 4 votes |
private void checkReplyCode(String command, int reply, FTPClient ftp) throws FTPException { if (!FTPReply.isPositiveCompletion(reply)) { throw new FTPException(reply, ftp.getReplyString()); } }
Example 20
Source File: DownloadFileFromFtpToTable.java From MicroCommunity with Apache License 2.0 | 4 votes |
/** * 下载文件并且存至tfs文件系统 * * @param remoteFileNameTmp * @param * @return */ private Map downLoadFileToTable(String remoteFileNameTmp, FTPClientTemplate ftpClientTemplate, Map ftpItemConfigInfo) { Map resultInfo = new HashMap(); String tfsReturnFileName = null; long block = 10 * 1024;// 默认 if (ftpClientTemplate == null) { resultInfo.put("SAVE_FILE_FLAG", "E"); return resultInfo; } String localPathName = ftpItemConfigInfo.get("localPath").toString().endsWith("/") ? ftpItemConfigInfo.get("localPath").toString() + ftpItemConfigInfo.get("newFileName").toString() : ftpItemConfigInfo.get("localPath").toString() + "/" + ftpItemConfigInfo.get("newFileName").toString(); ftpItemConfigInfo.put("localfilename", localPathName);// 本地带路径的文件名回写,后面读文件时使用 try { File file = new File(localPathName); RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");// 建立随机访问 FTPClient ftpClient = new FTPClient(); ftpClient.connect(ftpClientTemplate.getHost(), ftpClientTemplate.getPort()); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { if (!ftpClient.login(ftpClientTemplate.getUsername(), ftpClientTemplate.getPassword())) { resultInfo.put("SAVE_FILE_FLAG", "E"); resultInfo.put("remark", "登录失败,用户名【" + ftpClientTemplate.getUsername() + "】密码【" + ftpClientTemplate.getPassword() + "】"); return resultInfo; } } ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 二进制 ftpClient.enterLocalPassiveMode(); // 被动模式 ftpClient.sendCommand("PASV"); ftpClient.sendCommand("SIZE " + remoteFileNameTmp + "\r\n"); String replystr = ftpClient.getReplyString(); String[] replystrL = replystr.split(" "); long filelen = 0; if (Integer.valueOf(replystrL[0]) == 213) { filelen = Long.valueOf(replystrL[1].trim()); } else { resultInfo.put("SAVE_FILE_FLAG", "E"); resultInfo.put("remark", "无法获取要下载的文件的大小!"); return resultInfo; } accessFile.setLength(filelen); accessFile.close(); ftpClient.disconnect(); int tnum = Integer.valueOf(ftpItemConfigInfo.get("TNUM").toString()); block = (filelen + tnum - 1) / tnum;// 每个线程下载的快大小 ThreadPoolExecutor cachedThreadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); List<Future<Map>> threadR = new ArrayList<Future<Map>>(); for (int i = 0; i < tnum; i++) { logger.debug("发起线程:" + i); // 保存线程日志 ftpItemConfigInfo.put("threadrunstate", "R"); ftpItemConfigInfo.put("remark", "开始下载文件"); ftpItemConfigInfo.put("data", "文件名:" + remoteFileNameTmp); long start = i * block; long end = (i + 1) * block - 1; ftpItemConfigInfo.put("begin", start); ftpItemConfigInfo.put("end", end); saveTaskLogDetail(ftpItemConfigInfo); Map para = new HashMap(); para.putAll(ftpItemConfigInfo); para.put("serverfilename", remoteFileNameTmp); para.put("filelength", filelen); para.put("tnum", i + 0); para.put("threadDownSize", block); para.put("transferflag", FTPClientTemplate.TransferType.download); FTPClientTemplate dumpThread = new FTPClientTemplate(para); Future<Map> runresult = cachedThreadPool.submit(dumpThread); threadR.add(runresult); } do { // 等待下载完成 Thread.sleep(1000); } while (cachedThreadPool.getCompletedTaskCount() < threadR.size()); saveDownFileData(ftpItemConfigInfo); // 下载已经完成,多线程保存数据至表中 } catch (Exception e) { // TODO Auto-generated catch block logger.error("保存文件失败:", e); resultInfo.put("SAVE_FILE_FLAG", "E"); resultInfo.put("remark", "保存文件失败:" + e); return resultInfo; } resultInfo.put("SAVE_FILE_FLAG", "S"); return resultInfo; }