public classFtp {private static final Logger logger = Logger.getLogger(FTP.class);/*** FTP客户端*/
privateFtpClient ftpClient;/*** 服务器连接
*
*@paramip
* 服务器IP
*@paramport
* 服务器端口
*@paramuser
* 用户名
*@parampassword
* 密码
*@parampath
* 服务器路径
*@throwsIOException
**/
public void connectServer(String ip, int port, String user, String password, String path) throwsIOException {try{
System.out.println("ftp connect...");
ftpClient= newFtpClient(ip);
ftpClient.login(user, password);//设置成2进制传输
ftpClient.binary();
System.out.println("ftp login success!");if (path.length() != 0) {//把远程系统上的目录切换到参数path所指定的目录
try{
ftpClient.cd(path);
}catch(Exception e) {throw new IOException("FTP server: 550 CWD failed 远程目录未响应", e);
}
}
ftpClient.binary();
}catch(IOException e) {
logger.error("ftp connect err!");
logger.error(e.getMessage());
e.printStackTrace();throw newIOException(e.getMessage());
}
}/*** 关闭连接
*
**/
public voidcloseConnect() {try{
ftpClient.closeServer();
System.out.println("closeConnect success");
}catch(IOException ex) {
System.out.println("not closeConnect");
ex.printStackTrace();throw newRuntimeException(ex);
}
}/*** 上传文件
*
*@paramlocalFile
* 本地文件
*@paramremoteFile
* 远程文件
**/
public voidupload(String localFile, String remoteFile) {
TelnetOutputStream os= null;
FileInputStream is= null;try{//将远程文件加入输出流中
os =ftpClient.put(remoteFile);//获取本地文件的输入流
File file_in = newFile(localFile);
is= newFileInputStream(file_in);//创建一个缓冲区
byte[] bytes = new byte[1024];intc;while ((c = is.read(bytes)) != -1) {
os.write(bytes,0, c);
}
System.out.println("upload success");
}catch(IOException ex) {
System.out.println("not upload");
ex.printStackTrace();throw newRuntimeException(ex);
}finally{try{if (is != null) {
is.close();
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (os != null) {
os.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 下载文件
*
*@paramremoteFile
* 远程文件路径(服务器端)
*@paramlocalFile
* 本地文件路径(客户端)
**/
public voiddownload(String remoteFile, String localFile) {
TelnetInputStream is= null;
FileOutputStream os= null;try{//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is =ftpClient.get(remoteFile);
File file_in= newFile(localFile);
os= newFileOutputStream(file_in);byte[] bytes = new byte[1024];intc;while ((c = is.read(bytes)) != -1) {
os.write(bytes,0, c);
}
System.out.println("download success");
}catch(IOException ex) {
System.out.println("not download");
ex.printStackTrace();throw newRuntimeException(ex);
}finally{try{if (is != null) {
is.close();
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (os != null) {
os.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 返回FTP目录下的文件列表
*
*@paramftpDirectory
*@return
*/
public ListgetFileNameList(String ftpDirectory) {
List list = new ArrayList();
BufferedReader bf= null;
InputStreamReader isr= null;
DataInputStream ds= null;try{
ds= newDataInputStream(ftpClient.nameList(ftpDirectory));
isr= newInputStreamReader(ds);
bf= newBufferedReader(isr);
String filename= "";while ((filename = bf.readLine()) != null) {
list.add(filename);
}
}catch(Exception e) {
e.printStackTrace();
}finally{try{
ds.close();
}catch(IOException e) {
e.printStackTrace();
}try{
isr.close();
}catch(IOException e) {
e.printStackTrace();
}try{
bf.close();
}catch(IOException e) {
e.printStackTrace();
}
}returnlist;
}public static void main(String agrs[]) throwsException {
Ftp ftp= newFtp();
ftp.connectServer("192.168.1.221", 21, "admin", "123@123", "/FTP Files/temp");
List list = ftp.getFileNameList("/FTP Files/temp");for(String str : list) {try{
Calendar cal= Calendar.getInstance();//使用日历类
int year = cal.get(Calendar.YEAR);//得到年
int month = cal.get(Calendar.MONTH) + 1;//得到月,因为从0开始的,所以要加1
int day = cal.get(Calendar.DAY_OF_MONTH);//得到天
String fileName = "WiMAX_" + year + month +day;
System.out.println("str" + str + "---------" + "fileName:" +fileName);
String regEx= "WiMAX_\\d{8}_\\d{5}_\\s{5}"; //表示a或F
Pattern pat =Pattern.compile(regEx);
System.out.println(pat.matcher(str).find());//if (str.contains(fileName)) {//fu.download("/FTP Files/temp/" + str, "d:/temp/" + str);//System.out.println("download:" + str);//}
}catch(Exception e) {
e.printStackTrace();
logger.error(e.getMessage());throwe;
}
}
ftp.closeConnect();
}
}