Downloading a file using the FTP4CHE library is as easy as a walk in the park as shown in the example below. The example show how to read the stream directly into a string, but can easily be changed into writing the file to a local disk instead.

Code

public class FTPTest implements FTPListener {
 
	private String getFileContent(String path) {
		String content = "";
		try {
			if (path != null && !path.equals("")) {
				String directPath = path.substring(0, path.lastIndexOf("/") + 1);
				String filename = path.substring(path.lastIndexOf("/") + 1);
				FTPFile remoteFile = new FTPFile(directPath, filename);
				content = convertStreamToString(ftpConnection.downloadStream(remoteFile));
			}
		} catch (Exception e) {}
		return content;
	}
 
    	public String convertStreamToString(InputStream is) {
	        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	        StringBuilder sb = new StringBuilder();
	        String line = null;
	        try {
	            while ((line = reader.readLine()) != null) {
	                sb.append(line + "\n");
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                is.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return sb.toString();
	}
 
}
Donate using PayPal
If you found this publication useful to you, then feel free to show your appreaciation towards open-source publications by doing a small donation. It only takes a moment of your time. Any donation is appreaciated – and would also motivate me to release more as open-source. Donations so far – $0 USD – See previous donations.