FTP는 파일 전송 프로토콜 (파일 전송 프로토콜)의 영어 약어이며 중국어 약어를 "서면 전송 프로토콜"이라고합니다. 인터넷에서 제어 파일의 양방향 전송에 사용됩니다. 동시에, 그것은 또한 응용 프로그램이기도합니다. 다른 운영 체제를 기반으로 다른 FTP 응용 프로그램이 있으며, 이러한 모든 응용 프로그램은 파일을 전송하기 위해 동일한 프로토콜을 준수합니다. FTP를 사용하면 사용자는 종종 "다운로드"와 "업로드"라는 두 가지 개념에 직면합니다. "다운로드"파일은 원격 호스트에서 자신의 컴퓨터로 파일을 복사하는 것을 의미합니다. "업로드"파일은 자신의 컴퓨터에서 원격 호스트로 파일을 복사하는 것을 의미합니다. 인터넷 언어에서 사용자는 클라이언트 프로그램을 통해 원격 호스트에 파일을 업로드 (다운로드) 할 수 있습니다.
먼저, Serv-U는 컴퓨터를 FTP 파일 서버로 설정하여 쉽게 작동 할 수 있도록 다운로드되었습니다.
1. FTP 파일 다운로드 (FTP 서버에서 로컬 컴퓨터로 다운로드)
import java.io.file; import java.io.fileinputStream; import java.io.filenotfoundException; import java.io.fileoutputStream; import java.io.ioexception; import java.io.inputStream; import java.io.outputStream; import org.apache.commons.net.ft.ftp; org.apache.commons.net.ftp.ftpclient; import org.apache.commons.net.ftp.ftpfile; import org.apache.commons.net.ftp.ftpreply; public class ftpapche {private static ftpclient ftpclient = new ftpclient (); system.getProperty ( "file.encoding");/*** 설명 : ftp 서버에서 파일을 다운로드하십시오** @version1.0* @param url* ftp server hostname* @param port* ftp server port* @param username* ftp login ac 다운로드* @param localpath* 다운로드 후* @return*/public static boolean downfile (문자열 URL, int 포트, 문자열 사용자 이름, 문자열 암호, 문자열 remotepath, 문자열 파일 이름, 문자열 로컬 포트) {boolean result = false; int reply; ftpclient.setcontrolencoding (encoding); * 새 문자열 대신 (remotepath.getBytes (인코딩), "ISO-8859-1") 트랜스 코딩 대신. * 테스트 후 통과 할 수 없습니다. */// ftpclientConfig conf = new ftpclientConfig (ftpclientConfig.syst_nt); // conf.SetServerLanguageCode ( "ZH"); ftpclient.connect (url, port); // default 포트가 사용되면 ftpclient. // //)에 의해 ftp 서버에 직접 연결할 수 있습니다. 로그인 // 파일 전송 유형을 바이너리 ftpclient.setFileType (ftpclient.binary_file_type)로 설정합니다. // ftp 로그인 응답 코드 답장 = ftpclient.getReplyCode (); // if (! ftpReply.ispositiveCoalmetion (reply)) {ftpclient.disconnect (); System.err.println ( "FTP 서버가 연결 거부. = ftpclient.listfiles (); for (ftpfile ff : fs) {if (ff.getname (). equals (filename)) {file localfile = new File (localPath + "/" + ff.getName ()); outputStream is = new FileoutPutStream (localFile); ftpclient.retrieve (ftpcclient.retrievefile). is); is.close ();}} ftpclient.logout (); result = true;} catch (ioException e) {e.printStackTrace ();} 최종 {if (ftpclient.isconnected ()) {try {ftpclient.disconnect ();} catch (ioexception i)}}}}}}}}}}}}}}} FTP 서버의 로컬 */public void testdownfile () {try {boolean flag = downfile ( "10.0.0.102", 21, "admin", "123456", "/", "ip.txt", "e :/"); System.out.println (flag)} {experce e)}}}}} {exprintic e)} main (string [] args) {ftpapche fa = new ftpapche (); fa.testdownfile ();}}2. FTP 파일 업로드 (로컬 컴퓨터에서 FTP 서버로 업로드)
import java.io.file; import java.io.fileinputStream; import org.apache.commons.net.ftpclient; import org.apache.commons.net.ftp.ftpreply; public class ftptest_05 {private ftpclient ftp;/*** @param 경로는 ftp server* @param 경로에 적용됩니다. @param 포트 번호* @param username username* @param password password* @return* @throws Exception*/ private boolean connect (문자열 경로, String addr, int port, string username, string password) 예외 {boolean result = false; ftp = new ftpclient (); 답장; ftp.connect (addr, port); ftp.login (username, password); ftp.setfiletype (ftpclient.binary_file_type); reply = ftp.getReplyCode (); if (! ftpreply.ispositevecompletion (replay)) {ftp.disconnect (); 결과;} ftp.changeworkingDirectory (path); result = true; return result;}/ ** * * @param 파일 업로드 파일 또는 폴더 * @throws 예외 */ private void upload (파일 파일) 던지기 예외 {if (file.isdirectory ()) {ftp.makedirectory (file.getname ()); ftp.changeworkingDirectory (file.getName ()); String [] files = file.list (); for (int i = 0; i <file.length; i ++) {file (file.get.getpath ()+"+"+"+"+" ); if (file1.isdirectory ()) {upload (file1); ftp.changetoparentDirectory ();} else {file file2 = new File (file.getPath ()+"//"+파일 [i]); fileInputStream = new FileInputStream (fileInputStream (file2); ftp.storefile (ftp.storefile) input); input.close ();}}} else {file file2 = new File (file.getPath ()); fileInputStream input = new FileInputStream (file2); ftp.storeFile (file2.getName (), 입력); input.close ();}} public void main (strows) throws). ftptest_05 (); boolean connflag = t.connect ( "/", "10.0.0.105", 21, "ls", "123456"); System.out.println ( " + connflag); 파일 = 새 파일 ("d : // test02 "); + file); t.upload (file); system.out.println ( "업로드 :" + "OK");}}위의 것은 Java가 편집기가 소개 한 FTP 파일의 업로드 및 다운로드 기능을 구현할 수있는 예제 코드입니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!