Connect and Login to FTP Server using Java
** UPDATE: FTP Complete tutorial now available here.
package com.simplecode.com; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPConnectionClosedException; public class FtpLogin { public static void main(String[] args) throws IOException { FTPClient ftpClient = new FTPClient(); boolean result; try { // Connect to the localhost ftpClient.connect("localhost"); // login to ftp server result = ftpClient.login("admin", "admin"); if (result == true) { System.out.println("Successfully logged in!"); } else { System.out.println("Login Fail!"); return; } } catch (FTPConnectionClosedException e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (FTPConnectionClosedException e) { System.out.println(e); } } } }Read More
Apache FTP Server
The Apache Ftp Server is a 100% pure Java FTP server. It’s designed to be a complete and portable FTP server engine solution based on currently available open protocols. Ftp Server can be run standalone as a Windows service or Unix/Linux daemon, or embedded into a Java application.
Features :
- It is a free open source FTP server.
- It includes write permission, user virtual directory, idle time out.
- It provides faster data upload/download.
- It also supports upload/download bandwidth limitations and anonymous login.
- It provides facility to resume broken file transfers for both upload and download files.
- It handles both binary and ASCII data transfers.
- It provides file to be used for storing user data and database too.
- By customizing all FTP messages as needed, it control messages.
- Implicit/explicit SSL/TLS support.
- Feature of IP restriction can ban IPs.
- It provides multithreaded design and multiplatform support
- It allow the users to change the date-time stamp of files through MDTM support.
- Through this server, Custom user manager, IP restrictor, logger can be added easily.
Read More
How to start FTP programming with Java
Although it is possible to use Java networking API (by using the interfaces and classes available in the packages java.net andjavax.net) to write code that communicates with a FTP server, that approach is discouraged because you will have to spend a lot of time on understanding the underlying FTP protocol, implementing the protocol handlers, testing, fixing bugs… and finally you re-invent the wheel! Instead, it’s advisable to look around and pick up some ready-made libraries, and that definitely saves your time! TheApache Commons Net library is an ideal choice for developing FTP based applications. It’s not just for only FTP, but for all common standard internet protocols such as NNTP, SMTP, POP3, Telnet… to name just a few.
With built-in Java API, we can use the java.net.URLConnection to do some FTP operations such as listing files and directories, upload and download. However that is very limited in terms of controllability and flexibility.
So, if you are going to write a Java application that needs to communicate with a FTP server, go on with Apache Commons Net.
|
What is FTP (File Transfer Protocol)?
FTP stands for File Transfer Protocol, which is basically a network protocol used to transfer files from one host to another host over a TCP-based network, such as the Internet.
FTP is built on a client-server architecture and uses separate control and data connections between the client and the server. FTP users may authenticate themselves using a clear-text sign-in protocol, normally in the form of a username and password, but can connect anonymously if the server is configured to allow it. For secure transmission that hides (encrypts) the username and password, and encrypts the content, FTP is often secured with SSL/TLS (“FTPS”). SSH File Transfer Protocol (“SFTP”) is sometimes also used instead.
The first FTP client applications were command-line applications developed before operating systems had graphical user interfaces, and are still shipped with most Windows, Unix, and Linux operating systems. Dozens of FTP clients and automation utilities have since been developed for desktops, servers, mobile devices, and hardware, and FTP has been incorporated into hundreds of productivity applications, such as Web page editors.
To initiate transfer of files with FTP, a program is used called the ‘Client’, which establishes a connection to the remote computer which runs the FTP ‘server’ software. Once the remote connection is established, the client is free to choose any file to send or receive the copy of files. To establish a successful connection to a FTP server, the client requires a username and password that has been set by the administrator of the server.
Read MoreJava Code to Convert XLSX to CSV Files
package com.simplecode.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; class XlsxtoCSV { static void xlsx(File inputFile, File outputFile) { // For storing data into CSV files StringBuffer data = new StringBuffer(); try { FileOutputStream fos = new FileOutputStream(outputFile); // Get the workbook object for XLSX file XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile)); // Get first sheet from the workbook XSSFSheet sheet = wBook.getSheetAt(0); Row row; Cell cell; // Iterate through each rows from first sheet Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { row = rowIterator.next(); // For each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: data.append(cell.getBooleanCellValue() + ","); break; case Cell.CELL_TYPE_NUMERIC: data.append(cell.getNumericCellValue() + ","); break; case Cell.CELL_TYPE_STRING: data.append(cell.getStringCellValue() + ","); break; case Cell.CELL_TYPE_BLANK: data.append("" + ","); break; default: data.append(cell + ","); } } } fos.write(data.toString().getBytes()); fos.close(); } catch (Exception ioe) { ioe.printStackTrace(); } } public static void main(String[] args) { File inputFile = new File("C:\test.xlsx"); File outputFile = new File("C:\output.csv"); xlsx(inputFile, outputFile); } }
To execute the above code you must have the following libraries
- dom4j-1.1.jar
- poi-3.7-20101029.jar
- poi-ooxml-3.7-20101029.jar
- poi-ooxml-schemas-3.7-20101029.jar
- xmlbeans-2.3.0.jar
Let me know if you face any issues. Read More