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


    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;

        }

  • 相关阅读:
    spring cloud网关gateway
    maven将依赖第三方包打包(package)到jar中
    spring boot创建多模块聚合工程
    spring cloud服务间调用feign
    取模和取余的区别
    实现多线程编程的三种方式
    打开eclipse编译后的.class文件
    对中断interrupt的理解
    对final和static的理解
    对synchronized的一点理解
  • 原文地址:https://www.cnblogs.com/attilax/p/15197630.html
Copyright © 2020-2023  润新知