π νΉμ FTP μ£Όμλ‘ μ μν΄μ νμΌμ λ°μμμ νμ±μ νλ μμ .

1. FTP μλ²λ‘ μ μν΄μ λ°μ΄ν° ( TXT ) νμΌμ κ°μ Έμ¨λ€.


FTP μ μ μμ± ν¨μ : :
import org.apache.commons.net.ftp.FTPClient;
public class FTPDownload {
private static final Logger log_error = LogManager.getLogger("com.error");
FTPClient ftp = null;
public FTPDownload(String host, String user, String pwd) throws Exception {
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.setControlEncoding("EUC-KR");
ftp.connect(host);
reply = ftp.getReplyCode();
if ( !FTPReply.isPositiveCompletion(reply) ) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
boolean flag = ftp.login(user, pwd);
if ( !flag ) {
log_error.fatal(" ** FTP μλ² μ μ μ€ μ€λ₯κ° λ°μνμ΅λλ€. ");
log_error.error(" invalid id / pw ftp server : " + host + " / id : " + user + " / pw : " + pwd);
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
}
λ€μ΄λ‘λλ₯Ό μν΄ μ§μ μ격μ§λ‘ μ μ : :
public boolean downloadFile(String remoteFilePath, String localFilePath) throws Exception {
boolean flag = false;
File file = new File (remoteFilePath);
if ( !file.exists() ){
log_error.fatal(" ** μλ²μ νμΌμ΄ μμ΅λλ€. :: " + remoteFilePath + " :: lcal file path : " + localFilePath);
return false;
} else {
// System.out.println(" ftp file download :: " + remoteFilePath + " / :: " + localFilePath );
}
FileOutputStream fos = new FileOutputStream(localFilePath);
flag = this.ftp.retrieveFile(remoteFilePath, fos);
return flag;
}
μμ μ½λμμ μ μ ν κ²½μ° fileμ κ²½λ‘λ₯Ό μ°Ύμ§ λͺ»νλ κ²½μ°κ° μμλ€.
$ ftp 111.222.333.444
$ id
$ pw


λͺ λ Ήμ΄λ₯Ό μ€ννλ©΄ νμΌλ μ μμ μΌλ‘ λ°μμ§λ κ²μ νμΈ ν μ μλ€.
π μλ¬ μμΈμ μλΉμ€ μ 곡μ¬μμ νμΌμ μ μ₯νλ μμΉκ° λ°λ κ²½μ°μλ€.
νμΌ μ μ₯ κ²½λ‘λ μ€μ κ°μΌλ‘ μ°Ύμμ λ€μ΄κ°κΈ° λλ¬Έμ μ€μ κ²½λ‘ λ°κΏμ μ²λ¦¬νμ.
'JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
java μ κ· ννμ (0) | 2022.05.09 |
---|---|
java Stream (0) | 2022.03.08 |
Stack (0) | 2022.02.23 |
java / POI / excel (0) | 2022.02.03 |
spring / intellij μ€μ (0) | 2022.02.01 |
π νΉμ FTP μ£Όμλ‘ μ μν΄μ νμΌμ λ°μμμ νμ±μ νλ μμ .

1. FTP μλ²λ‘ μ μν΄μ λ°μ΄ν° ( TXT ) νμΌμ κ°μ Έμ¨λ€.


FTP μ μ μμ± ν¨μ : :
import org.apache.commons.net.ftp.FTPClient;
public class FTPDownload {
private static final Logger log_error = LogManager.getLogger("com.error");
FTPClient ftp = null;
public FTPDownload(String host, String user, String pwd) throws Exception {
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.setControlEncoding("EUC-KR");
ftp.connect(host);
reply = ftp.getReplyCode();
if ( !FTPReply.isPositiveCompletion(reply) ) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
boolean flag = ftp.login(user, pwd);
if ( !flag ) {
log_error.fatal(" ** FTP μλ² μ μ μ€ μ€λ₯κ° λ°μνμ΅λλ€. ");
log_error.error(" invalid id / pw ftp server : " + host + " / id : " + user + " / pw : " + pwd);
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
}
λ€μ΄λ‘λλ₯Ό μν΄ μ§μ μ격μ§λ‘ μ μ : :
public boolean downloadFile(String remoteFilePath, String localFilePath) throws Exception {
boolean flag = false;
File file = new File (remoteFilePath);
if ( !file.exists() ){
log_error.fatal(" ** μλ²μ νμΌμ΄ μμ΅λλ€. :: " + remoteFilePath + " :: lcal file path : " + localFilePath);
return false;
} else {
// System.out.println(" ftp file download :: " + remoteFilePath + " / :: " + localFilePath );
}
FileOutputStream fos = new FileOutputStream(localFilePath);
flag = this.ftp.retrieveFile(remoteFilePath, fos);
return flag;
}
μμ μ½λμμ μ μ ν κ²½μ° fileμ κ²½λ‘λ₯Ό μ°Ύμ§ λͺ»νλ κ²½μ°κ° μμλ€.
$ ftp 111.222.333.444
$ id
$ pw


λͺ λ Ήμ΄λ₯Ό μ€ννλ©΄ νμΌλ μ μμ μΌλ‘ λ°μμ§λ κ²μ νμΈ ν μ μλ€.
π μλ¬ μμΈμ μλΉμ€ μ 곡μ¬μμ νμΌμ μ μ₯νλ μμΉκ° λ°λ κ²½μ°μλ€.
νμΌ μ μ₯ κ²½λ‘λ μ€μ κ°μΌλ‘ μ°Ύμμ λ€μ΄κ°κΈ° λλ¬Έμ μ€μ κ²½λ‘ λ°κΏμ μ²λ¦¬νμ.
'JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
java μ κ· ννμ (0) | 2022.05.09 |
---|---|
java Stream (0) | 2022.03.08 |
Stack (0) | 2022.02.23 |
java / POI / excel (0) | 2022.02.03 |
spring / intellij μ€μ (0) | 2022.02.01 |