java基于Apache FTP實現(xiàn)文件上傳、下載、修改文件名、刪除
Apache FTP 是應(yīng)用比較廣泛的FTP上傳客戶端工具,它易于操作,代碼簡略,結(jié)構(gòu)清晰,是做FTP文件客戶端管理軟件的優(yōu)先之選。FTP的操作包括:FTP文件上傳(斷點續(xù)傳)、FTP文件下載、FTP文件重命名、FTP文件刪除,這些操作已經(jīng)將FTP應(yīng)用管理的方式發(fā)揮的淋漓盡致了,So 我一直都用此種方式來實現(xiàn)FTP文件服務(wù)器的管理工作;下附FTP工具代碼。
1、FTP文件操作狀態(tài)枚舉類
package com.scengine.wtms.utils.ftp;
public enum FTPStatus
{
File_Exits(0), Create_Directory_Success(1), Create_Directory_Fail(2), Upload_From_Break_Success(3), Upload_From_Break_Faild(4), Download_From_Break_Success(5), Download_From_Break_Faild(6), Upload_New_File_Success(7), Upload_New_File_Failed(8), Delete_Remote_Success(9), Delete_Remote_Faild(10),Remote_Bigger_Local(11),Remote_smaller_local(12),Not_Exist_File(13),Remote_Rename_Success(14),Remote_Rename_Faild(15),File_Not_Unique(16);
private int status;
public int getStatus()
{
return status;
}
public void setStatus(int status)
{
this.status = status;
}
FTPStatus(int status)
{
this.status = status;
}
}
2、FTP文件操作工具代碼
package com.scengine.wtms.utils.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import com.scengine.wtms.utils.Log;
public class FTPUtils
{
private FTPClient ftpClient = new FTPClient();
/**
* 對象構(gòu)造 設(shè)置將過程中使用到的命令輸出到控制臺
*/
public FTPUtils()
{
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}
/**
*
* java編程中用于連接到FTP服務(wù)器
*
* @param hostname
* 主機名
*
* @param port
* 端口
*
* @param username
* 用戶名
*
* @param password
* 密碼
*
* @return 是否連接成功
*
* @throws IOException
*/
public boolean connect(String hostname, int port, String username, String password) throws IOException
{
ftpClient.connect(hostname, port);
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
if (ftpClient.login(username, password))
{
return true;
}
}
disconnect();
return false;
}
/**
* 刪除遠(yuǎn)程FTP文件
*
* @param remote
* 遠(yuǎn)程文件路徑
* @return
* @throws IOException
*/
public FTPStatus delete(String remote) throws IOException
{
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPStatus result = null;
FTPFile[] files = ftpClient.listFiles(remote);
if (files.length == 1)
{
boolean status = ftpClient.deleteFile(remote);
result = status ? FTPStatus.Delete_Remote_Success : FTPStatus.Delete_Remote_Faild;
}
else
{
result = FTPStatus.Not_Exist_File;
}
Log.getLogger(this.getClass()).info("FTP服務(wù)器文件刪除標(biāo)識:"+result);
return result;
}
/**
* 重命名遠(yuǎn)程FTP文件
*
* @param name
* 新遠(yuǎn)程文件名稱(路徑-必須保證在同一路徑下)
*
* @param remote
* 遠(yuǎn)程文件路徑
*
* @return 是否成功
*
* @throws IOException
*/
public FTPStatus rename(String name,String remote) throws IOException
{
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPStatus result = null;
FTPFile[] files = ftpClient.listFiles(remote);
if (files.length == 1)
{
boolean status = ftpClient.rename(remote, name);
result = status ? FTPStatus.Remote_Rename_Success : FTPStatus.Remote_Rename_Faild;
}
else
{
result = FTPStatus.Not_Exist_File;
}
Log.getLogger(this.getClass()).info("FTP服務(wù)器文件名更新標(biāo)識:"+result);
return result;
}
/**
*
* 從FTP服務(wù)器上下載文件
*
* @param fileName
* 下載文件的名字(包括后綴名)
*
* @param remote
* 遠(yuǎn)程文件路徑
*
* @param local
* 本地文件路徑
*
* @return 是否成功
*
* @throws IOException
*/
public FTPStatus download(String fileName,String remote,HttpServletResponse response) throws IOException
{
// 開啟輸出流彈出文件保存路徑選擇窗口
response.setContentType("application/octet-stream");
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +fileName);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPStatus result;
OutputStream out = response.getOutputStream();
boolean status = ftpClient.retrieveFile(remote, out);
result=status?FTPStatus.Download_From_Break_Success:FTPStatus.Download_From_Break_Faild;
Log.getLogger(this.getClass()).info("FTP服務(wù)器文件下載標(biāo)識:"+result);
out.close();
return result;
}
/**
*
* 從FTP服務(wù)器上下載文件
*
* @param remote
* 遠(yuǎn)程文件路徑
*
* @param local
* 本地文件路徑
*
* @return 是否成功
*
* @throws IOException
*/
@SuppressWarnings("resource")
public FTPStatus download(String remote, String local) throws IOException
{
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPStatus result;
File f = new File(local);
FTPFile[] files = ftpClient.listFiles(remote);
if (files.length != 1)
{
Log.getLogger(this.getClass()).info("遠(yuǎn)程文件不唯一");
return FTPStatus.File_Not_Unique;
}
long lRemoteSize = files[0].getSize();
if (f.exists())
{
OutputStream out = new FileOutputStream(f, true);
Log.getLogger(this.getClass()).info("本地文件大小為:" + f.length());
if (f.length() >= lRemoteSize)
{
Log.getLogger(this.getClass()).info("本地文件大小大于遠(yuǎn)程文件大小,下載中止");
return FTPStatus.Remote_smaller_local;
}
ftpClient.setRestartOffset(f.length());
boolean status = ftpClient.retrieveFile(remote, out);
result=status?FTPStatus.Download_From_Break_Success:FTPStatus.Download_From_Break_Faild;
out.close();
} else
{
OutputStream out = new FileOutputStream(f);
boolean status = ftpClient.retrieveFile(remote, out);
result=status?FTPStatus.Download_From_Break_Success:FTPStatus.Download_From_Break_Faild;
out.close();
}
return result;
}
/**
*
* 上傳文件到FTP服務(wù)器,支持?jǐn)帱c續(xù)傳
*
* @param local
* 本地文件名稱,絕對路徑
*
* @param remote
* 遠(yuǎn)程文件路徑,使用/home/directory1/subdirectory/file.ext
* 按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創(chuàng)建不存在的目錄結(jié)構(gòu)
*
* @return 上傳結(jié)果
*
* @throws IOException
*/
@SuppressWarnings("resource")
public FTPStatus upload(String local, String remote) throws IOException
{
// 設(shè)置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
// 設(shè)置以二進(jìn)制流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPStatus result;
// 對遠(yuǎn)程目錄的處理
String remoteFileName = remote;
if (remote.contains("/"))
{
remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);
String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(directory))
{
// 如果遠(yuǎn)程目錄不存在,則遞歸創(chuàng)建遠(yuǎn)程服務(wù)器目錄
int start = 0;
int end = 0;
if (directory.startsWith("/"))
{
start = 1;
} else
{
start = 0;
}
end = directory.indexOf("/", start);
while (true)
{
String subDirectory = remote.substring(start, end);
if (!ftpClient.changeWorkingDirectory(subDirectory))
{
if (ftpClient.makeDirectory(subDirectory))
{
ftpClient.changeWorkingDirectory(subDirectory);
} else
{
Log.getLogger(this.getClass()).info("創(chuàng)建目錄失敗");
return FTPStatus.Create_Directory_Fail;
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 檢查所有目錄是否創(chuàng)建完畢
if (end <= start)
{
break;
}
}
}
}
// 檢查遠(yuǎn)程是否存在文件
FTPFile[] files = ftpClient.listFiles(remoteFileName);
if (files.length == 1)
{
long remoteSize = files[0].getSize();
File f = new File(local);
long localSize = f.length();
if (remoteSize == localSize)
{
return FTPStatus.File_Exits;
} else if (remoteSize > localSize)
{
return FTPStatus.Remote_Bigger_Local;
}
// 嘗試移動文件內(nèi)讀取指針,實現(xiàn)斷點續(xù)傳
InputStream is = new FileInputStream(f);
if (is.skip(remoteSize) == remoteSize)
{
ftpClient.setRestartOffset(remoteSize);
if (ftpClient.storeFile(remote, is))
{
return FTPStatus.Upload_From_Break_Success;
}
}
// 如果斷點續(xù)傳沒有成功,則刪除服務(wù)器上文件,重新上傳
if (!ftpClient.deleteFile(remoteFileName))
{
return FTPStatus.Delete_Remote_Faild;
}
is = new FileInputStream(f);
if (ftpClient.storeFile(remote, is))
{
result = FTPStatus.Upload_New_File_Success;
} else
{
result = FTPStatus.Upload_New_File_Failed;
}
is.close();
} else
{
InputStream is = new FileInputStream(local);
if (ftpClient.storeFile(remoteFileName, is))
{
result = FTPStatus.Upload_New_File_Success;
} else
{
result = FTPStatus.Upload_New_File_Failed;
}
is.close();
}
return result;
}
/**
*
* 斷開與遠(yuǎn)程服務(wù)器的連接
*
* @throws IOException
*/
public void disconnect() throws IOException
{
if (ftpClient.isConnected())
{
ftpClient.disconnect();
}
}
public static void main(String[] args)
{
FTPUtils myFtp = new FTPUtils();
try
{
myFtp.connect("192.168.1.200", 21, "duser", "HTPDuserXP32");
Log.getLogger(FTPUtils.class).info(myFtp.upload("C:\\Users\\Administrator\\Desktop\\swing.drawer.jar", "/jars/swing.drawer.jar"));
myFtp.disconnect();
} catch (IOException e)
{
Log.getLogger(FTPUtils.class).info("FTP上傳文件異常:" + e.getMessage());
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java設(shè)計模式七大原則之單一職責(zé)原則詳解
單一職責(zé)原則(Single Responsibility Principle, SRP),有且僅有一個原因引起類的變更。簡單來說,就是針對一個java類,它應(yīng)該只負(fù)責(zé)一項職責(zé)。本文將詳細(xì)介紹一下Java設(shè)計模式七大原則之一的單一職責(zé)原則,需要的可以參考一下2022-02-02
自從在 IDEA 中用了熱部署神器 JRebel 之后,開發(fā)效率提升了 10(真棒)
在javaweb開發(fā)過程中,使用熱部署神器 JRebel可以使class類還是更新spring配置文件都能立馬見到效率,本文給大家介紹JRebel的兩種安裝方法,小編建議使用第二種方法,具體安裝步驟跟隨小編一起看看吧2021-06-06
Java鏈表的天然遞歸結(jié)構(gòu)性質(zhì)圖文與實例分析
這篇文章主要介紹了Java鏈表的天然遞歸結(jié)構(gòu)性質(zhì),結(jié)合圖文與實例形式分析了java鏈表中遞歸操作的原理、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2020-03-03
Spring Boot配置Swagger的實現(xiàn)代碼
這篇文章主要介紹了Spring Boot配置Swagger的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
一篇文章教你如何在SpringCloud項目中使用OpenFeign
這篇文章主要介紹了SpringCloud 使用Open feign 優(yōu)化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-08-08

