java使用ftp上傳文件示例分享
更新時(shí)間:2014年02月17日 14:19:18 作者:
這篇文章主要介紹了java使用ftp上傳文件示例,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
/**
* class name:FTPFileTransmit <BR>
* class description: please write your description <BR>
* Remark: <BR>
* @version 1.00 2011-8-9
*/
public class FTPFileTransmit {
private String ftpPath;
private String ftpName;
private String ftpPassword;
private String ftpServerIP;
public FTPFileTransmit() {
this.ftpPath = "xxx/xxx/";
this.ftpName = "name";
this.ftpPassword = "pass";
this.ftpServerIP = "192.168.0.xx";
}
/**
* Method name: saveInFTP <BR>
* Description: 把文件存儲(chǔ)在FTP上 <BR>
* Remark: <BR>
* @param FolderName 示例"xxx/xxx/"
* @param FileName 示例"thefilename"
* @param data byte[]數(shù)組
* @return boolean<BR>
*/
public boolean saveInFTP (String FolderName, String FileName, byte[] data) {
boolean flag = false;
// 創(chuàng)建FTP客戶端
FTPClient ftpClient = new FTPClient();
// 輸入流用于讀取文件
// FileInputStream fis = null;
ByteArrayInputStream bis = null;
try {
// 如果FolderName 和 FileName都不符合基本要求, 那么就沒有必要進(jìn)行ftp操作
if (FolderName != null
&& FolderName.compareTo("") != 0
&& FileName != null
&& FileName.compareTo("") != 0) {
// 建立FTP連接
ftpClient.connect(this.ftpServerIP);
// 如果登錄成功后, 才進(jìn)行創(chuàng)建輸入流
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// File srcClientFile = new File("C:/ParseXML.xml");
// 實(shí)例化輸入流
// fis = new FileInputStream(srcClientFile);
if (ftpClient.changeWorkingDirectory(FolderName)) {
// 將byte[]寫入到輸入流中, 實(shí)例化
bis = new ByteArrayInputStream(data);
// 設(shè)置緩沖
ftpClient.setBufferSize(1024);
// 設(shè)置文件類型(二進(jìn)制類型)
if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {
flag = ftpClient.storeFile(FileName, bis);
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
// 關(guān)閉輸入流
IOUtils.closeQuietly(bis);
// 關(guān)閉連接
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
/**
* Method name: getFromFTP <BR>
* Description: 從FTP上讀取文件 <BR>
* Remark: <BR>
* @return boolean<BR>
*/
public boolean getFromFTP () {
boolean flag = false;
// 創(chuàng)建FTP客戶端
FTPClient ftpClient = new FTPClient();
// 輸出流用于輸出文件
FileOutputStream fos = null;
try {
// 建立FTP連接
ftpClient.connect(this.ftpServerIP);
// 如果登錄成功后, 才進(jìn)行創(chuàng)建輸出流
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// FTP文件
String distinationFile = "/name/xxx/xxx/xxx文件";
// 實(shí)例化輸出流
fos = new FileOutputStream("C:/ParseXML_InFTP.xml");
// 設(shè)置緩沖
ftpClient.setBufferSize(1024);
// 設(shè)置文件類型(二進(jìn)制類型)
if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {
ftpClient.retrieveFile(distinationFile, fos);
flag = true;
}
}
} catch (SocketException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
// 關(guān)閉輸出流
IOUtils.closeQuietly(fos);
// 關(guān)閉連接
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public boolean createDirectory() {
boolean flag = false;
// 創(chuàng)建FTP客戶端
FTPClient ftpClient = new FTPClient();
try {
// 建立FTP連接
ftpClient.connect(this.ftpServerIP);
// 如果登錄成功后, 才進(jìn)行操作
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// 切換文件路徑, 到FTP上的"NNDD3"文件夾下
if (this.ftpPath != null && this.ftpPath.compareTo("") != 0
&& ftpClient.changeWorkingDirectory(this.ftpPath)) {
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
String time = f.format(new Date());
String FolderName = time + "_ReTransmit";
ftpClient.makeDirectory(FolderName);
flag = true;
}
}
} catch (SocketException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
// 關(guān)閉連接
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public String[] getAllFolderNames () {
// 創(chuàng)建FTP客戶端
FTPClient ftpClient = new FTPClient();
try {
// 建立FTP連接
ftpClient.connect(this.ftpServerIP);
// 如果登錄成功后, 才進(jìn)行操作
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// 切換文件路徑, 到FTP上的"NNDD3"文件夾下
if (this.ftpPath != null && this.ftpPath.compareTo("") != 0
&& ftpClient.changeWorkingDirectory(this.ftpPath)) {
// 將當(dāng)前時(shí)間減去2天, 刪除的是前兩天的數(shù)據(jù)包
String time = minusTime();
String[] allNames = ftpClient.listNames();
String[] temp = new String[allNames.length];
// 初始化數(shù)組
for (int j = 0; j < allNames.length; j ++) {
temp[j] = "";
}
// 找出要?jiǎng)h除文件夾的名稱
for (int i = 0; i < allNames.length; i ++) {
if (allNames[i].substring(0, 8).compareTo(time) <= 0) {
temp[i] = allNames[i];
}
}
return temp;
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 關(guān)閉連接
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
*
* Method name: minusTime <BR>
* Description: 獲取錢兩天的時(shí)間,如2011-8-1的前兩天就是2011-7-30 <BR>
* Remark: <BR>
* @return String<BR>
*/
private String minusTime() {
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
Date d = new Date();
String timeMinus2 = df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000));
return timeMinus2;
}
public static void main(String[] args) {
FTPFileTransmit ftpFileTransmit = new FTPFileTransmit();
ftpFileTransmit.deleteFoldersInFTP();
// boolean flag = ftpFileTransmit.createDirectory();
// if (flag) {
// System.out.println("****** FTP文件夾創(chuàng)建成功 ******");
// }
// String FolderName = ftpFileTransmit.ftpPath + "20110809_ReTransmit/";
// byte[] data = new byte[1024];
// for (int i = 0; i < data.length; i ++) {
// data[i] = 'a';
// }
// boolean flag = ftpFileTransmit.saveInFTP(FolderName, "2011080912345678" ,data);
// if (flag) {
// System.out.println("****** FTP文件夾創(chuàng)建成功 ******");
// }
}
}
您可能感興趣的文章:
- JAVA中使用FTPClient實(shí)現(xiàn)文件上傳下載實(shí)例代碼
- java實(shí)現(xiàn)FTP文件上傳與文件下載
- JAVA技術(shù)實(shí)現(xiàn)上傳下載文件到FTP服務(wù)器(完整)
- Java中FTPClient上傳中文目錄、中文文件名亂碼問題解決方法
- Java通過FTP服務(wù)器上傳下載文件的方法
- JAVA SFTP文件上傳、下載及批量下載實(shí)例
- Java實(shí)現(xiàn)FTP文件與文件夾的上傳和下載
- java實(shí)現(xiàn)ftp上傳 如何創(chuàng)建文件夾
- Java實(shí)現(xiàn)FTP批量大文件上傳下載篇1
- java實(shí)現(xiàn)上傳文件到FTP
相關(guān)文章
深入理解Java SpringCloud Ribbon 負(fù)載均衡
Ribbon是一個(gè)客戶端負(fù)載均衡器,它提供了對(duì)HTTP和TCP客戶端的行為的大量控制。這篇文章主要介紹了SpringCloud Ribbon 負(fù)載均衡的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2021-09-09
Spring框架中Bean的三種配置和實(shí)例化方法總結(jié)
在Spring框架中,Bean的配置和實(shí)例化是很重要的基礎(chǔ)內(nèi)容,掌握各種配置方式,才能靈活管理Bean對(duì)象,本文將全面介紹Bean的別名配置、作用范圍配置,以及構(gòu)造器實(shí)例化、工廠實(shí)例化等方式2023-10-10
面試官:詳細(xì)談?wù)凧ava對(duì)象的4種引用方式
這篇文章主要給大家介紹了java面試官常會(huì)問到的,關(guān)于Java對(duì)象的4種引用方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway詳細(xì)分析
這篇文章主要介紹了SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
springcloud-feign調(diào)用報(bào)錯(cuò)問題
這篇文章主要介紹了springcloud-feign調(diào)用報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

