复制代码代码如下:
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;
/**
* Klassenname:FTPFileTransmit <BR>
* Klassenbeschreibung: Bitte schreiben Sie Ihre Beschreibung <BR>
* Bemerkung: <BR>
* @Version 1.00 2011-8-9
*/
öffentliche Klasse FTPFileTransmit {
privater String ftpPath;
privater String ftpName;
privater String ftpPassword;
privater String ftpServerIP;
public FTPFileTransmit() {
this.ftpPath = "xxx/xxx/";
this.ftpName = "name";
this.ftpPassword = "pass";
this.ftpServerIP = "192.168.0.xx";
}
/**
* Methodenname: saveInFTP <BR>
* Beschreibung: 把文件存储在FTP上 <BR>
* Bemerkung: <BR>
* @param FolderName 示例"xxx/xxx/"
* @param FileName ist „thefilename“
* @param data byte[]数组
* @return boolean<BR>
*/
public boolean saveInFTP (String FolderName, String FileName, byte[] data) {
boolesches Flag = false;
// 创建FTP客户端
FTPClient ftpClient = new FTPClient();
// 输入流用于读取文件
// FileInputStream fis = null;
ByteArrayInputStream bis = null;
versuchen {
// „FolderName“ und „FileName“.
if (FolderName != null
&& FolderName.compareTo("") != 0
&& Dateiname != null
&& FileName.compareTo("") != 0) {
// 建立FTP连接
ftpClient.connect(this.ftpServerIP);
// 如果登录成功后, 才进行创建输入流
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// Datei srcClientFile = new File("C:/ParseXML.xml");
// 实例化输入流
// fis = new FileInputStream(srcClientFile);
if (ftpClient.changeWorkingDirectory(FolderName)) {
// 将byte[]写入到输入流中, 实例化
bis = new ByteArrayInputStream(data);
// 设置缓冲
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制类型)
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 (Ausnahme e) {
e.printStackTrace();
Flag = false;
} Endlich {
versuchen {
// 关闭输入流
IOUtils.closeQuietly(bis);
// 关闭连接
ftpClient.disconnect();
} Catch (IOException e) {
e.printStackTrace();
}
}
Rückkehrflag;
}
/**
* Methodenname: getFromFTP <BR>
* Beschreibung: 从FTP上读取文件 <BR>
* Bemerkung: <BR>
* @return boolean<BR>
*/
public boolean getFromFTP () {
boolesches Flag = false;
// 创建FTP客户端
FTPClient ftpClient = new FTPClient();
// 输出流用于输出文件
FileOutputStream fos = null;
versuchen {
// 建立FTP连接
ftpClient.connect(this.ftpServerIP);
// 如果登录成功后, 才进行创建输出流
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// FTP-Server
String distinationFile = "/name/xxx/xxx/xxx文件";
// 实例化输出流
fos = new FileOutputStream("C:/ParseXML_InFTP.xml");
// 设置缓冲
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制类型)
if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {
ftpClient.retrieveFile(distinationFile, fos);
Flag = wahr;
}
}
} Catch (SocketException e) {
e.printStackTrace();
Flag = false;
} Catch (IOException e) {
e.printStackTrace();
Flag = false;
} Catch (Ausnahme e) {
e.printStackTrace();
Flag = false;
} Endlich {
versuchen {
// 关闭输出流
IOUtils.closeQuietly(fos);
// 关闭连接
ftpClient.disconnect();
} Catch (IOException e) {
e.printStackTrace();
}
}
Rückkehrflag;
}
public boolean createDirectory() {
boolesches Flag = false;
// 创建FTP客户端
FTPClient ftpClient = new FTPClient();
versuchen {
// 建立FTP连接
ftpClient.connect(this.ftpServerIP);
// 如果登录成功后, 才进行操作
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 = wahr;
}
}
} Catch (SocketException e) {
e.printStackTrace();
Flag = false;
} Catch (IOException e) {
e.printStackTrace();
Flag = false;
} Catch (Ausnahme e) {
e.printStackTrace();
Flag = false;
} Endlich {
versuchen {
// 关闭连接
ftpClient.disconnect();
} Catch (IOException e) {
e.printStackTrace();
}
}
Rückkehrflag;
}
public String[] getAllFolderNames () {
// 创建FTP客户端
FTPClient ftpClient = new FTPClient();
versuchen {
// 建立FTP连接
ftpClient.connect(this.ftpServerIP);
// 如果登录成功后, 才进行操作
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// 切换文件路径, 到FTP上的„NNDD3“文件夹下
if (this.ftpPath != null && this.ftpPath.compareTo("") != 0
&& ftpClient.changeWorkingDirectory(this.ftpPath)) {
// 将当前时间减去2天, 删除的是前两天的数据包
String time = minusTime();
String[] allNames = ftpClient.listNames();
String[] temp = new String[allNames.length];
// 初始化数组
for (int j = 0; j < allNames.length; j ++) {
temp[j] = "";
}
// 找出要删除文件夹的名称
for (int i = 0; i < allNames.length; i ++) {
if (allNames[i].substring(0, 8).compareTo(time) <= 0) {
temp[i] = allNames[i];
}
}
Rücklauftemperatur;
}
}
} Catch (SocketException e) {
e.printStackTrace();
} Catch (IOException e) {
e.printStackTrace();
} Endlich {
versuchen {
// 关闭连接
ftpClient.disconnect();
} Catch (IOException e) {
e.printStackTrace();
}
}
null zurückgeben;
}
/**
*
* Methodenname: minusTime <BR>
* Beschreibung: 获取钱两天的时间, 如2011-8-1的前两天就是2011-7-30 <BR>
* Bemerkung: <BR>
* @return String<BR>
*/
privater String minusTime() {
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
Datum d = neues Datum();
String timeMinus2 = df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000));
RückkehrzeitMinus2;
}
public static void main(String[] args) {
FTPFileTransmit ftpFileTransmit = new FTPFileTransmit();
ftpFileTransmit.deleteFoldersInFTP();
// boolesches Flag = ftpFileTransmit.createDirectory();
// if (flag) {
// System.out.println("****** FTP-Drucker ******");
// }
// String FolderName = ftpFileTransmit.ftpPath + "20110809_ReTransmit/";
// byte[] data = neues 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-Drucker ******");
// }
}
}