Java Code Examples for org.apache.commons.net.ftp.FTPClient#retrieveFileStream()

The following examples show how to use org.apache.commons.net.ftp.FTPClient#retrieveFileStream() . 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: FTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@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 2
Source File: FtpFileSystem.java    From xenon with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream readFromFile(Path path) throws XenonException {
    LOGGER.debug("newInputStream path = {}", path);

    assertIsOpen();
    Path absPath = toAbsolutePath(path);
    assertPathExists(absPath);
    assertPathIsFile(absPath);

    // Since FTP connections can only do a single thing a time, we need a
    // new FTPClient to handle the stream.
    FTPClient newClient = adaptor.connect(getLocation(), credential);
    newClient.enterLocalPassiveMode();

    try {
        InputStream in = newClient.retrieveFileStream(absPath.toString());

        checkClientReply(newClient, "Failed to read from path: " + absPath.toString());

        return new TransferClientInputStream(in, new CloseableClient(newClient));
    } catch (IOException e) {
        throw new XenonException(ADAPTOR_NAME, "Failed to read from path: " + absPath);
    }
}
 
Example 3
Source File: FTPTransfer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: FTPClientUtils.java    From springboot-ureport with Apache License 2.0 5 votes vote down vote up
/**
 * FTP下载文件
 * @param path 文件路径 
 * @throws IOException 
	 */
public InputStream downloadFile(String path)  {
	FTPClient ftpClient = borrowObject();
	try {
		byte[] bytes = path.getBytes("GBK");
		InputStream fileStream = ftpClient.retrieveFileStream(new String(bytes, "iso-8859-1"));
		log.info("文件 {} 下载成功!", path);
		return fileStream;
	} catch (IOException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}finally {
		returnObject(ftpClient);
	}
}
 
