Apache FTP 도구를 사용하여 다음과 같이 파일을 업로드, 다운로드 및 삭제하십시오.
1. 해당 JAR 패키지를 다운로드하십시오
Commons-Net-1.4.1.jar
2. 구현 코드는 다음과 같습니다.
공개 클래스 ftputils {// ftp 서버 주소 public string hostname = "192.168.1.249"; // FTP 서버 포트 번호 기본값은 21 공개 정수 포트 = 21; // ftp 로그인 계정 public String username = "root"; // ftp 로그인 암호 public String password = "123"; 공개 ftpclient ftpclient = null; / *** ftp server 초기화*/ public void initftpclient () {ftpclient = new ftpclient (); ftpclient.setcontrolencoding ( "UTF-8"); try {System.out.println ( "Connecting ... ftp server :"+this.hostname+":"+this.port); ftpclient.connect (호스트 이름, 포트); // FTP 서버를 연결하여 ftpclient.login (사용자 이름, 비밀번호); // ftp 서버에 로그인 int replyCode = ftpclient.getReplyCode (); // 서버가 if (! ftpReply.ispositiveCombletion (replyCode)) {System.out.println ( "Connect 실패 ... FTP 서버 :"+this.HostName+":"+this.port); } system.out.println ( "Connect FutherFu ... ftp 서버 :"+this.hostname+":"+this.port); } catch (marlomedurlexception e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); }} / *** 파일 업로드* @param pathName ftp 서비스 저장 주소* @param filename filename ftp* @param originfilename 업로드 할 파일의 이름 (절대 주소)** / public boolean uploadfile (문자열 pathname, 문자열 filename, stringfilename) {boolean = 거짓; inputStream inputStream = null; try {system.out.println ( "파일 업로드 시작"); inputStream = new FileInputStream (새 파일 (OriginFileName)); initftpclient (); ftpclient.setFileType (ftpclient.binary_file_type); createirecroty (pathname); ftpclient.makedirectory (pathname); ftpclient.changeworkingdirectory (pathname); ftpclient.storefile (filename, inputstream); inputStream.close (); ftpclient.logout (); flag = true; System.out.println ( "파일 업로드"); } catch (예외 e) {System.out.println ( "파일 업로드 실패"); e.printstacktrace (); } 마지막으로 {if (ftpclient.isconnected ()) {try {ftpclient.disconnect (); } catch (ioexception e) {e.printstacktrace (); }} if (null! = inputStream) {try {inputStream.close (); } catch (ioexception e) {e.printstacktrace (); }}} return true; } / *** 파일 업로드* @param pathName ftp 서비스 저장 주소* @param filename 파일 이름 ftp에 업로드* @param inputstream 입력 파일 스트림* / public boolean uploadfile (문자열 파일 이름, 입력 inputstream) {boolean flag = false; try {system.out.println ( "파일 업로드 시작"); initftpclient (); ftpclient.setFileType (ftpclient.binary_file_type); createirecroty (pathname); ftpclient.makedirectory (pathname); ftpclient.changeworkingdirectory (pathname); ftpclient.storefile (filename, inputstream); inputStream.close (); ftpclient.logout (); flag = true; System.out.println ( "파일 업로드"); } catch (예외 e) {System.out.println ( "파일 업로드 실패"); e.printstacktrace (); } 마지막으로 {if (ftpclient.isconnected ()) {try {ftpclient.disconnect (); } catch (ioexception e) {e.printstacktrace (); }} if (null! = inputStream) {try {inputStream.close (); } catch (ioexception e) {e.printstacktrace (); }} true를 반환합니다. } // 디렉토리 경로 변경 public boolean changeworkingdirectory (문자열 디렉토리) {부울 플래그 = true; try {flag = ftpclient.changeworkingDirectory (directory); if (flag) {system.out.println ( "폴더 입력" + directory + "success!"); } else {system.out.println ( "폴더 입력" + directory + "실패! 폴더 만들기 시작"); }} catch (ioexception ioe) {ioe.printstacktrace (); } 반환 플래그; } // 다층 디렉토리 파일을 만듭니다. FTP 서버가 있으면 생성되지 않습니다. 없는 경우 공개 부울 창작 (String Remote)을 만들어 {부울 성공 = true; 문자열 디렉토리 = 원격 + "/"; // 원격 디렉토리가 존재하지 않으면 원격 서버 디렉토리를 재귀 적으로 만듭니다. int end = 0; if (directory.startSwith ( "/")) {start = 1; } else {start = 0; } end = directory.indexof ( "/", 시작); 문자열 경로 = ""; 문자열 경로 = ""; while (true) {String subdirectory = new String (remote.substring (start, end) .getBytes ( "gbk"), "iso-8859-1"); Path = Path + "/" + 하위 디렉토리; if (! ustryfile (path)) {if (makedirectory (subdirectory)) {changeworkingDirectory (subdirectory); } else {system.out.println ( "directory 만들기 [" + subdirectory + "] 실패"); ChangeWorkingDirectory (하위 디렉토리); }} else {changeWorkingDirectory (하위 디렉토리); } paths = paths + "/" + 하위 디렉토리; start = end + 1; end = directory.indexof ( "/", 시작); // 모든 디렉토리가 생성되었는지 확인하십시오 (END <= start) {break; }}} 반환 성공; } // FTP 서버 파일이 존재하는지 여부를 결정합니다. 공개 부울 ExpationFile (String Path)는 ioexception {boolean flag = false; ftpfile [] ftpfilearr = ftpclient.listfiles (path); if (ftpfilearr.length> 0) {flag = true; } 반환 플래그; } // 디렉토리 작성 public boolean makedirectory (String dir) {boolean flag = true; try {flag = ftpclient.makedirectory (dir); if (flag) {system.out.println ( "폴더 만들기" + dir + "success!"); } else {system.out.println ( "폴더 만들기" + dir + "실패!"); }} catch (예외 e) {e.printstacktrace (); } 반환 플래그; } / ** * 파일 다운로드 * * @param pathname ftp 서버 파일 디렉토리 * * @param filename 파일 이름 * * @param localpath 파일 경로 * * @return * / public boolean downloadfile (문자열 pathname, 문자열 파일 이름, 문자열 로컬 경로) {boolean flag = false; OutputStream os = null; try {system.out.println ( "시작 파일 시작"); initftpclient (); // ftp 디렉토리를 전환합니다. ftpclient.changeworkingDirectory (PathName); ftpfile [] ftpfiles = ftpclient.listfiles (); for (ftpfile 파일 : ftpfiles) {if (filename.equalsignorecase (file.getname ())) {file localfile = new File (localPath + "/" + file.getName ()); os = 새 FileOutputStream (localFile); ftpclient.retrieveFile (file.getName (), os); os.close (); }} ftpclient.logout (); flag = true; System.out.println ( "성공적으로 파일 다운로드"); } catch (예외 e) {System.out.println ( "다운로드 파일 실패"); e.printstacktrace (); } 마지막으로 {if (ftpclient.isconnected ()) {try {ftpclient.disconnect (); } catch (ioexception e) {e.printstacktrace (); }} if (null! = os) {try {os.close (); } catch (ioexception e) {e.printstacktrace (); }}} return 플래그; } / ** * 파일 삭제 * * @param pathname ftp server 디렉토리를 저장합니다 * * @param filename 삭제할 파일 이름 * * @return * / public boolean deletefile (String pathName, String Filename) {boolean flag = false; try {system.out.println ( "파일 삭제 시작"); initftpclient (); // ftp 디렉토리를 전환합니다. ftpclient.changeworkingDirectory (PathName); ftpclient.dele (filename); ftpclient.logout (); flag = true; System.out.println ( "파일 삭제"); } catch (예외 e) {System.out.println ( "파일 삭제 실패"); e.printstacktrace (); } 마지막으로 {if (ftpclient.isconnected ()) {try {ftpclient.disconnect (); } catch (ioexception e) {e.printstacktrace (); }} 반환 플래그; } public static void main (String [] args) {ftputils ftp = new ftputils (); //ftp.uploadfile("ftpfile/data ","123.docx ","e : //123.docx "); //ftp.downloadfile("ftpfile/data ","123.docx ","f : // "); ftp.deletefile ( "ftpfile/data", "123.docx"); System.out.println ( "OK"); }} 위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.