zoukankan      html  css  js  c++  java
  • 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;

        }

  • 相关阅读:
    实时获取阿里旺旺聊天记录,实时获取千牛聊天记录
    千牛hook 旺旺hook,旺旺发消息call,千牛发消息call,千牛机器人,破解旺旺发消息代码,破解千牛发消息代码,反汇编旺旺发消息,反汇编千牛发消息,旺旺发消息组件,千牛发消息组件
    hook千牛 千牛破解发消息 千牛机器人 千牛发消息组件 调用千牛发消息 实时获取千牛聊天记录 可以提供代码
    [转发]分布式事务,这一篇就够了
    C++之throw以及try{}...catch{}【转载】
    C++之Effective C++学习-条款2
    c++中为什么析构函数要被设置为虚函数(virtual)
    c++中在声明静态变量时,使用const可直接初始化,不在需要定义式
    js检测浏览器类型_js检测是否为火狐浏览器
    PHP8.0 JIT 配置
  • 原文地址:https://www.cnblogs.com/attilax/p/15197618.html
Copyright © 2011-2022 走看看