자바 연결 ftp 다운로드
2521 단어 자바
/** ftp */
private String ftpHost;
/** ftp */
private String ftpName;
/** ftp */
private String ftpPass;
/** ftp */
private String ftpDir;
/** */
private String localPath;
public boolean downloadFile(String path, String fileName) {
//
String fullPath = path + fileName;
FTPClient client = new FTPClient();
//
client.setConnectTimeout(30000);
File dir = new File(localPath + path);
//
if (!dir.exists())
dir.mkdirs();
try {
// ftp
client.connect(ftpHost);
int replyCode = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
client.disconnect();
return false;
}
replyCode = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
client.quit();
return false;
}
//
if (client.login(ftpName, ftpPass)) {
// 2
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
client.disconnect();
logger.error(" ftp ");
}
/** ftp server system type */
FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
/** server language */
config.setServerLanguageCode("zh");
/** server time zone */
config.setServerTimeZoneId("Asia/Shanghai");
/** ftp transfer mode in binary */
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
/** receive buffer size */
client.setReceiveBufferSize(524288);
client.configure(config);
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.changeWorkingDirectory(ftpDir);// FTP
//
return client.retrieveFile(ftpDir + fullPath, new FileOutputStream(localPath + fullPath));
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (client.isConnected()) {
try {
client.logout();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
안의 구체 적 인 매개 변 수 는 상황 에 따라 설정 할 수 있 고 apache 의 오픈 소스 프로젝트 입 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.