내가 과거에했던 프로젝트는 파일을 FTP 서버에 업로드했으며 이제 검토합니다. 비교적 간단하며 한 번에 이해할 수 있습니다.
환경 : 먼저 FTP 서버를 먼저 설치하십시오. IIS와 함께 Win8에서 로컬로 구성했습니다. 바이두에서 설치 문서를 찾을 수 있습니다.
1. 프로젝트 디렉토리에서 FTP 구성 파일 생성, 디렉토리는 아래와 같습니다.
01 ftpconfig.properties :
FTPIP = 10.73.222.29
ftpport = 21
ftpuser = wp
ftppwd = 04143114WP
ftpremotepath = d : // 공유
02 ftpconfig.properties의 특정 컨텐츠를 읽는 클래스 :
package com.java.core.util; import java.io.ioexception; import java.io.inputStream; import java.util.properties;/*** @author wangpei* @version 생성 : 2017 년 5 월 6 일 9:42:40 pm at 9:42:40 pm ftp 파일의 구성 파일을 읽으십시오*/public classftptptpperties {private inputtptperties} 개인 속성 속성; public readftpproperties () {is = this.getclass (). getResourCeasStream ( "/ftpconfig.properties"); // 입력 스트림 속성으로 구성 파일을 읽습니다. = new Properties (); try {properties.load (is); } catch (ioException e) {System.out.println ( "구성 파일이 존재하지 않습니다 .."); e.printstacktrace (); } 마침내 {if (null! = is) {try {is.close (); } catch (ioException e) {System.out.println ( "폐쇄 스트림 실패 .."); e.printstacktrace (); }}}} public string getip () {// ftp 서버의 IP 주소 반환 속성을 가져옵니다. } public String getPort () {// FTP 서버의 반환 속성 포트를 가져옵니다 .getProperty ( "ftpport"); } public String getUser () {// ftp 로그인 username return reporties.getProperty ( "ftpuser"); } public String getPwd () {// ftp 서버의 return properties.getProperty ( "ftppwd")의 로그인 비밀번호 가져옵니다. } public String getRemotePath () {// 파일이 FTP 서버에 저장된 디렉토리를 가져옵니다. }}03 파일 업로드 및 다운로드의 인터페이스 클래스
package com.java.web.service; import java.io.inputStream; import org.apache.commons.net.ftpclient; import com.java.core.util.readftpproperties;/** * @author wangpei * @version 생성 : 2017 년 5 월 6:39:03 PM * 파일 포워드로드 및 다운로드. 인터페이스 ftpservice { / * * ftp * / public boolean loginftp (ftpclient client, readftpproperties rfp)에 로그인; / * * ftp */ public boolean logout (ftpclient client); /// * * FTP의 이름을 inputStream */ public boolean uploadfile (ftpclient client, string remotepath, string filenewname, inputstream inputstream, readftppropstries rfp)에 업로드합니다. / * * 디렉토리에서 파일 이름을 다운로드하여 remotepath */ public inputStream downfileByftp (ftpclient client, String Remotepath, String filename); /** ftp*/ public boolean delfile (ftpclient client, string pathname)에서 디렉토리 PathName으로 파일을 삭제합니다.04 파일 업로드 및 다운로드를위한 인터페이스 구현 클래스
package com.java.web.service.serviceimpl; import java.io.ioexcept; import java.io.inputStream; import java.io.unsupportedencodingException; import java.net.socketexception; import org.apache.commons.net.ftp.ftp; import org.apache.commons.net.net.net.net org.apache.commons.net.ftp.ftpfile; import com.java.core.util.readftpproperties; import com.java.web.service.ftpservice;/*** @author wangpei* @version 생성 시간 : 10:02:28 PM 10:02:28 PM 10:028 Boolean loginftp (ftpclient client, readftpproperties rfp) {string ftpip = rfp.getip (); 문자열 ftpport = rfp.getport (); 문자열 ftpuser = rfp.getUser (); 문자열 ftppwd = rfp.getpwd (); // 문자열 fgtpremotepath = rfp.getRemotepath (); 부울 b = 거짓; try {client.connect (ftpip, integer.parseint (ftpport)); } catch (numberformatexception e) {system.out.println ( "ftp에 연결할 수 없음"); 거짓을 반환합니다. } catch (pocketexception e) {System.out.println ( "ftp에 연결할 수 없음"); 거짓을 반환합니다. } catch (ioexception e) {System.out.println ( "ftp에 연결할 수 없음"); 거짓을 반환합니다. } catch (ioexception e) {System.out.println ( "ftp에 연결할 수 없음"); 거짓을 반환합니다. } client.setControlEncoding ( "uft-8"); try {b = client.login (ftpuser, ftppwd); } catch (ioException e) {System.out.println ( "로그인 ftp 오류"); 로그 아웃 (클라이언트); // 로그 아웃/분리 FTP 서버 링크 return false; } 반환 b; } public boolean logout (ftpclient client) {boolean b = false; try {b = client.logout (); // logout client.disconnect (); // disternect} catch (ioexception e) {return false; } 반환 b; } public boolean uploadfile (ftpclient 클라이언트, 문자열 remotepath, 문자열 filenewname, inputStream inputStream, readftpproperties rfp) {boolean b = false; {client.setfileType (ftpclient.binary_file_type); client.enterlocalPassiveMode (); if (remotepath! = null &&! "". for (string onepath : paths) {if (onepath == null || "".equals (onepath.trim ()) {계속; } onepath = new String (onepath.getBytes ( "utf-8"), "iso-8859-1"); System.out.println ( "onepath =" + onepath); if (! client.changeworkingdirectory (onepath)) {client.makedirectory (onepath); // ftp 서버 디렉토리 client.changeworkingdirectory (onepath); // ftp 서버 디렉토리 변경} else {system.out.println ( "파일 단일 경로"); }}} b = client.storeFile (new String (filenewName.getBytes ( "UTF-8"), "ISO-8859-1"), inputStream); } catch (UnsupportedEncodingException e) {return false; } catch (ioexception e) {return false; } 반환 b; } public inputStream downfileByftp (ftpclient ftpclient, 문자열 remotepath, String filename) {ftpfile [] fs; 입력 스트림은 = null입니다. {// 수동 모드 설정 ftpclient.enterlocalpassiveMode (); // set ftpclient.setFileType (ftp.binary_file_type); // 편집 형식 설정 ftpclient.setControlenCoding ( "UTF-8"); remotepath = remotepath.substring (0, remotepath.lastindexof (filename)); fs = ftpclient.listfiles (remotepath); // (ftpfile ff : fs) {if (ff.getname (). equals (filename)) {// 대상 파일 찾기는 = ftpclient.retrieveFilestream ((remotepath + filename)). "ISO-8859-1"); 부서지다; }}} catch (ioexception e) {e.printstacktrace (); } 반환은; } public boolean delfile (ftpclient ftpclient, String PathName) {boolean b = false; try {b = ftpclient.deletefile (pathname); 반환 b; } catch (예외 e) {return false; } 마지막으로 {logout (ftpclient); // 종료/FTP 서버 링크}}}}}코드는 이해하기 쉽고 한 번 읽음으로써 이해해야합니다. 여기에서 구체적으로 분석하지 않으며 주로 코드의 주석을 살펴 봅니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.