zoukankan      html  css  js  c++  java
  • 实现ftp单个文件上传

    首先配置ftp.properties

    ftpip=10.192.88.60
    ftpuser=eftp
    ftppassword=eftp
    #localpath=/home/eftp

    编写读ftp.properties文件类;

    public class ReadFtpProperties{

    private InputStream is;

    private Properties properties;

    public ReadFtpProperties() {

    is = this.getClass().getResourceAsStream("ftp.properties");

     properties = new Properties();

    try {

    properties.load(is);

    } catch (IOException e) {

    System.out.println("配置文件不存在..");

    e.printStackTrace();

    } finally {

    if (null != is) {

    try { is.close();

    } catch (IOException e) {

    System.out.println("关闭流失败.."); e.printStackTrace();

    }

    }

    }

    }

    public String getIp() {

    // 获取ftp服务器的ip地址 return properties.getProperty("ftpIp"); }

    public String getUser() {

    // 获取ftp登录用户名 return properties.getProperty("ftpuser");

    }

    public String getPwd() {

    // 获取ftp服务器的登录密码 return properties.getProperty("ftppassword");

    }

    public String getRemotePath() {

    // 获取ftp服务器的存放文件的目录 return properties.getProperty("#localpath"); }

    }

    ftp文件下载类

    public class FtpDownload {

    /**
    * FTP下载单个文件
    */
    public static boolean testDownload(String remoteFile,String localFile) {
    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;
    boolean bool;
    boolean bool1;
    try {
    String ftpIP=IWapContext.getProperty("POSFtp.IP", "127.0.0.1");
    String ftpUsername=ReadFtpProperties.getUser();
    String ftpPassword=ReadFtpProperties.getPwd();
    String remoteFile1=ReadFtpProperties.getRemotePath();
    ftpClient.connect(ftpIP);
    bool1=ftpClient.login(ftpUsername,ftpPassword);

    String remoteFileName = remoteFile1+remoteFile;
    fos = new FileOutputStream(localFile);

    ftpClient.setBufferSize(1024);
    //设置文件类型(二进制)
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    bool=ftpClient.retrieveFile(remoteFileName, fos);
    } catch (IOException e) {
    e.printStackTrace();
    throw new RuntimeException("FTP客户端出错!", e);
    } finally {
    IOUtils.closeQuietly(fos);
    try {
    ftpClient.disconnect();
    } catch (IOException e) {
    e.printStackTrace();
    throw new RuntimeException("关闭FTP连接发生异常!", e);
    }
    }
    return bool&&bool1;
    }
    }

  • 相关阅读:
    codeforces570D Tree Requests
    codeforces600E Lomsat gelral
    BZOJ2001 [Hnoi2010]City 城市建设
    BZOJ2565 最长双回文串
    BZOJ4031 [HEOI2015]小Z的房间
    BZOJ2467 [中山市选2010]生成树
    SPOJ104 HIGH
    爆零系列—补题A
    DP一直是自己的弱势 开始练滚动数组——HDOJ4502
    HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法
  • 原文地址:https://www.cnblogs.com/2014330122wwh/p/8358239.html
Copyright © 2011-2022 走看看