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

java使用ftp上传文件示例分享

程序员文章站 2024-02-24 11:50:04
复制代码 代码如下:import java.io.bytearrayinputstream;  import java.io.fileoutputstream;&...

复制代码 代码如下:

import java.io.bytearrayinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.net.socketexception; 
import java.text.simpledateformat; 
import java.util.date; 

import org.apache.commons.io.ioutils; 
import org.apache.commons.net.ftp.ftpclient; 

/**
 * class name:ftpfiletransmit <br>
 * class description: please write your description <br>
 * remark: <br>
 * @version 1.00 2011-8-9
 */ 
public class ftpfiletransmit { 

    private string ftppath; 
    private string ftpname; 
    private string ftppassword; 
    private string ftpserverip; 

    public ftpfiletransmit() { 
        this.ftppath = "xxx/xxx/"; 
        this.ftpname = "name"; 
        this.ftppassword = "pass"; 
        this.ftpserverip = "192.168.0.xx"; 
    } 

     
    /**
     * method name: saveinftp <br>
     * description: 把文件存储在ftp上 <br>
     * remark: <br>
     * @param foldername            示例"xxx/xxx/"
     * @param filename              示例"thefilename"
     * @param data                  byte[]数组
     * @return  boolean<br>
     */ 
    public boolean saveinftp (string foldername, string filename, byte[] data) { 
        boolean flag = false; 

        // 创建ftp客户端  
        ftpclient ftpclient = new ftpclient(); 
        // 输入流用于读取文件  
//      fileinputstream fis = null;  
        bytearrayinputstream bis = null; 

        try { 
            // 如果foldername 和 filename都不符合基本要求, 那么就没有必要进行ftp操作  
            if (foldername != null 
                    && foldername.compareto("") != 0 
                    && filename != null 
                    && filename.compareto("") != 0) { 

                // 建立ftp连接  
                ftpclient.connect(this.ftpserverip); 

                // 如果登录成功后, 才进行创建输入流  
                if (ftpclient.login(this.ftpname, this.ftppassword)) { 
//                  file srcclientfile = new file("c:/parsexml.xml");  

                    // 实例化输入流  
//                  fis = new fileinputstream(srcclientfile);  

                    if (ftpclient.changeworkingdirectory(foldername)) { 
                        // 将byte[]写入到输入流中, 实例化  
                        bis = new bytearrayinputstream(data); 

                        // 设置缓冲  
                        ftpclient.setbuffersize(1024); 

                        // 设置文件类型(二进制类型)  
                        if (ftpclient.setfiletype(ftpclient.binary_file_type)) { 
                            flag = ftpclient.storefile(filename, bis); 
                        } 
                    } 
                } 
            } 
        } catch (socketexception e) { 
            e.printstacktrace(); 
            flag = false; 
        } catch (ioexception e) { 
            e.printstacktrace(); 
            flag = false; 
        } catch (exception e) { 
            e.printstacktrace(); 
            flag = false; 
        } finally { 
            try { 
                // 关闭输入流  
                ioutils.closequietly(bis); 
                // 关闭连接  
                ftpclient.disconnect(); 
            } catch (ioexception e) { 
                e.printstacktrace(); 
            } 
        } 

        return flag; 
    } 

    /**
     * method name: getfromftp <br>
     * description: 从ftp上读取文件 <br>
     * remark: <br>
     * @return  boolean<br>
     */ 
    public boolean getfromftp () { 
        boolean flag = false; 

        // 创建ftp客户端  
        ftpclient ftpclient = new ftpclient(); 
        // 输出流用于输出文件  
        fileoutputstream fos = null; 

        try { 
            // 建立ftp连接  
            ftpclient.connect(this.ftpserverip); 
            // 如果登录成功后, 才进行创建输出流  
            if (ftpclient.login(this.ftpname, this.ftppassword)) { 
                // ftp文件  
                string distinationfile = "/name/xxx/xxx/xxx文件"; 
                // 实例化输出流  
                fos = new fileoutputstream("c:/parsexml_inftp.xml"); 

                // 设置缓冲  
                ftpclient.setbuffersize(1024); 

                // 设置文件类型(二进制类型)  
                if (ftpclient.setfiletype(ftpclient.binary_file_type)) { 
                    ftpclient.retrievefile(distinationfile, fos); 
                    flag = true; 
                } 
            } 
        } catch (socketexception e) { 
            e.printstacktrace(); 
            flag = false; 
        } catch (ioexception e) { 
            e.printstacktrace(); 
            flag = false; 
        } catch (exception e) { 
            e.printstacktrace(); 
            flag = false; 
        } finally { 
            try { 
                // 关闭输出流  
                ioutils.closequietly(fos); 
                // 关闭连接  
                ftpclient.disconnect(); 
            } catch (ioexception e) { 
                e.printstacktrace(); 
            } 
        } 

        return flag; 
    } 

