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

  • 相关阅读:
    201621123060《JAVA程序设计》第九周学习总结
    201621123060《JAVA程序设计》第八周学习总结
    网络1712--c语言第二次作业总结
    Linux笔记
    Python-Flask框架之"图书管理系统"项目,附详解源代码及页面效果截图
    CentOS7防火墙firewall
    Linux文件处理三剑客(grep、sed、awk)
    OpenStack、虚拟机以及和当前流行的k8s、Docker四者之间的关系
    反向代理与正向代理的区别
    本地虚拟机在NAT网络连接模式下如何设置才可以访问外网以及使用Xshell远程连接
  • 原文地址:https://www.cnblogs.com/2014330122wwh/p/8358239.html
Copyright © 2011-2022 走看看