java中ftp删除文件,Java 实现ftp 文件上传、下载和删除
實現FTP相關功能
1、下載相應的jar包
commons-net-3.6.jar
2、代碼實現
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 org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* FTP 操作類
* @author d
* @version 1.0
* 2019.12.12
*/
public class FtpUtil {
public static void main(String[] args) {
FtpUtil ftpUtil = new FtpUtil();
ftpUtil.downLoadFile("/", "", "D:/test");
// ftpUtil.uploadFile("/1", "1.zip", "D:/test/1.zip");
// ftpUtil.deleteFile("/1", "1.zip");
}
/**
* Ftp 服務器地址
**/
public String hostname = "127.0.0.1";
/**
* Ftp 端口號
**/
public int port = 21;
/**
* Ftp 登錄賬號
**/
public String username = "anonymous";
/**
* Ftp 登錄密碼
**/
public String password = null;
/**
* 設置緩沖區大小4M
**/
private static final int BUFFER_SIZE = 1024 * 1024 * 4;
/**
* Ftp 操作對象
**/
public FTPClient ftpClient = null;
/**
* 連接FTP服務器
*
* @param address 地址
* @param port 端口
* @param username 用戶名
* @param password 密碼
*/
private void login() {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
try {
ftpClient.connect(hostname, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//文件類型為二進制文件
ftpClient.setBufferSize(BUFFER_SIZE);//限制緩沖區大小
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeConnect();
System.out.println("FTP服務器連接失敗");
}
} catch (Exception e) {
System.out.println("FTP登錄失敗" + e.getMessage());
}
}
/**
* 關閉FTP連接
*/
private void closeConnect() {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
System.out.println("關閉FTP連接失敗" + e.getMessage());
}
}
}
/**
* 下載文件
* @param ftpPath FTP文件目錄
* @param fileName 需下載的文件名
* @param savePath 下載后的文件路徑
* @return 返回是否下載成功 true
*/
public Boolean downLoadFile(String ftpPath, String fileName, String savePath){
login();
OutputStream os = null;
if (ftpClient != null) {
try {
//判斷是否存在該目錄
if (!ftpClient.changeWorkingDirectory(ftpPath)) {
System.out.println("/" + ftpPath + "該目錄不存在");
return false;
}
ftpClient.enterLocalPassiveMode();//設置被動模式,開通一個端口來傳輸數據
FTPFile[] ftpFiles = ftpClient.listFiles();
// 判斷該目錄下是否有文件
if (ftpFiles == null || ftpFiles.length == 0) {
System.out.println("/" + ftpPath + "該目錄下無文件");
return false;
}
for(FTPFile file : ftpFiles){
if(fileName.equals("") || fileName.equalsIgnoreCase(file.getName())){//文件名稱為"",下載指定文件
if(!file.isDirectory()){//是否文件夾
File saveFile = new File(savePath + "/" + file.getName());
os = new FileOutputStream(saveFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
}
return true;
} catch (IOException e) {
System.out.println("下載文件失敗" + e.getMessage());
} finally {
if(null != os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
closeConnect();
}
}
return false;
}
/**
* 上傳文件
* @param savePath FTP保存目錄
* @param fileName 上傳到FTP的文件名
* @param filePath 待上傳文件的名稱(絕對地址)
* @return
*/
public boolean uploadFile(String savePath, String fileName,String filePath){
login();
boolean flag = false;
InputStream inputStream = null;
if (ftpClient != null) {
try{
inputStream = new FileInputStream(new File(filePath));
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.makeDirectory(savePath);
ftpClient.changeWorkingDirectory(savePath);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
flag = true;
}catch (Exception e) {
e.printStackTrace();
}finally{
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
closeConnect();
}
}
return flag;
}
/** * 刪除文件 *
* @param pathname FTP服務器保存目錄
* @param filename 要刪除的文件名稱
* @return */
public boolean deleteFile(String filePath, String filename){
login();
boolean flag = false;
if (ftpClient != null) {
try {
ftpClient.changeWorkingDirectory(filePath);
ftpClient.dele(filename);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnect();
}
}
return flag;
}
}
package com.miapsoft.sample.common;
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 org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;
/**?* FTP 操作類?* @author jyyr?* @version 1.0?* 2019.12.12?*/public class FtpUtil {? ??? ? public static void main(String[] args) {? ? FtpUtil ftpUtil = new FtpUtil();? ? ftpUtil.downLoadFile("/", "", "D:/test");//? ? ftpUtil.uploadFile("/1", "1.zip", "D:/test/1.zip");//? ? ftpUtil.deleteFile("/1", "1.zip");? ? }? ??? ? /**? ? ?* Ftp 服務器地址? ? ?**/? ? public String hostname = "10.129.240.158";? ? /**? ? ?* Ftp 端口號? ? ?**/? ? public int port = 21;? ? /**? ? ?* Ftp 登錄賬號? ? ?**/? ? public String username = "anonymous";? ? /**? ? ?* Ftp 登錄密碼? ? ?**/? ? public String password = null;? ? /**? ? ?* 設置緩沖區大小4M? ? ?**/? ? private static final int BUFFER_SIZE = 1024 * 1024 * 4;? ? /**? ? ?* Ftp 操作對象? ? ?**/? ? public FTPClient ftpClient = null;
/**? ? ?* 連接FTP服務器? ? ?*? ? ?* @param address? 地址? ? ?* @param port? ? ?端口? ? ?* @param username 用戶名? ? ?* @param password 密碼? ? ?*/? ? private void login() {? ? ? ? ftpClient = new FTPClient();? ? ? ? ftpClient.setControlEncoding("utf-8");? ? ? ? try {? ? ? ? ? ? ftpClient.connect(hostname, port);? ? ? ? ? ? ftpClient.login(username, password);? ? ? ? ? ? ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//文件類型為二進制文件? ? ? ? ? ? ftpClient.setBufferSize(BUFFER_SIZE);//限制緩沖區大小? ? ? ? ? ? int reply = ftpClient.getReplyCode();? ? ? ? ? ? if (!FTPReply.isPositiveCompletion(reply)) {? ? ? ? ? ? ? ? closeConnect();? ? ? ? ? ? ? ? System.out.println("FTP服務器連接失敗");? ? ? ? ? ? }? ? ? ? } catch (Exception e) {? ? ? ? ? ? System.out.println("FTP登錄失敗" + e.getMessage());? ? ? ? }? ? }
/**? ? ?* 關閉FTP連接? ? ?*/? ? private void closeConnect() {? ? ? ? if (ftpClient != null && ftpClient.isConnected()) {? ? ? ? ? ? try {? ? ? ? ? ? ? ? ftpClient.logout();? ? ? ? ? ? ? ? ftpClient.disconnect();? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? System.out.println("關閉FTP連接失敗" + e.getMessage());? ? ? ? ? ? }? ? ? ? }? ? }? ??? ? /**? ? ?* 下載文件? ? ?* @param ftpPath FTP文件目錄? ? ?* @param fileName 需下載的文件名? ? ?* @param savePath 下載后的文件路徑? ? ?* @return 返回是否下載成功 true? ? ?*/? ? public Boolean downLoadFile(String ftpPath, String fileName, String savePath){? ? ? ? login();? ? ? ? OutputStream os = null;? ? ? ? if (ftpClient != null) {? ? ? ? ? ? try {? ? ? ? ? ? ? ? //判斷是否存在該目錄? ? ? ? ? ? ? ? if (!ftpClient.changeWorkingDirectory(ftpPath)) {? ? ? ? ? ? ? ? ? ? System.out.println("/" + ftpPath + "該目錄不存在");? ? ? ? ? ? ? ? ? ? return false;? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ftpClient.enterLocalPassiveMode();//設置被動模式,開通一個端口來傳輸數據
FTPFile[] ftpFiles = ftpClient.listFiles();? ? ? ? ? ? ? ? // 判斷該目錄下是否有文件? ? ? ? ? ? ? ? if (ftpFiles == null || ftpFiles.length == 0) {? ? ? ? ? ? ? ? System.out.println("/" + ftpPath + "該目錄下無文件");? ? ? ? ? ? ? ? ? ? return false;? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? for(FTPFile file : ftpFiles){? ? ? ? ? ? ? ? if(fileName.equals("") || fileName.equalsIgnoreCase(file.getName())){//文件名稱為"",下載指定文件? ? ? ? ? ? ? ? if(!file.isDirectory()){//是否文件夾?? ? ? ? ? ? ? ? ? ? ? ? ? ? File saveFile = new File(savePath + "/" + file.getName());?? ? ? ? ? ? ? ? ? ? ? ? ? ? os = new FileOutputStream(saveFile);?? ? ? ? ? ? ? ? ? ? ? ? ? ? ftpClient.retrieveFile(file.getName(), os);?? ? ? ? ? ? ? ? ? ? ? ? ? ? os.close();?? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? return true;? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? System.out.println("下載文件失敗" + e.getMessage());? ? ? ? ? ? } finally {? ? ? ? ? ? ? ? if(null != os){? ? ? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? os.close();? ? ? ? ? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? ? ? ? ? }?? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? closeConnect();? ? ? ? ? ? }? ? ? ? }? ? ? ? return false;? ? }
/**? ? * 上傳文件? ? * @param savePath FTP保存目錄? ? * @param fileName 上傳到FTP的文件名? ? * @param filePath 待上傳文件的名稱(絕對地址)? ? * @return? ? */? ? public boolean uploadFile(String savePath, String fileName,String filePath){? ? ? ? login();? ? ? ? boolean flag = false;? ? ? ? InputStream inputStream = null;? ? ? ? if (ftpClient != null) {? ? ? ? ? ? try{? ? ? ? ? ? ? ? inputStream = new FileInputStream(new File(filePath));? ? ? ? ? ? ? ? ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);? ? ? ? ? ? ? ? //CreateDirecroty(savePath);? ? ? ? ? ? ? ? ftpClient.makeDirectory(savePath);? ? ? ? ? ? ? ? ftpClient.changeWorkingDirectory(savePath);? ? ? ? ? ? ? ? ftpClient.storeFile(fileName, inputStream);? ? ? ? ? ? ? ? inputStream.close();? ? ? ? ? ? ? ? flag = true;? ? ? ? ? ? }catch (Exception e) {? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? }finally{? ? ? ? ? ? ? ? if(null != inputStream){? ? ? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? ? ? inputStream.close();? ? ? ? ? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? ? ? ? ? }?? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? closeConnect();? ? ? ? ? ? }? ? ? ? }? ? ? ? return flag;? ? }
//創建多層目錄文件,如果有ftp服務器已存在該文件,則不創建,如果無,則創建? ? public boolean CreateDirecroty(String remote) throws IOException {? ? ? ? boolean success = true;? ? ? ? /*String directory = remote + "/";? ? ? ? // 如果遠程目錄不存在,則遞歸創建遠程服務器目錄? ? ? ? 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("創建目錄[" + subDirectory + "]失敗");? ? ? ? ? ? ? ? ? ? ? ? changeWorkingDirectory(subDirectory);? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? } else {? ? ? ? ? ? ? ? ? ? changeWorkingDirectory(subDirectory);? ? ? ? ? ? ? ? }
paths = paths + "/" + subDirectory;? ? ? ? ? ? ? ? start = end + 1;? ? ? ? ? ? ? ? end = directory.indexOf("/", start);? ? ? ? ? ? ? ? // 檢查所有目錄是否創建完畢? ? ? ? ? ? ? ? if (end <= start) {? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }*/? ? ? ? return success;? ? }
/** * 刪除文件 *?? ? * @param pathname FTP服務器保存目錄? ? * @param filename 要刪除的文件名稱?? ? * @return */?? ? public boolean deleteFile(String filePath, String filename){? ?login();? ? ? ? boolean flag = false;?? ? ? ? if (ftpClient != null) {? ? ? ? ? ? try {?? ? ? ? ? ? ? ? ftpClient.changeWorkingDirectory(filePath);? ? ? ? ? ? ? ? ftpClient.dele(filename);?? ? ? ? ? ? ? ? flag = true;?? ? ? ? ? ? } catch (Exception e) {?? ? ? ? ? ? ? ? e.printStackTrace();?? ? ? ? ? ? } finally {? ? ? ? ? ? ? ? closeConnect();? ? ? ? ? ? }? ? ? ? }? ? ? ? return flag;?? ? }? ??}
標簽:ftp,Java,String,FTP,param,null,上傳,public,ftpClient
來源: https://www.cnblogs.com/LEON-D/p/12049465.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java中ftp删除文件,Java 实现ftp 文件上传、下载和删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php自动维护,完全禁用关闭Window
- 下一篇: java美元兑换,(Java实现) 美元