Commons net의 FTP
package org.migle.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
* FTP Client , <a href="http://commons.apache.org/net/">Jakarta Commons Net
* FTPClient</a> 、 , Jakarta Commons Net
* FTPClient {@link #getFtpclient()}
* org.apache.commons.net.ftp.FTPClient
* , org.apache.commons.net.ftp.FTPClient <a href =
* "http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html"
* > FTPClient API </a>
*
* @since V2.0
* @version V1.0 2010-2-26
* @author
*/
public class FTPClientUtil {
private static Log logger = LogFactory.getLog(FTPClient.class);
private FTPClient ftpclient;
public FTPClientUtil(String host, int port) throws Exception {
this(host, port, null, null);
}
public FTPClientUtil(String host, int port, String username, String password)
throws Exception {
ftpclient = new FTPClient();
try {
ftpclient.connect(host, port);
int reply = ftpclient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpclient.disconnect();
logger.fatal("FTP ");
throw new Exception("FTP ");
}
if (username != null) {
if (!ftpclient.login(username, password)) {
ftpclient.disconnect();
logger.fatal(" , ");
throw new Exception(" , ");
}
}
} catch (SocketException e) {
logger.fatal(" FTP ", e);
throw new Exception(e);
} catch (IOException e) {
logger.fatal(" FTP ", e);
throw new Exception(e);
}
}
/**
*
* @param path
* FTP
* @param input
*
* @throws IOException
*/
public boolean upload(String pathname, InputStream input)
throws IOException {
//
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
if (pathname.indexOf("/") != -1) {
String path = pathname.substring(0, pathname.lastIndexOf("/"));
mkdir(path);
}
return ftpclient.storeFile(new String(pathname.getBytes(), ftpclient.getControlEncoding()), input);
}
/**
* FTP pathname , localName
*
* @param pathname
* @param localName
* @return
* @throws Exception
*/
public boolean download(String pathname, String localName) throws Exception {
String filename = localName != null ? localName : pathname
.substring(pathname.lastIndexOf("/") + 1);
if (filename == null || filename.isEmpty()) {
return false;
}
//
ftpclient.enterLocalPassiveMode();
//
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
if (ftpclient.listFiles(new String(pathname.getBytes(),ftpclient.getControlEncoding())).length == 0) {
logger.fatal(" ");
throw new Exception(" ");
}
File tmp = new File(filename + "_tmp"); //
File file = new File(filename);
FileOutputStream output = null;
boolean flag;
try {
output = new FileOutputStream(tmp);
flag = ftpclient.retrieveFile(new String(pathname.getBytes(),ftpclient.getControlEncoding()), output);
output.close();
if (flag) {
// , 。
tmp.renameTo(file);
System.out.println(file.getAbsolutePath());
}
} catch (FileNotFoundException e) {
logger.fatal(" ", e);
throw new Exception(e);
} finally {
output.close();
}
return flag;
}
/**
* , : <code>
* getFtpclient().removeDirectory(String pathname)
* </code>
* {@link org.apache.commons.net.ftp.FTPClient FTPClient}
*
* @param pathname
* @return true, false( false)
* @throws IOException
*/
public boolean delete(String pathname) throws IOException {
return ftpclient.deleteFile(new String(pathname.getBytes(),ftpclient.getControlEncoding()));
}
/**
* pathname,"/"
*
* @param pathname
*
* @return true false
* @throws IOException
*/
public boolean changeWorkingDirectory(String pathname) throws IOException {
return ftpclient.changeWorkingDirectory(new String(pathname.getBytes(),ftpclient.getControlEncoding()));
}
/**
* @return {@link org.apache.commons.net.ftp.FTPClient FTPClient}
*/
public FTPClient getFtpclient() {
return this.ftpclient;
}
/**
* @param ftpclient
* {@link org.apache.commons.net.ftp.FTPClient FTPClient}
*/
public void setFtpclient(FTPClient ftpclient) {
this.ftpclient = ftpclient;
}
public void close() throws Exception {
ftpclient.disconnect();
}
/**
*
* @param pathname
* , , ("/" )
* @return true, false( false)
* @throws IOException
*/
public boolean mkdir(String pathname) throws IOException {
//ftpclient.setControlEncoding("ISO-8859-1");
// ,
return ftpclient.makeDirectory(new String(pathname.getBytes(),ftpclient.getControlEncoding()));
}
public static void main(String[] args) throws Exception {
// URI uri = new URI(
// "ftp://100.180.185.205/oracle_10201_database_win32/database/welcome.html");
// System.out.println(uri.toURL().getHost());
// System.out.println(uri.toURL().getPath());
// System.out.println(uri.toURL().getFile());
FTPClientUtil ftputil = new FTPClientUtil("100.180.185.205", 21, "username",
"password");
System.out.println(ftputil.mkdir(" "));
ftputil.close();
// ftputil.upload("drop.sql", new FileInputStream("c:/drop.sql"));
// ftputil.download("/drop.sql", "drop.sql");
// FTPClient ftp = ftputil.getFtpclient();
// FTPListParseEngine engine = ftp.initiateListParsing();
// System.out.println(ftp.listFiles().length);
// while (engine.hasNext()) {
// FTPFile[] files = engine.getNext(25); // "page size" you want
// }
// FTPClient ftpclient = ftputil.getFtpclient();
//
// FTPFile[] f = ftpclient.listFiles("/kk/aa/drop2.sql");
// System.out.println(f.length);
// System.out.println(ftpclient.makeDirectory("/tt/aa/bb"));
// System.out.println(ftpclient.getReplyString());
// ftpclient.changeWorkingDirectory("/tt/aa/bb");
//
// //System.out.println(ftpclient.deleteFile("/tt/aa/cc/"));
// System.out.println(ftpclient.getReplyString());
// ftpclient.changeWorkingDirectory("/");
// System.out.println(ftpclient.listFiles().length);
// System.out.println("a".lastIndexOf("/"));
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
프로덕션을 위한 SolrCloud 설정이 작업을 시작했을 때 SolrCloud에 익숙하지 않았습니다. 하지만 시간이 지나면서 요령이 생겼고 프로덕션 서버에서 SolrCloud를 설정하기 위한 최신 가이드를 작성하는 것이 좋은 생각이라고 생각합니다. 공식...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.