Java利用apache ftp工具實(shí)現(xiàn)文件上傳下載和刪除功能
利用apache ftp工具實(shí)現(xiàn)文件的上傳下載和刪除,具體如下
1、下載相應(yīng)的jar包
commons-net-1.4.1.jar
2、實(shí)現(xiàn)代碼如下:
public class FtpUtils {
//ftp服務(wù)器地址
public String hostname = "192.168.1.249";
//ftp服務(wù)器端口號(hào)默認(rèn)為21
public Integer port = 21 ;
//ftp登錄賬號(hào)
public String username = "root";
//ftp登錄密碼
public String password = "123";
public FTPClient ftpClient = null;
/**
* 初始化ftp服務(wù)器
*/
public void initFtpClient() {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
try {
System.out.println("connecting...ftp服務(wù)器:"+this.hostname+":"+this.port);
ftpClient.connect(hostname, port); //連接ftp服務(wù)器
ftpClient.login(username, password); //登錄ftp服務(wù)器
int replyCode = ftpClient.getReplyCode(); //是否成功登錄服務(wù)器
if(!FTPReply.isPositiveCompletion(replyCode)){
System.out.println("connect failed...ftp服務(wù)器:"+this.hostname+":"+this.port);
}
System.out.println("connect successfu...ftp服務(wù)器:"+this.hostname+":"+this.port);
}catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上傳文件
* @param pathname ftp服務(wù)保存地址
* @param fileName 上傳到ftp的文件名
* @param originfilename 待上傳文件的名稱(絕對(duì)地址) *
* @return
*/
public boolean uploadFile( String pathname, String fileName,String originfilename){
boolean flag = false;
InputStream inputStream = null;
try{
System.out.println("開始上傳文件");
inputStream = new FileInputStream(new File(originfilename));
initFtpClient();
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
flag = true;
System.out.println("上傳文件成功");
}catch (Exception e) {
System.out.println("上傳文件失敗");
e.printStackTrace();
}finally{
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服務(wù)保存地址
* @param fileName 上傳到ftp的文件名
* @param inputStream 輸入文件流
* @return
*/
public boolean uploadFile( String pathname, String fileName,InputStream inputStream){
boolean flag = false;
try{
System.out.println("開始上傳文件");
initFtpClient();
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
flag = true;
System.out.println("上傳文件成功");
}catch (Exception e) {
System.out.println("上傳文件失敗");
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
//改變目錄路徑
public boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
System.out.println("進(jìn)入文件夾" + directory + " 成功!");
} else {
System.out.println("進(jìn)入文件夾" + directory + " 失?。¢_始創(chuàng)建文件夾");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
//創(chuàng)建多層目錄文件,如果有ftp服務(wù)器已存在該文件,則不創(chuàng)建,如果無,則創(chuàng)建
public boolean CreateDirecroty(String remote) throws IOException {
boolean success = true;
String directory = remote + "/";
// 如果遠(yuǎn)程目錄不存在,則遞歸創(chuàng)建遠(yuǎn)程服務(wù)器目錄
if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
String path = "";
String paths = "";
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
System.out.println("創(chuàng)建目錄[" + subDirectory + "]失敗");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}
paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
// 檢查所有目錄是否創(chuàng)建完畢
if (end <= start) {
break;
}
}
}
return success;
}
//判斷ftp服務(wù)器文件是否存在
public boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
//創(chuàng)建目錄
public boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = ftpClient.makeDirectory(dir);
if (flag) {
System.out.println("創(chuàng)建文件夾" + dir + " 成功!");
} else {
System.out.println("創(chuàng)建文件夾" + dir + " 失?。?);
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/** * 下載文件 *
* @param pathname FTP服務(wù)器文件目錄 *
* @param filename 文件名稱 *
* @param localpath 下載后的文件路徑 *
* @return */
public boolean downloadFile(String pathname, String filename, String localpath){
boolean flag = false;
OutputStream os=null;
try {
System.out.println("開始下載文件");
initFtpClient();
//切換FTP目錄
ftpClient.changeWorkingDirectory(pathname);
FTPFile[] ftpFiles = ftpClient.listFiles();
for(FTPFile file : ftpFiles){
if(filename.equalsIgnoreCase(file.getName())){
File localFile = new File(localpath + "/" + file.getName());
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
ftpClient.logout();
flag = true;
System.out.println("下載文件成功");
} catch (Exception e) {
System.out.println("下載文件失敗");
e.printStackTrace();
} finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
/** * 刪除文件 *
* @param pathname FTP服務(wù)器保存目錄 *
* @param filename 要?jiǎng)h除的文件名稱 *
* @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 (Exception e) {
System.out.println("刪除文件失敗");
e.printStackTrace();
} finally {
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
return flag;
}
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");
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Apache Calcite進(jìn)行SQL解析(java代碼實(shí)例)
- Java使用Apache.POI中HSSFWorkbook導(dǎo)出到Excel的實(shí)現(xiàn)方法
- java操作Apache druid的實(shí)例代碼
- 淺析Java中Apache BeanUtils和Spring BeanUtils的用法
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer異常
- Java常用類庫Apache Commons工具類說明及使用實(shí)例詳解
- Java?Apache?common-pool對(duì)象池介紹
相關(guān)文章
mybatis的insert語句插入數(shù)據(jù)時(shí)的返回值的實(shí)現(xiàn)
這篇文章主要介紹了mybatis的insert語句插入數(shù)據(jù)時(shí)的返回值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java后端實(shí)現(xiàn)生成驗(yàn)證碼圖片的示例代碼
驗(yàn)證碼是一種用于驗(yàn)證用戶身份或確保用戶操作安全的技術(shù)手段,通常以圖形、聲音或文字的形式出現(xiàn),本文主要介紹了如何通過java實(shí)現(xiàn)生成驗(yàn)證碼圖片,需要的可以參考下2023-12-12
JavaMail發(fā)送(帶圖片和附件)和接收郵件實(shí)現(xiàn)詳解(四)
這篇文章主要為大家詳細(xì)介紹了JavaMail帶圖片和附件的發(fā)送和接收郵件實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
IntelliJ IDEA中使用mybatis-generator的示例
這篇文章主要介紹了IntelliJ IDEA中使用mybatis-generator,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
springboot中RestTemplate發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)示例
RestTemplate是一個(gè) spring-web 提供的執(zhí)行HTTP請(qǐng)求的同步阻塞式工具類,本文就來介紹一下RestTemplate發(fā)送HTTP請(qǐng)求,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03

