欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java操作ftp下载文件示例

程序员文章站 2024-02-24 11:33:04
复制代码 代码如下:    /**     *      *...

复制代码 代码如下:

    /**
     *
     * java操作 ftp 下载
     * 文件下载。
     *
     */
    private void ftpdownload()
    {
        ftpclient ftpclient = null;
        inputstream input = null;
        boolean loginflag = false;
        list<string> list = new arraylist<string>();
        try
        {
            int defaultport = commonsmessage.ftp_default_port; // 21
            int timeout = m400constant.num_sixty_thousand * m400constant.num_two; // 120000
            ftpclient = new ftpclient();

            ftpclient.setdefaultport(defaultport);
            ftpclient.setconnecttimeout(timeout);
//            ftpclient.setsotimeout(timeout);
            ftpclient.setdefaulttimeout(timeout);
            ftpclient.setdatatimeout(timeout);
            ftpclient.setcontrolencoding("utf-8");
            ftpclientconfig conf = new ftpclientconfig(ftpclientconfig.syst_unix);
            conf.setserverlanguagecode("en");
            conf.setdefaultdateformatstr("mmm dd hh:mm");
            ftpclient.configure(conf);

            ftpclient.connect(systemconfig.getkey("scpftpip"));
            loginflag = ftpclient.login(systemconfig.getkey("scpftpusername").trim(),
                    systemconfig.getkey("scpftppassword").trim());
            string ftpdir = systemconfig.getkey("ftphangupdir").trim();
            if (!loginflag)
            {
                debugger.debug("login scp ftp is :" + loginflag);
            }
            // string ftpdir = systemconfig.getkey("ftphangupdir").trim();

            if (ftpclient.changeworkingdirectory(ftpdir))
            {
               

                ftpfile file[] = ftpclient.listfiles(ftpdir);

                if (file != null && file.length > 0)
                {
                    debugger.debug("hungup file size is:  " + file.length);
                    for (int i = 0; i < file.length; i++)
                    {
                        string filename = file[i].getname();
                        ftpclient.setbuffersize(commonsmessage.one_k_buffer_size);

                        // 设置文件类型(二进制)
                        ftpclient.setfiletype(ftp.binary_file_type);

                        input = ftpclient.retrievefilestream(filename);
                        processinput(ftpclient, input, list, filename);

                        // if (downflag) {
                        // ftpclient.deletefile(filename);
                        // }
                    }
                }
                else
                {
                    try
                    {
                        thread.sleep(m400constant.num_five_thousand);
                    }
                    catch (interruptedexception e)
                    {
                        /*
                         * if (debugger.isdebugenable()) {
                         * debugger.debug("failed to queryscptoquery", e); }
                         */
                        error("failed to queryscptoquery", e);
                    }
                }

            }
            else
            {
                debugger.debug("scp dir is error ");
            }

        }
        catch (exception e)
        {
            /*
             * debugger.debug("dowonload file is faild  ", e);
             */
            error("dowonload file is faild  ", e);
        }
        finally
        {
            close(input, "close inputstream  is faild  ");
            if (ftpclient != null)
            {
                try
                {
                    deletefile(ftpclient, list);
                    boolean logoutflag = false;
                    try
                    {
                        logoutflag = ftpclient.logout();
                        // debugger.debug("logout : " + logoutflag);
                    }
                    catch (exception e)
                    {
                        /*
                         * debugger.debug("logout : " + logoutflag);
                         */
                        error("logout : " + logoutflag, e);
                    }
                    ftpclient = null;
                }
                catch (exception e)
                {
                    /*
                     * if (debugger.isdebugenable()) {
                     * debugger.error("failed to ftpdownload", e); }
                     */
                    error("failed to ftpdownload", e);
                }
                // try {
                // ftpclient.disconnect();
                // } catch (ioexception e) {
                // if (debugger.isdebugenable()) {
                // debugger.debug("failed to ftpdownload", e);
                // }
                // }
            }
        }
    }

    /**
     * 处理ftp对应文件输入流。
     *
     * @param ftpclient
     *            ftp客户端对象
     * @param input
     *            ftp文件输入流
     * @param list
     *            处理完毕的文件列表,用于执行文件删除使用
     * @param filename
     *            当前处理的文件名称
     */
    private void processinput(ftpclient ftpclient, inputstream input, list<string> list, string filename)
    {
        if (input == null)
        {
            return;
        }
        // boolean downflag = callingservice(input);
        callingservice(input);
        try
        {
            input.close();
            ftpclient.completependingcommand();
            list.add(filename);
        }
        catch (exception e)
        {
            /*
             * debugger.debug("failed to ftpdownload", e);
             */
            error("failed to ftpdownload", e);
        }
    }
}