Example 5
Source File: FTPFileSystem.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDirectory()) {
    disconnect(client);
    throw new FileNotFoundException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 6
Source File: FTPFileSystem.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDirectory()) {
    disconnect(client);
    throw new FileNotFoundException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 7
Source File: FtpFileUtil.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Reads content of file on from FTP server to String.
 * @param hostName the FTP server host name to connect
 * @param port the port to connect
 * @param userName the user name
 * @param password the password
 * @param filePath file to read.
 * @return file's content.
 * @throws RuntimeException in case any exception has been thrown.
 */
public static String loadFileFromFTPServer(String hostName, Integer port,
        String userName, String password, String filePath, int numberOfLines) {

    String result = null;
    FTPClient ftpClient = new FTPClient();
    InputStream inputStream = null;
    String errorMessage = "Unable to connect and download file '%s' from FTP server '%s'.";

    try {
        connectAndLoginOnFTPServer(ftpClient, hostName, port, userName, password);

        // load file into string
        ftpClient.enterLocalPassiveMode();
        inputStream = ftpClient.retrieveFileStream(filePath);
        validatResponse(ftpClient);
        result = FileUtil.streamToString(inputStream, filePath, numberOfLines);
        ftpClient.completePendingCommand();

    } catch (IOException ex) {
        throw new RuntimeException(String.format(errorMessage, filePath, hostName), ex);

    } finally {
        closeInputStream(inputStream);
        disconnectAndLogoutFromFTPServer(ftpClient, hostName);
    }

    return result;
}
 
Example 8
Source File: FTPFileSystem.java    From RDFS with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDir()) {
    disconnect(client);
    throw new IOException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 9
Source File: FTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: FTPFileSystem.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDir()) {
    disconnect(client);
    throw new IOException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 11
Source File: RinexNavigation.java    From GNSS_Compare with Apache License 2.0 4 votes vote down vote up
private RinexNavigationParserGps getFromFTP(String url) throws IOException{
	RinexNavigationParserGps rnp = null;

	String origurl = url;
	if(negativeChache.containsKey(url)){
		if(System.currentTimeMillis()-negativeChache.get(url).getTime() < 60*60*1000){
			throw new FileNotFoundException("cached answer");
		}else{
			negativeChache.remove(url);
		}
	}

	String filename = url.replaceAll("[ ,/:]", "_");
	if(filename.endsWith(".Z")) filename = filename.substring(0, filename.length()-2);
	File rnf = new File(RNP_CACHE,filename);

	if(rnf.exists()){
     System.out.println(url+" from cache file "+rnf);
     rnp = new RinexNavigationParserGps(rnf);
     try{
       rnp.init();
       return rnp;
     }
     catch( Exception e ){
       rnf.delete();
     }
	}
	
	// if the file doesn't exist of is invalid
	System.out.println(url+" from the net.");
	FTPClient ftp = new FTPClient();

	try {
		int reply;
		System.out.println("URL: "+url);
		url = url.substring("ftp://".length());
		String server = url.substring(0, url.indexOf('/'));
		String remoteFile = url.substring(url.indexOf('/'));
		String remotePath = remoteFile.substring(0,remoteFile.lastIndexOf('/'));
		remoteFile = remoteFile.substring(remoteFile.lastIndexOf('/')+1);

		ftp.connect(server);
		ftp.login("anonymous", "[email protected]");

		System.out.print(ftp.getReplyString());

		// After connection attempt, you should check the reply code to
		// verify
		// success.
		reply = ftp.getReplyCode();

		if (!FTPReply.isPositiveCompletion(reply)) {
			ftp.disconnect();
			System.err.println("FTP server refused connection.");
			return null;
		}

		ftp.enterLocalPassiveMode();
		ftp.setRemoteVerificationEnabled(false);

		System.out.println("cwd to "+remotePath+" "+ftp.changeWorkingDirectory(remotePath));
		System.out.println(ftp.getReplyString());
		ftp.setFileType(FTP.BINARY_FILE_TYPE);
		System.out.println(ftp.getReplyString());

		System.out.println("open "+remoteFile);
		InputStream is = ftp.retrieveFileStream(remoteFile);
		System.out.println(ftp.getReplyString());
		if(ftp.getReplyString().startsWith("550")){
			negativeChache.put(origurl, new Date());
			throw new FileNotFoundException();
		}
     InputStream uis = is;

		if(remoteFile.endsWith(".Z")){
			uis = new UncompressInputStream(is);
		}

		rnp = new RinexNavigationParserGps(uis,rnf);
		rnp.init();
		is.close();


		ftp.completePendingCommand();

		ftp.logout();
	} 
	finally {
		if (ftp.isConnected()) {
			try {
				ftp.disconnect();
			} catch (IOException ioe) {
				// do nothing
			}
		}
	}
	return rnp;
}
 
Example 12
Source File: FTPUtil.java    From seed with Apache License 2.0 4 votes vote down vote up
/**
 * 文件下载
 * <p>
 *     文件下载失败时,该方法会自动登出服务器并释放FTP连接,然后抛出RuntimeException
 *     读取文件流后,一定要调用completePendingCommand()告诉FTP传输完毕,否则会导致该FTP连接在下一次读取不到文件
 * </p>
 * @param hostname  目标主机地址
 * @param username  FTP登录用户
 * @param password  FTP登录密码
 * @param remoteURL 保存在FTP上的含完整路径和后缀的完整文件名
 */
public static InputStream download(String hostname, String username, String password, String remoteURL){
    if(!login(hostname, username, password, DEFAULT_DEFAULT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT, DEFAULT_DATA_TIMEOUT)){
        throw new SeedException(CodeEnum.SYSTEM_BUSY.getCode(), "FTP服务器登录失败");
    }
    FTPClient ftpClient = ftpClientMap.get();
    try{
        //注意这里没有写成new String(remoteURL.getBytes(DEFAULT_CHARSET), "ISO-8859-1")
        //这是因为有时会因为编码的问题导致明明ftp上面有文件,但读到的是空数组,所以我们就使用默认编码
        FTPFile[] files = ftpClient.listFiles(new String(remoteURL.getBytes(DEFAULT_CHARSET)));
        if(1 != files.length){
            logout();
            throw new SeedException(CodeEnum.FILE_NOT_FOUND.getCode(), "远程文件["+remoteURL+"]不存在");
        }
        InputStream is = ftpClient.retrieveFileStream(remoteURL);
        //拷貝InputStream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int len;
        while((len=is.read(buff)) > -1){
            baos.write(buff, 0, len);
        }
        baos.flush();
        //事实上就像JDK的API所述:Closing a ByteArrayOutputStream has no effect
        //查询ByteArrayOutputStream.close()的源码会发现,它没有做任何事情,所以其close()与否是无所谓的
        baos.close();
        IOUtils.closeQuietly(is);
        //completePendingCommand()會一直等待FTPServer返回[226 Transfer complete]
        //但是FTPServer需要在InputStream.close()執行之後才會返回,所以要先執行InputStream.close()
        //201704121637測試發現:對於小文件,沒有調用close(),直接completePendingCommand()也會返回true
        //但大文件就會卡在completePendingCommand()位置,所以上面做了一步InputStream拷貝
        if(!ftpClient.completePendingCommand()){
            logout();
            throw new SeedException(CodeEnum.SYSTEM_BUSY.getCode(), "File transfer failed.");
        }
        return new ByteArrayInputStream(baos.toByteArray());
    }catch(IOException e){
        logout();
        throw new SeedException(CodeEnum.SYSTEM_BUSY.getCode(), "从FTP服务器["+hostname+"]下载文件["+remoteURL+"]失败,堆棧軌跡如下:", e);
    }
}
 
Example 13
Source File: FTPServerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test CRUD for FTP server
 *
 * @throws Exception
 */
public void testCRUD() throws Exception
{
    final String PATH1 = "FTPServerTest";
    final String PATH2 = "Second part";
    
    logger.debug("Start testFTPCRUD");
    
    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 successful", login);
                      
        reply = ftp.cwd("/Alfresco/User Homes");
        assertTrue(FTPReply.isPositiveCompletion(reply));
        
        // Delete the root directory in case it was left over from a previous test run
        try
        {
            ftp.removeDirectory(PATH1);
        }
        catch (IOException e)
        {
            // ignore this error
        }
        
        // make root directory
        ftp.makeDirectory(PATH1);
        ftp.cwd(PATH1);
        
        // make sub-directory in new directory
        ftp.makeDirectory(PATH2);
        ftp.cwd(PATH2);
        
        // List the files in the new directory
        FTPFile[] files = ftp.listFiles();
        assertTrue("files not empty", files.length == 0);
        
        // Create a file
        String FILE1_CONTENT_1="test file 1 content";
        String FILE1_NAME = "testFile1.txt";
        ftp.appendFile(FILE1_NAME , new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8")));
        
        // Get the new file
        FTPFile[] files2 = ftp.listFiles();
        assertTrue("files not one", files2.length == 1);
        
        InputStream is = ftp.retrieveFileStream(FILE1_NAME);
        
        String content = inputStreamToString(is);
        assertEquals("Content is not as expected", content, FILE1_CONTENT_1);
        ftp.completePendingCommand();
        
        // Update the file contents
        String FILE1_CONTENT_2="That's how it is says Pooh!";
        ftp.storeFile(FILE1_NAME , new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
        
        InputStream is2 = ftp.retrieveFileStream(FILE1_NAME);
        
        String content2 = inputStreamToString(is2);
        assertEquals("Content is not as expected", FILE1_CONTENT_2, content2);
        ftp.completePendingCommand();
        
        // now delete the file we have been using.
        assertTrue (ftp.deleteFile(FILE1_NAME));
        
        // negative test - file should have gone now.
        assertFalse (ftp.deleteFile(FILE1_NAME));
        
    } 
    finally
    {
        // clean up tree if left over from previous run

        ftp.disconnect();
    }    
}
 
Example 14
Source File: FTPServerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Create a user other than "admin" who has access to a set of files. 
 * 
 * Create a folder containing test.docx as user one
 * Update that file as user two.
 * Check user one can see user two's changes.
 * 
 * @throws Exception
 */
public void testTwoUserUpdate() throws Exception
{
    logger.debug("Start testFTPConnect");
    
    final String TEST_DIR="/Alfresco/User Homes/" + USER_ONE;
 
    FTPClient ftpOne = connectClient();
    FTPClient ftpTwo = connectClient();
    try
    {
        int reply = ftpOne.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            fail("FTP server refused connection.");
        }
        
        reply = ftpTwo.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            fail("FTP server refused connection.");
        }
    
        boolean login = ftpOne.login(USER_ONE, PASSWORD_ONE);
        assertTrue("user one login not successful", login);
        
        login = ftpTwo.login(USER_TWO, PASSWORD_TWO);
        assertTrue("user two login not successful", login);
        
        boolean success = ftpOne.changeWorkingDirectory("Alfresco");
        assertTrue("user one unable to cd to Alfreco", success);
        success = ftpOne.changeWorkingDirectory("User*Homes");
        assertTrue("user one unable to cd to User*Homes", success);
        success = ftpOne.changeWorkingDirectory(USER_ONE);
        assertTrue("user one unable to cd to " + USER_ONE, success);
        
        success = ftpTwo.changeWorkingDirectory("Alfresco");
        assertTrue("user two unable to cd to Alfreco", success);
        success = ftpTwo.changeWorkingDirectory("User*Homes");
        assertTrue("user two unable to cd to User*Homes", success);
        success = ftpTwo.changeWorkingDirectory(USER_ONE);
        assertTrue("user two unable to cd " + USER_ONE, success);

        // Create a file as user one
        String FILE1_CONTENT_1="test file 1 content";
        String FILE1_NAME = "test.docx";
        success = ftpOne.appendFile(FILE1_NAME , new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8")));
        assertTrue("user one unable to append file", success);
        
        // Update the file as user two
        String FILE1_CONTENT_2="test file content updated";
        success = ftpTwo.storeFile(FILE1_NAME , new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
        assertTrue("user two unable to append file", success);
        
        // User one should read user2's content
        InputStream is1 = ftpOne.retrieveFileStream(FILE1_NAME);
        assertNotNull("is1 is null", is1);
        String content1 = inputStreamToString(is1);
        assertEquals("Content is not as expected", FILE1_CONTENT_2, content1);
        ftpOne.completePendingCommand();
        
        // User two should read user2's content
        InputStream is2 = ftpTwo.retrieveFileStream(FILE1_NAME);
        assertNotNull("is2 is null", is2);
        String content2 = inputStreamToString(is2);
        assertEquals("Content is not as expected", FILE1_CONTENT_2, content2);
        ftpTwo.completePendingCommand();
        logger.debug("Test finished");
  
    } 
    finally
    {
        ftpOne.dele(TEST_DIR);
        if(ftpOne != null)
        {
            ftpOne.disconnect();
        }
        if(ftpTwo != null)
        {
            ftpTwo.disconnect();
        }
    }       

}
 
Example 15
Source File: CfdaServiceImpl.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @return
 * @throws IOException
 */
public SortedMap<String, CFDA> getGovCodes() throws IOException {
    Calendar calendar = dateTimeService.getCurrentCalendar();
    SortedMap<String, CFDA> govMap = new TreeMap<String, CFDA>();

    // ftp://ftp.cfda.gov/programs09187.csv
    String govURL = parameterService.getParameterValueAsString(CfdaBatchStep.class, KFSConstants.SOURCE_URL_PARAMETER);
    String fileName = StringUtils.substringAfterLast(govURL, "/");
    govURL = StringUtils.substringBeforeLast(govURL, "/");
    if (StringUtils.contains(govURL, "ftp://")) {
        govURL = StringUtils.remove(govURL, "ftp://");
    }

    // need to pull off the '20' in 2009
    String year = "" + calendar.get(Calendar.YEAR);
    year = year.substring(2, 4);
    fileName = fileName + year;

    // the last 3 numbers in the file name are the day of the year, but the files are from "yesterday"
    fileName = fileName + String.format("%03d", calendar.get(Calendar.DAY_OF_YEAR) - 1);
    fileName = fileName + ".csv";

    LOG.info("Getting government file: " + fileName + " for update");

    InputStream inputStream = null;
    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(govURL);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            LOG.error("FTP connection to server not established.");
            throw new IOException("FTP connection to server not established.");
        }

        boolean isLoggedIn = ftp.login("anonymous", "");
        if (!isLoggedIn) {
            LOG.error("Could not login as anonymous.");
            throw new IOException("Could not login as anonymous.");
        }

        LOG.info("Successfully connected and logged in");
        ftp.enterLocalPassiveMode();
        inputStream = ftp.retrieveFileStream(fileName);
        if (inputStream != null) {
            LOG.info("reading input stream");
            InputStreamReader screenReader = new InputStreamReader(inputStream);
            BufferedReader screen = new BufferedReader(screenReader);

            CSVReader csvReader = new CSVReader(screenReader, ',', '"', 1);
            List<String[]> lines = csvReader.readAll();
            for (String[] line : lines) {
                String title = line[0];
                String number = line[1];

                CFDA cfda = new CFDA();
                cfda.setCfdaNumber(number);
                cfda.setCfdaProgramTitleName(title);

                govMap.put(number, cfda);
            }
        }

        ftp.logout();
        ftp.disconnect();
    }
    finally {
        if (ftp.isConnected()) {
            ftp.disconnect();
        }
    }

    return govMap;
}
 
