java編寫(xiě)ftp下載工具
需要用到 java 寫(xiě)一個(gè) ftp 的工具,因?yàn)橹挥幸稽c(diǎn)點(diǎn) java 基礎(chǔ),但是由于好幾年不用,幾乎算是不會(huì)了,只好一點(diǎn)點(diǎn)來(lái)搞,還好能撿起來(lái)。
不過(guò)因?yàn)槭窃?Linux 下使用 javac 編譯,不是在 WIN 下使用 IDE 來(lái)做這些事情,所以在運(yùn)行和編譯上又費(fèi)了一些時(shí)間,不過(guò)正是因?yàn)檫@樣對(duì) JAVA 的一些編譯、運(yùn)行的知識(shí)又了解了一些。
對(duì)于 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 java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpClient {
private String host;
private int port;
private String username;
private String password;
private boolean binaryTransfer = true;
private boolean passiveMode = true;
private String encoding = "UTF-8";
private int clientTimeout = 3000;
private boolean flag=true;
private FTPClient ftpClient = null;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isBinaryTransfer() {
return binaryTransfer;
}
public void setBinaryTransfer(boolean binaryTransfer) {
this.binaryTransfer = binaryTransfer;
}
public boolean isPassiveMode() {
return passiveMode;
}
public void setPassiveMode(boolean passiveMode) {
this.passiveMode = passiveMode;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public int getClientTimeout() {
return clientTimeout;
}
public void setClientTimeout(int clientTimeout) {
this.clientTimeout = clientTimeout;
}
public FtpClient(String Host) {
this.username = "anonymous";
this.encoding = "utf-8";
this.binaryTransfer = true;
this.binaryTransfer = true;
this.port = 21;
this.host = Host;
try {
this.ftpClient = getFTPClient();
} catch (Exception e) {
System.out.println("Create FTPClient error!");
}
}
private FTPClient getFTPClient() throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding(encoding);
connect(ftpClient);
if (passiveMode) {
ftpClient.enterLocalPassiveMode();
}
setFileType(ftpClient);
try {
ftpClient.setSoTimeout(clientTimeout);
} catch (SocketException e) {
throw new IOException("Set timeout error.", e);
}
return ftpClient;
}
private void setFileType(FTPClient ftpClient) throws IOException {
try {
if (binaryTransfer) {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
} catch (IOException e) {
throw new IOException("Could not to set file type.", e);
}
}
public boolean connect(FTPClient ftpClient) throws IOException {
try {
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftpClient.login(username, password)) {
setFileType(ftpClient);
return true;
}
} else {
this.ftpClient.disconnect();
throw new IOException("FTP server refused connection.");
}
} catch (IOException e) {
if (this.ftpClient.isConnected()) {
try {
this.ftpClient.disconnect();
} catch (IOException e1) {
throw new IOException("Could not disconnect from server.", e);
}
}
throw new IOException("Could not connect to server.", e);
}
return false;
}
private void disconnect() throws IOException {
try {
this.ftpClient.logout();
} catch (IOException e) {
System.out.println("logout may timeout!");
} finally {
if (this.ftpClient.isConnected()) {
this.ftpClient.disconnect();
}
}
}
public InputStream getStream(String serverFile) throws IOException {
InputStream inStream = null;
try {
inStream = this.ftpClient.retrieveFileStream(serverFile);
System.out.println("inStream get over!");
return inStream;
} catch (IOException e) {
System.out.println("get stream exception");
return null;
}
}
public boolean writeStream(InputStream input, String localFile) throws IOException {
FileOutputStream fout = new FileOutputStream(localFile);
int ch = 0;
if(input == null){
System.out.println("input is null");
return false;
}
try {
ch = input.read();
while(ch != -1){
fout.write(ch);
ch = input.read();
}
System.out.println("write over!");
return flag;
} catch (IOException e) {
throw new IOException("Couldn't get file from server.", e);
}
}
public boolean isExist(String remoteFilePath)throws IOException{
try{
File file=new File(remoteFilePath);
String remotePath=remoteFilePath.substring(0,(remoteFilePath.indexOf(file.getName())-1));
String[] listNames = this.ftpClient.listNames(remotePath);
System.out.println(remoteFilePath);
for(int i=0;i<listNames.length;i++){
System.out.println(listNames[i]);
if(remoteFilePath.equals(listNames[i])){
flag=true;
System.out.println("file:"+file.getName()+" existed");
break;
}else {
flag=false;
}
}
} catch (IOException e) {
throw new IOException("FILE EXCEPTION", e);
}
return flag;
}
//main for testing
public static void main(String[] args) throws IOException {
String hostname = "cp01-testing-ps7130.cp01.baidu.com";
String serverFile="/home/work/check_disk.sh";
String localFile="/home/work/workspace/project/dhc2-0/dhc/base/ftp/task_get";
FtpClient ftp = new FtpClient(hostname);
System.out.println(ftp.isExist(serverFile));
ftp.writeStream(ftp.getStream(serverFile), localFile);
ftp.disconnect();
}
}
這個(gè)工具是為了配合另外一個(gè) Hadoop 工具做 集群上傳用的,所以里面的把 input 和 output 流分開(kāi)了,也是為了方便另外一個(gè)工具使用。
補(bǔ)充一點(diǎn),如何在 linux 配置運(yùn)行:
如果這樣的代碼需要在 linux 下環(huán)境運(yùn)行,首先要配置好響應(yīng)的包,例如
import org.apache.commons.net.ftp.FTPClient;
這個(gè)包在 apache 的網(wǎng)站上直接下載就行,解壓后找到對(duì)應(yīng)的 jar 包,在編譯的時(shí)候進(jìn)行引用:
export FTPPATH="${路徑}/xxx.jar"
javac -classpath $CLASSPATH:$FTPPATH FtpClient.java
同樣,在運(yùn)行的時(shí)候也要指定 classpath:
java -classpath $CLASSPATH:$FTPPATH FtpClient
建議不要把$FTPPATH 包含在 CLASSPATH 中,用什么包就引用什么環(huán)境變量就行了,沒(méi)必要一股腦都添加進(jìn)去,就像我們沒(méi)必要 import 所有的包一樣。
以上所述就是本文的全部?jī)?nèi)容了,希望能夠?qū)Υ蠹覍W(xué)習(xí)java有所幫助。
請(qǐng)您花一點(diǎn)時(shí)間將文章分享給您的朋友或者留下評(píng)論。我們將會(huì)由衷感謝您的支持!
相關(guān)文章
springboot?@PostConstruct無(wú)效的解決
這篇文章主要介紹了springboot?@PostConstruct無(wú)效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11IDEA插件Statistic統(tǒng)計(jì)代碼快速分辨爛項(xiàng)目
這篇文章主要為大家介紹了使用IDEA插件Statistic來(lái)統(tǒng)計(jì)項(xiàng)目代碼,幫助大家快速識(shí)別出爛項(xiàng)目,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01基于Java實(shí)現(xiàn)的Dijkstra算法示例
這篇文章主要介紹了基于Java實(shí)現(xiàn)的Dijkstra算法示例,一個(gè)比較典型的算法示例,需要的朋友可以參考下2014-07-07Java 將Excel轉(zhuǎn)為OFD格式(方法步驟)
OFD是一種開(kāi)放版式文檔是我國(guó)國(guó)家版式文檔格式標(biāo)準(zhǔn),本文通過(guò)Java后端程序代碼展示如何將Excel轉(zhuǎn)為OFD格式,分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2021-12-12Spring Cloud Feign接口返回流的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Feign接口返回流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java的System.getProperty()方法獲取大全
這篇文章主要介紹了Java的System.getProperty()方法獲取大全,羅列了System.getProperty()方法獲取各類信息的用法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12優(yōu)化spring?boot應(yīng)用后6s內(nèi)啟動(dòng)內(nèi)存減半
這篇文章主要為大家介紹了優(yōu)化spring?boot后應(yīng)用6s內(nèi)啟動(dòng)內(nèi)存減半的優(yōu)化示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02