本文整理汇总了Java中org.apache.commons.net.ftp.FTPClient.retrieveFile方法的典型用法代码示例。如果您正苦于以下问题:Java FTPClient.retrieveFile方法的具体用法?Java FTPClient.retrieveFile怎么用?Java FTPClient.retrieveFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.retrieveFile方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: download
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 下载文件
*
* @param remoteDir 远程操作目录
* @param remoteFileName 远程下载文件名
* @param downloadFile 下载文件
* @return 下载结果<br> true - 下载成功<br>
* false - 下载失败
*/
public boolean download(String remoteDir, String remoteFileName, File downloadFile) {
FTPClient ftp = null;
try {
ftp = initFtpClient(remoteDir);
if (ftp == null) {
logger.debug("ftp初始化失败");
return false;
}
try (OutputStream os = new FileOutputStream(downloadFile)) {
boolean storeRet = ftp.retrieveFile(remoteFileName, os);
if (!storeRet) {
logger.debug("下载文件失败");
return false;
}
}
return true;
} catch (IOException e) {
logger.error("FTP操作异常", e);
return false;
} finally {
close(ftp);
}
}
示例2: download
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public static byte[] download(String url, int port, String username, String password, String remotePath,
String fileName) throws IOException {
FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000);
ftp.setAutodetectUTF8(true);
ftp.setCharset(CharsetUtil.UTF_8);
ftp.setControlEncoding(CharsetUtil.UTF_8.name());
try {
ftp.connect(url, port);
ftp.login(username, password);// 登录
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
throw new IOException("login fail!");
}
ftp.changeWorkingDirectory(remotePath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
try (ByteArrayOutputStream is = new ByteArrayOutputStream();) {
ftp.retrieveFile(ff.getName(), is);
byte[] result = is.toByteArray();
return result;
}
}
}
ftp.logout();
} finally {
if (ftp.isConnected()) {
ftp.disconnect();
}
}
return null;
}
示例3: downloadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 从FTP服务器下载指定的文件至本地
*
* @author gaoxianglong
*/
public boolean downloadFile(File file) {
boolean result = false;
FTPClient ftpClient = ftpConnManager.getFTPClient();
if (null == ftpClient || !ftpClient.isConnected()) {
return result;
}
try (BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(System.getProperty("user.home") + "/" + file.getName()))) {
result = ftpClient.retrieveFile(file.getName(), out);
if (result) {
result = true;
log.info("file-->" + file.getPath() + "成功从FTP服务器下载");
}
} catch (Exception e) {
log.error("error", e);
} finally {
disconnect(ftpClient);
}
return result;
}
示例4: downloadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public static boolean downloadFile(String url, int port, String username, String password,
String remotePath, String fileName, String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
System.out.println(fs.length);
for (FTPFile ff : fs) {
System.out.println(ff.getName());
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();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
示例5: downloadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* FTP下载
*
* @author wangwei
*/
public void downloadFile(String downloadFilename, String remoteFolder,
OutputStream outputStream, HttpServletResponse response)
throws Exception {
// 连接FTP服务器
FTPClient ftp = this.connectFTPServer();
try {
this.changeDirectory(remoteFolder);
FTPFile[] fs = ftp.listFiles();
for (int i = 0; i < fs.length; i++) {
FTPFile ff = fs[i];
if (ff.getName().equals(downloadFilename)) {
response.setHeader(
"Content-disposition",
"attachment;filename="
+ URLEncoder.encode(downloadFilename,
"utf-8"));
boolean result = ftp.retrieveFile(new String(ff.getName()
.getBytes("GBK"), "ISO-8859-1"), outputStream);
if (!result) {
throw new Exception("FTP文件下载失败!");
}
outputStream.flush();
}
}
} catch (Exception e) {
throw e;
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}