【问题标题】:How do you upload a file to an FTP server?如何将文件上传到 FTP 服务器?
【发布时间】:2011-09-25 12:58:14
【问题描述】:

我创建了一个函数来从我有权访问的 FTP 服务器下载文件。如何将文件上传回 FTP 服务器?

下面是我使用的download_files方法:

public static void download_files(String un, String pw, String ip, String dir, String fn, String fp){

    URLConnection con;
    BufferedInputStream in = null;
    FileOutputStream out = null;

    try{
        URL url = new URL("ftp://"+un+":"+pw+"@"+ip+"/"+dir+"/"+fn+";type=i");
        con = url.openConnection();
        in = new BufferedInputStream(con.getInputStream());
        out = new FileOutputStream(fp+fn);

        int i = 0;
        byte[] bytesIn = new byte[1024];

        while ((i = in.read(bytesIn)) >= 0) {
            out.write(bytesIn, 0, i);
        }

    }catch(Exception e){
        System.out.print(e);
        e.printStackTrace();
        System.out.println("Error while FTP'ing "+fn);
    }finally{
        try{
            out.close();
            in.close();
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("Error while closing FTP connection");
        }
    }
}

【问题讨论】:

标签: java upload ftp download


【解决方案1】:

使用Apache Commons Net 库中的FTPClient 类。

这是一个带有示例的 sn-p:

FTPClient client = new FTPClient();
FileInputStream fis = null;

try {
    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    //
    // Create an InputStream of the file to be uploaded
    //
    String filename = "Touch.dat";
    fis = new FileInputStream(filename);

    //
    // Store file to server
    //
    client.storeFile(filename, fis);
    client.logout();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fis != null) {
            fis.close();
        }
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

摘自http://www.kodejava.org/examples/356.html的片段

【讨论】:

【解决方案2】:

也请查看FTP4J...

【讨论】:

    【解决方案3】:

    我使用了 EDT FTP 包,这是一个免费的 Java FTP GPL 库:http://www.enterprisedt.com/products/edtftpj/overview.html

    这是一个代码示例,来自他们提供的 Demo.java 类:

    ftp = new FTPClient();
    ftp.setRemoteHost("hostname");
    
    // connect
    ftp.connect();
    
    // login
    ftp.login("user", "password");
    
    // set up passive ASCII transfers
    ftp.setConnectMode(FTPConnectMode.PASV);
    ftp.setType(FTPTransferType.ASCII);
    
    // get directory and print it to console            
    String[] files = ftp.dir(".", true);
    for (int i = 0; i < files.length; i++)
        log.debug(files[i]);
    
    // copy file to server 
    ftp.put("test.txt", "test.txt");
    
    // copy file from server
    ftp.get("test.txt" + ".copy", "test.txt");
    
    // delete file from server
    ftp.delete("test.txt");
    
    // Shut down client                
    ftp.quit();
    

    【讨论】:

      【解决方案4】:

      看看apache-commons-net,他们有一些 FTP 工具可以帮助你!

      【讨论】:

        猜你喜欢
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-05
        相关资源
        最近更新 更多