Example 16
Source File: FtpService.java    From cerberus-source with GNU General Public License v3.0 4 votes vote down vote up
@Override
public AnswerItem<AppService> getFTP(HashMap<String, String> informations, FTPClient ftp, AppService myResponse) throws IOException {
    MessageEvent message = null;
    AnswerItem<AppService> result = new AnswerItem<>();
    LOG.info("Start retrieving ftp file");
    FTPFile[] ftpFile = ftp.listFiles(informations.get("path"));
    if (ftpFile.length != 0) {
        InputStream done = ftp.retrieveFileStream(informations.get("path"));
        boolean success = ftp.completePendingCommand();
        myResponse.setResponseHTTPCode(ftp.getReplyCode());
        if (success && FTPReply.isPositiveCompletion(myResponse.getResponseHTTPCode())) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while ((n = done.read(buf)) >= 0) {
                baos.write(buf, 0, n);
            }
            byte[] content = baos.toByteArray();
            myResponse.setFile(content);
            LOG.info("ftp file successfully retrieve");
            message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CALLSERVICE);
            message.setDescription(message.getDescription().replace("%SERVICEMETHOD%", "GET"));
            message.setDescription(message.getDescription().replace("%SERVICEPATH%", informations.get("path")));
            result.setResultMessage(message);
            String expectedContent = IOUtils.toString(new ByteArrayInputStream(content), "UTF-8");
            String extension = testCaseExecutionFileService.checkExtension(informations.get("path"), "");
            if ("JSON".equals(extension) || "XML".equals(extension) || "TXT".equals(extension)) {
                myResponse.setResponseHTTPBody(expectedContent);
            }
            myResponse.setResponseHTTPBodyContentType(extension);
            result.setItem(myResponse);
            baos.close();
        } else {
            LOG.error("Error when downloading the file. Something went wrong");
            message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
            message.setDescription(message.getDescription().replace("%SERVICE%", informations.get("path")));
            message.setDescription(message.getDescription().replace("%DESCRIPTION%",
                    "Error when downloading the file. Something went wrong"));
            result.setResultMessage(message);
        }
        done.close();
    } else {
        LOG.error("The file is not present on FTP server. Please check the FTP path");
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);
        message.setDescription(message.getDescription().replace("%SERVICE%", informations.get("path")));
        message.setDescription(message.getDescription().replace("%DESCRIPTION%",
                "Impossible to retrieve the file. Please check the FTP path"));
        result.setResultMessage(message);
    }
    return result;
}