250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- jpa
- IntelliJ
- STS
- PostgreSQL
- ubuntu
- oracle
- Exception
- Python
- Eclipse
- Source
- Core Java
- Docker
- maven
- SpringBoot
- Tomcat
- Spring Boot
- 문서
- myBatis
- error
- Thymeleaf
- spring
- AJAX
- Open Source
- MySQL
- 설정
- JavaScript
- JDBC
- 오픈소스
- git
- MSSQL
Archives
- Today
- Total
헤르메스 LIFE
[Source] Apache commons FTPClient Java example - Download files from server 본문
Core Java
[Source] Apache commons FTPClient Java example - Download files from server
헤르메스의날개 2022. 3. 15. 18:45728x90
출처 : http://www.mysamplecode.com/2012/03/apache-commons-ftpclient-java-example_16.html
package com.as400samplecode;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class GetMyFiles {
static Properties props;
public static void main(String[] args) {
GetMyFiles getMyFiles = new GetMyFiles();
if (args.length < 1)
{
System.err.println("Usage: java " + getMyFiles.getClass().getName()+
" Properties_file");
System.exit(1);
}
String propertiesFile = args[0].trim();
getMyFiles.startFTP(propertiesFile);
}
public boolean startFTP(String propertiesFile){
props = new Properties();
try {
props.load(new FileInputStream("properties/" + propertiesFile));
String serverAddress = props.getProperty("serverAddress").trim();
String userId = props.getProperty("userId").trim();
String password = props.getProperty("password").trim();
String remoteDirectory = props.getProperty("remoteDirectory").trim();
String localDirectory = props.getProperty("localDirectory").trim();
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
ftp.connect(serverAddress);
//login to server
if(!ftp.login(userId, password))
{
ftp.logout();
return false;
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return false;
}
//enter passive mode
ftp.enterLocalPassiveMode();
//get system name
System.out.println("Remote system is " + ftp.getSystemType());
//change current directory
ftp.changeWorkingDirectory(remoteDirectory);
System.out.println("Current directory is " + ftp.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("File is " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream(localDirectory + "/" + file.getName());
//get the file from the remote system
ftp.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
ftp.deleteFile(file.getName());
}
}
ftp.logout();
ftp.disconnect();
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
return true;
}
}
#Properties File FTP to server
serverAddress=ftp.someDomain.com
userId=yourUserId
password=yourPassword
remoteDirectory=/remote/MyFolder
localDirectory=/local/MyFolder
|
package lib;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;
import org.apache.commons.net.ftp.FTPSClient;
public class FTPClientTest {
private static FTPSClient client = new FTPSClient();
public static void main(String[] args) {
try {
client.connect("dt.kcmkt.com", 21);
System.out.println("wDConnect() : " + client.getReplyString());
// passivemode
client.enterRemotePassiveMode();
System.out.println("wDConnect() : " + client.getReplyString());
client.enterLocalPassiveMode();
System.out.println("wDConnect() : " + client.getReplyString());
} catch (Exception e) {
e.printStackTrace();
}
if (client.isConnected()) {
System.out.println("wDConnect() : " + client.isConnected());
} else {
System.out.println("FTP connect fail!!");
}
try {
if (client.login("WKRY000001@dtwkry.kcmkt.com", "v6c5X$z3")) {
System.out.println("FTP login ok ................... !!");
} else {
System.out.println("FTP login connect fail!!");
}
} catch (Exception e) {
e.printStackTrace();
}
try {
client.changeWorkingDirectory("/Prod/Out/");
System.out.println("wDChangeWorkDir() : " + client.getReplyString());
FTPFile[] FTPFILELIST = client.listFiles();
for (FTPFile ftpFile : FTPFILELIST) {
System.out.println("wDChangeWorkDir() : " + ftpFile.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
try {
client.logout();
System.out.println("FTP client logout !!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client.isConnected()) {
try {
client.disconnect();
System.out.println("FTP client disconnect !!");
} catch (Exception ie) {
ie.printStackTrace();
}
}
}
}
}
728x90
'Core Java' 카테고리의 다른 글
CXF 2.3.11 설정 (0) | 2022.08.11 |
---|---|
[Open Source] MySql Password Encoder (0) | 2022.03.21 |
[Core Java] File List 떨구는 샘플 (0) | 2021.03.23 |
[Core Java] 외부명령 실행하기 (0) | 2021.03.23 |
[Core Java] QRCode 출력 (0) | 2021.03.23 |