JAVA 实现FTP功能_Java实现FTP上传下载功能

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值