zoukankan      html  css  js  c++  java
  • ftp sun jdk自带

    package com.italktv.colnv.stat.util;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.SocketAddress;
    import java.text.SimpleDateFormat;
    
    import sun.net.ftp.FtpClient;
    import sun.net.ftp.FtpProtocolException;
    
    /** * * @author 皮锋 java自带的API对FTP的操作 * */
    public class FtpUtil {
        // FTP客户端   
        private FtpClient ftpClient;
    
        /**
         * * 服务器连接 * * @param ip * 服务器IP * @param port * 服务器端口 * @param user * 用户名 * @param
         * password * 密码 * @param path * 服务器路径
         */
        public void connectServer(String ip, int port, String user, String password, String path) {
            try {
                ftpClient = FtpClient.create();
                try {
                    SocketAddress addr = new InetSocketAddress(ip, port);
                    this.ftpClient.connect(addr);
                    this.ftpClient.login(user, password.toCharArray());
                    System.out.println("login  success!");
                    ftpClient.setBinaryType();
                    ftpClient.enablePassiveMode(true);
                    if (path.length() != 0) {
                        //  把远程系统上的目录切换到参数path所指定的目录            
                        this.ftpClient.changeDirectory(path);
                    }
                } catch (FtpProtocolException e) {
                    e.printStackTrace();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 上传文件 *
         * 
         * @param localFile
         *            * 本地文件 * @param remoteFile * 远程文件
         */
        public void upload(String localFile, String remoteFile) {
            //this.localFilename  =  localFile;      
            //this.remoteFilename  =  remoteFile;      
            OutputStream os = null;
            FileInputStream is = null;
            try {
                try {
                    //  将远程文件加入输出流中  
                    os = this.ftpClient.putFileStream(remoteFile);
                } catch (FtpProtocolException e) {
                    e.printStackTrace();
    
                }
                //  获取本地文件的输入流  
                File file_in = new File(localFile);
                is = new FileInputStream(file_in);        //  创建一个缓冲区  
                byte[] bytes = new byte[1024];
                int c;
                while ((c = is.read(bytes)) != -1) {
                    os.write(bytes, 0, c);
                }
                System.out.println("upload  success");
            } catch (IOException ex) {
                System.out.println("not  upload");
                ex.printStackTrace();
                throw new RuntimeException(ex);
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (os != null) {
                            os.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * 文件下载 *
         * 
         * @param remoteFile
         *            * 远程文件 * @param localFile * 本地文件
         */
        public void download(String remoteFile, String localFile) {
            InputStream is = null;
    
            FileOutputStream os = null;
            try {
                try {
                    //  获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地  
                    is = this.ftpClient.getFileStream(remoteFile);
                } catch (FtpProtocolException e) {
                    e.printStackTrace();
                }
                File file_in = new File(localFile);
                os = new FileOutputStream(file_in);
                byte[] bytes = new byte[1024];
                int c;
                while ((c = is.read(bytes)) != -1) {
                    os.write(bytes, 0, c);
                }
                System.out.println("download  success");
            } catch (IOException ex) {
                System.out.println("not  download");
                ex.printStackTrace();
                throw new RuntimeException(ex);
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (os != null) {
                            os.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * 关闭连接
         */
        public void closeConnect() {
            try {
                this.ftpClient.close();
                System.out.println("disconnect success");
            } catch (IOException ex) {
                System.out.println("not disconnect");
                ex.printStackTrace();
                throw new RuntimeException(ex);
            }
        }
        
         
        public static void main(String agrs[]) {
            FtpUtil fu = new FtpUtil();
            /* * 使用默认的端口号、用户名、密码以及根目录连接FTP服务器 */
            fu.connectServer("upload.dreamstime.com", 21, "369605", "enigma", "/");   
    
    //        String filepath[] = { "AWC1002972G020-1.dwg", "AWC1002974G020-1.cgm" };
    //        String localfilepath[] = { "D:\AWC1002972G020-1.dwg", "D:\AWC1002974G020-1.cgm" };
    //        // 下载 
    //        for (int i = 0; i < filepath.length; i++) {
    //            fu.download(filepath[i], localfilepath[i]);
    //        }
            
            String localfile = "h:\view.jpg";
            String remotefile = "view.jpg";   // 上传 
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss");
            System.out.println(sDateFormat.format(new java.util.Date()));   // new Date()为获取当前系统时间   
            fu.upload(localfile, remotefile);
            System.out.println(sDateFormat.format(new java.util.Date()));   // new Date()为获取当前系统时间  
            fu.closeConnect();
        }
    }
  • 相关阅读:
    用例输入单元测试(3)参数化测试方法
    线程文件[MFC]线程优先级设置,下拉列表框ComboBox,复选框CButton,列表框CListBox的使用
    设置系统SetLocalTime设置系统时间
    页面错误毕业设计之错误集锦(六)
    定义数据类型SQL server中SET ANSI_PADDING对char、varchar、nvarchar的影响
    查找关键字算法:静态查找表(Static Search Table)
    密码注册ASP.NET实现忘记密码
    卡函数or1200基于simplespi的SD卡驱动
    目的地返回POJ 2336 动态规划(DP) Ferry Loading II
    坐标序列hdu 1003 解题报告 Max Sum
  • 原文地址:https://www.cnblogs.com/bigben0123/p/6430042.html
Copyright © 2011-2022 走看看