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

The following examples show how to use org.apache.commons.net.ftp.FTPClient#completePendingCommand() . 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: UploadThread.java    From imageServer with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件到服务器,新上传和断点续传
 *
 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
 * @param localFile  本地文件 File句柄,绝对路径
 * @param ftpClient  FTPClient 引用
 * @param remoteSize 远程已上传的文件大小
 * @return
 * @throws IOException
 */
private boolean uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException {
    //显示进度的上传
    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localFile, "r");
    OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
    //断点续传
    if (remoteSize > 0) {
        ftpClient.setRestartOffset(remoteSize);
        process = remoteSize / step;
        raf.seek(remoteSize);
        localreadbytes = remoteSize;
    }
    /* 每一次上传的数据量 */
    byte[] bytes = new byte[1024*10];
    int c;
    while ((c = raf.read(bytes)) != -1) {
        out.write(bytes, 0, c);
        localreadbytes += c;
        if (localreadbytes / step != process) {
            process = localreadbytes / step;
            log.info("上传进度:" + process);
        }
    }
    out.flush();
    raf.close();
    out.close();
    return ftpClient.completePendingCommand();
}
 
Example 2
Source File: UploadThread.java    From imageServer with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件到服务器,新上传和断点续传
 *
 * @param pipedOutputStream 输出的连接管道流
 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
 * @param localFile  本地文件 File句柄,绝对路径
 * @param ftpClient  FTPClient 引用
 * @param remoteSize 远程已上传的文件大小
 * @return
 * @throws IOException
 */
private boolean uploadFile(PipedOutputStream pipedOutputStream,String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException {
    //显示进度的上传
    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localFile, "r");
    OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
    //断点续传
    if (remoteSize > 0) {
        ftpClient.setRestartOffset(remoteSize);
        process = remoteSize / step;
        raf.seek(remoteSize);
        localreadbytes = remoteSize;
    }
    /* 每一次上传的数据量 */
    byte[] bytes = new byte[1024*10];
    int c;
    while ((c = raf.read(bytes)) != -1) {
        out.write(bytes, 0, c);
        localreadbytes += c;
        if (localreadbytes / step != process) {
            process = localreadbytes / step;
            pipedOutputStream.write(Integer.valueOf(process+""));
            pipedOutputStream.flush();
            log.info("上传进度:" + process);
        }
    }
    out.flush();
    out.close();
    raf.close();
    return ftpClient.completePendingCommand();
}
 
Example 3
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 4
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 5
Source File: FtpContinueClient.java    From spring-boot-study with MIT License 4 votes vote down vote up
/**
 * 上传文件到服务器,新上传和断点续传
 *
 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
 * @param localFile 本地文件File句柄,绝对路径
 * @param ftpClient FTPClient引用
 * @return
 * @throws IOException
 */
public UploadStatus uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize)
        throws IOException {
    UploadStatus status;
    // 显示进度的上传
    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localFile, "r");
    OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
    // 断点续传
    if (remoteSize > 0)
    {
        ftpClient.setRestartOffset(remoteSize);
        process = remoteSize / step;
        raf.seek(remoteSize);
        localreadbytes = remoteSize;
    }
    byte[] bytes = new byte[1024];
    int c;
    while ((c = raf.read(bytes)) != -1)
    {
        out.write(bytes, 0, c);
        localreadbytes += c;
        if (localreadbytes / step != process)
        {
            process = localreadbytes / step;
            System.out.println("上传进度:" + process);
            // TODO 汇报上传状态
        }
    }
    out.flush();
    raf.close();
    out.close();
    boolean result = ftpClient.completePendingCommand();
    if (remoteSize > 0)
    {
        status = result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed;
    }
    else
    {
        status = result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed;
    }

    return status;
}
 
Example 6
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 7
Source File: FTPTransfer.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void flush() throws IOException {
    final FTPClient client = getClient(null);
    client.completePendingCommand();
}
 
Example 8
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 9
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 10
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 11
Source File: FTPFileSystem.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus status;
  try {
    status = getFileStatus(client, file);
  } catch (FileNotFoundException fnfe) {
    status = null;
  }
  if (status != null) {
    if (overwrite && !status.isDirectory()) {
      delete(client, file, false);
    } else {
      disconnect(client);
      throw new FileAlreadyExistsException("File already exists: " + file);
    }
  }
  
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. 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
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  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
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
Example 12
Source File: FTPFileSystem.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus status;
  try {
    status = getFileStatus(client, file);
  } catch (FileNotFoundException fnfe) {
    status = null;
  }
  if (status != null) {
    if (overwrite && !status.isDirectory()) {
      delete(client, file, false);
    } else {
      disconnect(client);
      throw new FileAlreadyExistsException("File already exists: " + file);
    }
  }
  
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. 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
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  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
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
Example 13
Source File: FTPFileSystem.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  if (exists(client, file)) {
    if (overwrite) {
      delete(client, file);
    } else {
      disconnect(client);
      throw new IOException("File already exists: " + file);
    }
  }
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. 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
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  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
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
Example 14
Source File: FtpCheck.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
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 15
Source File: FTPFileSystem.java    From hadoop-gpu with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  if (exists(client, file)) {
    if (overwrite) {
      delete(client, file);
    } else {
      disconnect(client);
      throw new IOException("File already exists: " + file);
    }
  }
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. 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
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  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
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
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;
}