博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ftp sun jdk自带
阅读量:4687 次
发布时间:2019-06-09

本文共 5791 字,大约阅读时间需要 19 分钟。

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();    }}

 

转载于:https://www.cnblogs.com/bigben0123/p/6430042.html

你可能感兴趣的文章
Navicat远程连接阿里云服务器的mysql
查看>>
https soap链接示例
查看>>
Git Day02,工作区,暂存区,回退,删除文件
查看>>
学前班
查看>>
关于自关联1
查看>>
hdu-1814(2-sat)
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
Linux shell 命令判断执行语法 ; , && , ||
查看>>
vim代码格式化插件clang-format
查看>>
RTP Payload Format for Transport of MPEG-4 Elementary Streams over http
查看>>
Java环境变量设置
查看>>
【JBPM4】判断节点decision 方法3 handler
查看>>
filter 过滤器(监听)
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
杭电3466————DP之01背包(对状态转移方程的更新理解)
查看>>
kafka中的消费组
查看>>
python--注释
查看>>
SQL case when else
查看>>