Atitit java ftp client sumup apache common net jar 1.1. 协议解析 1 1.2. 读取文件 1 2. } 3 1. 下载文件 ftpCli

本文介绍了一个基于Java的FTP客户端工具类实现,该工具类利用Apache Commons Net库进行FTP文件的上传和下载操作,并提供了连接FTP服务器的方法。文章通过具体示例展示了如何使用此工具类完成常见的FTP任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Atitit java ftp client  sumup apache common net jar

 

1.1. 协议解析 1

1.2. 读取文件 1

2. } 3

1. 下载文件   ftpClient.retrieveFile("/home/cnhis/managerweb/webapps/webcon/cpu.htm", new FileOutputStream(new File(pathname))); 3

2.1. 上传 4

 

 

1.1. 协议解析

1.2. 读取文件

 

 

package com.attilax.net.ftp;

 

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Map;

import java.util.StringTokenizer;

 

import org.apache.commons.io.FileUtils;

import org.apache.commons.io.IOUtils;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

import org.apache.log4j.Logger;

 

import com.alibaba.fastjson.JSON;

import com.attilax.io.FileService;

import com.google.common.collect.Maps;

 

/**

 * Ftp工具类 需要导入commons-net-3.4.jar这个包

 */

public class FtpUtil {

    

    private static Logger logger=Logger.getLogger(FtpUtil.class);

    

    private static FTPClient ftp;

    

    public static void main(String[] args) throws FileNotFoundException, IOException {

    String ftpf = "ftp://192.168.1.18/home/cnhis/managerweb/webapps/webcon/cpu.htm";

     ftpf = "ftp://192.168.1.77:5221/0logs555/cpu2.htm";

FTPFile ftpfile=new FTPFile();

  //  连接ftp server

byte[] data_bytearr = ftpurl2bytearr(ftpf,7000);

System.out.println("get data_bytearr ");

 String pathname = "c:\\0logs555\\cpu9.htm";

FileService.writeByteArrayToFile(pathname,data_bytearr);

//FileOutputStream fileOutputStream = new FileOutputStream(new File(pathname));

    

 

    System.out.println("000");

}

private static byte[] ftpurl2bytearr(String ftpurl, int timeout) throws IOException {

Map m=parseFtpurl(ftpurl);

FtpUtil fu=new FtpUtil();

fu.host=(String) m.get("host");

fu.port=  (int) m.get("port");

System.out.println(JSON.toJSONString(fu));

        FTPClient ftpClient =fu. connect(timeout);

        System.out.println("connect finished ");

ByteArrayOutputStream ByteArrayOutputStream1 = new ByteArrayOutputStream();

String reltPath = "/home/cnhis/managerweb/webapps/webcon/cpu.htm";

reltPath=(String) m.get("path");

ftpClient.retrieveFile(reltPath, ByteArrayOutputStream1);

byte[] data_bytearr=ByteArrayOutputStream1.toByteArray();

return data_bytearr;

}

    @SuppressWarnings("all")

private static Map parseFtpurl(String ftpurl) {

int doubleslash_protocalstart_Index=ftpurl.indexOf("//");

int host_port_endIndex=ftpurl.indexOf("/", doubleslash_protocalstart_Index+2);

int maohaorIdx_hostport_splitor=ftpurl.indexOf(":",doubleslash_protocalstart_Index);

Map m=Maps.newConcurrentMap();

if(maohaorIdx_hostport_splitor>0 )

m.put("host", ftpurl.substring(doubleslash_protocalstart_Index+2, maohaorIdx_hostport_splitor));

else

m.put("host", ftpurl.substring(doubleslash_protocalstart_Index+2, host_port_endIndex));

m.put("path", ftpurl.substring(host_port_endIndex, ftpurl.length()));

if(maohaorIdx_hostport_splitor>0 )

m.put("port", Integer.parseInt( ftpurl.substring(maohaorIdx_hostport_splitor+1, host_port_endIndex)));

return m;

2.  }

1. 下载文件  ftpClient.retrieveFile("/home/cnhis/managerweb/webapps/webcon/cpu.htm", new FileOutputStream(new File(pathname)));

 

2.1. 上传

 

 

 

 

package com.attilax.net.ftp;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.StringTokenizer;

 

import org.apache.commons.io.FileUtils;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

import org.apache.log4j.Logger;

 

/**

 * Ftp工具类 需要导入commons-net-3.4.jar这个包

 */

public class FtpUtil {

    

    private static Logger logger=Logger.getLogger(FtpUtil.class);

    

    private static FTPClient ftp;

    

    public static void main(String[] args) throws FileNotFoundException, IOException {

    String ftpf = "ftp://192.168.1.18/home/cnhis/managerweb/webapps/webcon/cpu.htm";

FTPFile ftpfile=new FTPFile();

  //  连接ftp server

FtpUtil fu=new FtpUtil();

fu.host="192.168.1.18";

        FTPClient ftpClient =fu. connect();

        String pathname = "c:\\0logs555\\cpu2.htm";

        FileUtils.forceMkdir(new File(pathname).getParentFile());

ftpClient.retrieveFile("/home/cnhis/managerweb/webapps/webcon/cpu.htm", new FileOutputStream(new File(pathname)));

    

    System.out.println("000");

}

 

 

 

    public static final String ANONYMOUS_USER="anonymous";

 

private String ftpPath;

 

private String host;

 

private int port=21;

 

private String user="anonymous";

 

private String password;

    private FTPClient connect(){        

        FTPClient client = new FTPClient();

        try{

            //连接FTP Server

            client.connect(this.host, this.port);

            //登陆

            if(this.user==null||"".equals(this.user)){

                //使用匿名登陆

                client.login(ANONYMOUS_USER, ANONYMOUS_USER);

            }else{

                client.login(this.user, this.password);

            }

            //设置文件格式

            client.setFileType(FTPClient.BINARY_FILE_TYPE);

            //获取FTP Server 应答

            int reply = client.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)){

                client.disconnect();

                throw new RuntimeException("  FTPReply.isPositiveCompletion(reply) : replycode is "+reply );

            }

            //切换工作目录  if defin workdir,if not difin nothing todo

            changeWorkingDirectory(client);

            System.out.println("===连接到FTP:"+host+":"+port);

        }catch(IOException e){

            throw new RuntimeException(e);

        }

        return client;

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值