How do you upload a file to an FTP server? How do you upload a file to an FTP server? java java

How do you upload a file to an FTP server?


Use the FTPClient Class from the Apache Commons Net library.

This is a snippet with an example:

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

Snippet taken from http://www.kodejava.org/examples/356.html


I have used the EDT FTP package, a free GPL library for FTP in Java: http://www.enterprisedt.com/products/edtftpj/overview.html

Here is a code sample, from the Demo.java class they provide:

ftp = new FTPClient();ftp.setRemoteHost("hostname");// connectftp.connect();// loginftp.login("user", "password");// set up passive ASCII transfersftp.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 serverftp.get("test.txt" + ".copy", "test.txt");// delete file from serverftp.delete("test.txt");// Shut down client                ftp.quit();