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: 把文件存儲在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)文章
Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解
這篇文章主要介紹了Spring?Cloud?Gateway中netty線程池優(yōu)化示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07mybatis-plus中l(wèi)ambdaQuery()與lambdaUpdate()比較常見的使用方法總結(jié)
mybatis-plus是在mybatis的基礎(chǔ)上做增強(qiáng)不做改變,簡化了CRUD操作,下面這篇文章主要給大家介紹了關(guān)于mybatis-plus中l(wèi)ambdaQuery()與lambdaUpdate()比較常見的使用方法,需要的朋友可以參考下2022-09-09一篇文章帶你了解Java Spring基礎(chǔ)與IOC
這篇文章主要介紹了Java Spring基礎(chǔ)與IOC,文中講解的相關(guān)內(nèi)容非常詳細(xì),也運(yùn)用了大量的代碼進(jìn)行講解,感興趣的小伙伴可以參考一下2021-08-08SpringCloud引入feign失敗或找不到@EnableFeignClients注解問題
這篇文章主要介紹了SpringCloud引入feign失敗或找不到@EnableFeignClients注解問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Springboot項(xiàng)目引入druid安裝部署使用教程
這篇文章主要介紹了Springboot項(xiàng)目引入druid安裝部署使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Java實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10SpringBoot自定義注解及AOP的開發(fā)和使用詳解
在公司項(xiàng)目中,如果需要做一些公共的功能,如日志等,最好的方式是使用自定義注解,自定義注解可以實(shí)現(xiàn)我們對想要添加日志的方法上添加,這篇文章基于日志功能來講講自定義注解應(yīng)該如何開發(fā)和使用,需要的朋友可以參考下2023-08-08