zoukankan      html  css  js  c++  java
  • java ftp上传文件

    //这边path指的是相对路径,需要切换到,path目录,然后
    public static void upload(String server, int port, String user, String pwd, List<String> sourcePaths, List<String> desPaths, String filename) {
    if (sourcePaths.size() == 0 || desPaths.size() == 0) {
    return;
    }
    if (sourcePaths.size() != desPaths.size()) {
    System.out.println("源文件地址与目标地址不匹配!");
    }
    FTPClient client = new FTPClient();
    FileInputStream inputStream = null;
    try {
    client.connect(server, port);
    client.login(user, pwd);
    for (int i = 0; i < sourcePaths.size(); i++) {
    inputStream = new FileInputStream(sourcePaths.get(i));
    client.changeWorkingDirectory(desPaths.get(i));
    boolean b = client.storeFile(filename, inputStream);
    if (b == false) {
    System.out.println("上传文件:" + sourcePaths.get(i) + "失败!目标主机:" + server);
    }
    }
    client.logout();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (inputStream != null) {
    inputStream.close();
    }
    client.disconnect();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>3.3</version>
            </dependency>
  • 相关阅读:
    java算法---五家共井
    JAVA实用工具--javamail
    java基础篇---HTTP协议
    java基础解疑!!!
    java基础解疑!!
    jsp----标签编程(JSTL)
    jsp----EL表达式
    java基础篇---线程问题
    算法篇---java经典问题!!!
    java基础篇---注解(Annotation)
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/10468584.html
Copyright © 2011-2022 走看看