本文整理匯總了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();
}
}
}