六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 86|回复: 0

jdk.ftp

[复制链接]

升级  44%

34

主题

34

主题

34

主题

秀才

Rank: 2

积分
116
 楼主| 发表于 2013-1-15 02:15:42 | 显示全部楼层 |阅读模式
package ftp.jdk;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import sun.net.TelnetInputStream;import sun.net.ftp.FtpClient;import ftp.FtpUser;/** * JavaJDK自带ftp操作 * @author  cKF29080 * @version  [版本号, Nov 24, 2010] * @see  [相关类/方法] * @since  [产品/模块版本] */public class FtpJdk {    private  FtpClient ftpClient =null;    public boolean loginFtp(String path)    throws Exception {    boolean isSuccess = false;    Properties properties = new Properties();    try {        File file=new  File(path);        InputStream fileInputStream=new FileInputStream(file);        properties.load(fileInputStream);        String ip = properties.getProperty("Ip");        String name = properties.getProperty("Ip");        String ps = properties.getProperty("Ip");       // saveDir = new String(saveDir.getBytes("ISO-8859-1"),"UTF-8");        fileInputStream.close();        ftpClient = new FtpClient(ip);        String  str = ftpClient.getResponseString();              System.out.println("log: "+str);        ftpClient.login(name,ps);        isSuccess = true;    } catch (Exception ex) {        throw new Exception("Connect ftp server error:" + ex.getMessage());    }    return isSuccess;    }   /**    * 下载文件    * @param remotePath ftp目录    * @param localPath 下载本地目录    * @param filename  下载文件名称    * @return [参数说明]     * @return boolean [返回类型说明]    * @exception throws [违例类型] [违例说明]    * @see [类、类#方法、类#成员]    */    public  boolean downloadFile(String remotePath,String localPath, String filename) {    try {                if (remotePath.length() == 0){                        return false;        }        /**更改目录*/        ftpClient.cd(remotePath);        String str = ftpClient.getResponseString() ;           System.out.println("log:"+str) ;             /****设置为 二进制输出***/         ftpClient.binary();          /***获得文件流***/        TelnetInputStream is = ftpClient.get(filename);        /**构造本地路径**/        File file =new File(localPath);        if (!file.exists()) {        file.mkdirs();        }        File file_out = new File(file, filename);        /**输出流**/        FileOutputStream os = new FileOutputStream(file_out);        byte[] bytes = new byte[1024];        int c;        while ((c = is.read(bytes)) != -1) {        os.write(bytes, 0, c);        }        is.close();        os.close();        ftpClient.closeServer();    } catch (FileNotFoundException e) {        e.printStackTrace();        return false;    } catch (IOException e) {        e.printStackTrace();        return false;    }    return true;    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表