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

The following examples show how to use org.apache.commons.net.ftp.FTPClient#retrieveFile() . 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: FtpUtils.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
public static void download(String ftpUrl, String localFilePath, String ftpUsername, String ftpPassword, String ftpControlEncoding) throws IOException {
    String username = StringUtils.isEmpty(ftpUsername) ? ConfigConstants.getFtpUsername() : ftpUsername;
    String password = StringUtils.isEmpty(ftpPassword) ? ConfigConstants.getFtpPassword() : ftpPassword;
    String controlEncoding = StringUtils.isEmpty(ftpControlEncoding) ? ConfigConstants.getFtpControlEncoding() : ftpControlEncoding;
    URL url = new URL(ftpUrl);
    String host = url.getHost();
    int port = (url.getPort() == -1) ? url.getDefaultPort() : url.getPort();
    String remoteFilePath = url.getPath();
    LOGGER.debug("FTP connection url:{}, username:{}, password:{}, controlEncoding:{}, localFilePath:{}", ftpUrl, username, password, controlEncoding, localFilePath);
    FTPClient ftpClient = connect(host, port, username, password, controlEncoding);
    OutputStream outputStream = new FileOutputStream(localFilePath);
    ftpClient.enterLocalPassiveMode();
    boolean downloadResult = ftpClient.retrieveFile(new String(remoteFilePath.getBytes(controlEncoding), StandardCharsets.ISO_8859_1), outputStream);
    LOGGER.debug("FTP download result {}", downloadResult);
    outputStream.flush();
    outputStream.close();
    ftpClient.logout();
    ftpClient.disconnect();
}
 
Example 2
Source File: MyFtp.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void getDataFile(
    FTPFile[] files, String destinationFolder, Calendar start, Calendar end, FTPClient ftp) {
  for (int i = 0; i < files.length; i++) {

    Date fileDate = files[i].getTimestamp().getTime();
    if (fileDate.compareTo(start.getTime()) >= 0 && fileDate.compareTo(end.getTime()) <= 0) {

      // Download a file from the FTP Server
      File file = new File(destinationFolder + File.separator + files[i].getName());
      try (FileOutputStream fos = new FileOutputStream(file); ) {
        ftp.retrieveFile(files[i].getName(), fos);
        file.setLastModified(fileDate.getTime());
      } catch (Exception e) {
        LOG.error(e.getMessage());
        TraceBackService.trace(e);
      }
    }
  }
}
 
Example 3
Source File: FTPClientUtils.java    From springboot-ureport with Apache License 2.0 5 votes vote down vote up
/**
 *  FTP下载文件 到 输出流
 * @param path ftp文件路径 
 * @param ops  
 * @return 是否写入成功
 */
public boolean downloadFile(String path, OutputStream ops)  {
	FTPClient ftpClient = borrowObject();
	try {
		boolean result = ftpClient.retrieveFile(path, ops);
		return result;
	} catch (IOException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}finally {
		returnObject(ftpClient);
	}
}
 
Example 4
Source File: FTPClientTemplate.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/** 
 * 下载一个远程文件到指定的流 处理完后记得关闭流 
 *  
 * @param remoteAbsoluteFile 
 * @param output 
 * @param
 * @return 
 * @throws Exception
 */  
public boolean get(String remoteAbsoluteFile, OutputStream output, boolean autoClose) throws Exception {
    try {  
        FTPClient ftpClient = getFTPClient();
        // 处理传输  
        return ftpClient.retrieveFile(remoteAbsoluteFile, output);  
    } catch (IOException e) {
        throw new Exception("Couldn't get file from server.", e);
    } finally {  
        if (autoClose) {  
            disconnect(); //关闭链接  
        }  
    }  
}
 
Example 5
Source File: FtpUtil.java    From learning-taotaoMall with MIT License 5 votes vote down vote up
/** 
 * Description: 从FTP服务器下载文件 
 * @param host FTP服务器hostname 
 * @param port FTP服务器端口 
 * @param username FTP登录账号 
 * @param password FTP登录密码 
 * @param remotePath FTP服务器上的相对路径 
 * @param fileName 要下载的文件名 
 * @param localPath 下载后保存到本地的路径 
 * @return 
 */
public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) {
	boolean result = false;
	FTPClient ftp = new FTPClient();
	try {
		int reply;
		ftp.connect(host, port);
		// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
		ftp.login(username, password);// 登录
		reply = ftp.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply)) {
			ftp.disconnect();
			return result;
		}
		ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
		FTPFile[] fs = ftp.listFiles();
		for (FTPFile ff : fs) {
			if (ff.getName().equals(fileName)) {
				File localFile = new File(localPath + "/" + ff.getName());

				OutputStream is = new FileOutputStream(localFile);
				ftp.retrieveFile(ff.getName(), is);
				is.close();
			}
		}

		ftp.logout();
		result = true;
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (ftp.isConnected()) {
			try {
				ftp.disconnect();
			} catch (IOException ioe) {
			}
		}
	}
	return result;
}
 
Example 6
Source File: RomLoader.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
static byte[] loadFTPArchive(final String host, final String path, final String name,
                             final String password) throws IOException {
  final FTPClient client = new FTPClient();
  client.connect(host);
  int replyCode = client.getReplyCode();

  if (FTPReply.isPositiveCompletion(replyCode)) {
    try {
      client.login(name == null ? "" : name, password == null ? "" : password);
      client.setFileType(FTP.BINARY_FILE_TYPE);
      client.enterLocalPassiveMode();

      final ByteArrayOutputStream out = new ByteArrayOutputStream(300000);
      if (client.retrieveFile(path, out)) {
        return out.toByteArray();
      } else {
        throw new IOException(
            "Can't load file 'ftp://" + host + path + "\' status=" + client.getReplyCode());
      }
    } finally {
      client.disconnect();
    }
  } else {
    client.disconnect();
    throw new IOException("Can't connect to ftp '" + host + "'");
  }
}
 
Example 7
Source File: Ftp.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
public File retrieve(String name,String fileName) throws IOException {
	String path = System.getProperty("java.io.tmpdir");
	File file = new File(path, fileName);
	file = UploadUtils.getUniqueFile(file);

	FTPClient ftp = getClient();
	OutputStream output = new FileOutputStream(file);
	ftp.retrieveFile(getPath() + name, output);
	output.close();
	ftp.logout();
	ftp.disconnect();
	return file;
}
 
Example 8
Source File: FTPService.java    From cs-actions with Apache License 2.0 4 votes vote down vote up
private void retrieveFile(FTPClient ftp, String remoteFilePath, OutputStream out) throws IOException, FTPException {
    ftp.retrieveFile(remoteFilePath, out);
    checkReply("get " + remoteFilePath, ftp);
}