    public boolean createdirectory() { 
        boolean flag = false; 

        // 创建ftp客户端  
        ftpclient ftpclient = new ftpclient(); 

        try { 
            // 建立ftp连接  
            ftpclient.connect(this.ftpserverip); 
            // 如果登录成功后, 才进行操作  
            if (ftpclient.login(this.ftpname, this.ftppassword)) { 

                // 切换文件路径, 到ftp上的"nndd3"文件夹下  
                if (this.ftppath != null && this.ftppath.compareto("") != 0 
                        && ftpclient.changeworkingdirectory(this.ftppath)) { 
                    simpledateformat f = new simpledateformat("yyyymmdd"); 
                    string time = f.format(new date()); 

                    string foldername = time + "_retransmit"; 
                    ftpclient.makedirectory(foldername); 
                    flag = true; 
                } 
            } 
        } catch (socketexception e) { 
            e.printstacktrace(); 
            flag = false; 
        } catch (ioexception e) { 
            e.printstacktrace(); 
            flag = false; 
        } catch (exception e) { 
            e.printstacktrace(); 
            flag = false; 
        } finally { 
            try { 
                // 关闭连接  
                ftpclient.disconnect(); 
            } catch (ioexception e) { 
                e.printstacktrace(); 
            } 
        } 

        return flag; 
    } 

    public string[] getallfoldernames () { 
        // 创建ftp客户端  
        ftpclient ftpclient = new ftpclient(); 

        try { 
            // 建立ftp连接  
            ftpclient.connect(this.ftpserverip); 

            // 如果登录成功后, 才进行操作  
            if (ftpclient.login(this.ftpname, this.ftppassword)) { 

                // 切换文件路径, 到ftp上的"nndd3"文件夹下  
                if (this.ftppath != null && this.ftppath.compareto("") != 0 
                        && ftpclient.changeworkingdirectory(this.ftppath)) { 
                    // 将当前时间减去2天, 删除的是前两天的数据包  
                    string time = minustime(); 

                    string[] allnames = ftpclient.listnames(); 

                    string[] temp = new string[allnames.length]; 
                    // 初始化数组  
                    for (int j = 0; j < allnames.length; j ++) { 
                        temp[j] = ""; 
                    } 

                    // 找出要删除文件夹的名称  
                    for (int i = 0; i < allnames.length; i ++) { 
                        if (allnames[i].substring(0, 8).compareto(time) <= 0) { 
                            temp[i] = allnames[i]; 
                        } 
                    } 

                    return temp; 
                } 
            } 
        } catch (socketexception e) { 
            e.printstacktrace(); 
        } catch (ioexception e) { 
            e.printstacktrace(); 
        } finally { 
            try { 
                // 关闭连接  
                ftpclient.disconnect(); 
            } catch (ioexception e) { 
                e.printstacktrace(); 
            } 
        } 

        return null; 
    } 

    /**
     * 
     * method name: minustime <br>
     * description: 获取钱两天的时间,如2011-8-1的前两天就是2011-7-30 <br>
     * remark: <br>
     * @return  string<br>
     */ 
    private string minustime() { 
        simpledateformat df=new simpledateformat("yyyymmdd");    
        date d = new date(); 
        string timeminus2 = df.format(new date(d.gettime() - 2 * 24 * 60 * 60 * 1000));    
        return timeminus2; 
    } 

    public static void main(string[] args) { 
        ftpfiletransmit ftpfiletransmit = new ftpfiletransmit(); 
        ftpfiletransmit.deletefoldersinftp(); 

//      boolean flag = ftpfiletransmit.createdirectory();  
//      if (flag) {  
//          system.out.println("****** ftp文件夹创建成功 ******");  
//      }  

//      string foldername = ftpfiletransmit.ftppath + "20110809_retransmit/";  
//      byte[] data = new byte[1024];  
//      for (int i = 0; i < data.length; i ++) {  
//          data[i] = 'a';  
//      }  
//      boolean flag = ftpfiletransmit.saveinftp(foldername, "2011080912345678" ,data);  
//      if (flag) {  
//          system.out.println("****** ftp文件夹创建成功 ******");  
//      }  
    } 
}