Liferay 6.2 – Save FTP files into Media Library


Hello. In this article I will show you how to connect to a FTP server, get the files found there and save them into the Media Library of a Liferay Portal.

Liferay doesn’t have any built-in support for FTP, therefore for this particular problem are necessary FTPClient and FTPFile. If you are using Maven to build your project, then you can simply copy-paste the following code into the pom.xml file of your Liferay Plugin and then update the project to get the jars.

The implementation is straight forward, nothing fancy. For the actual save into the library, the following methods were used:

  • Folder creation – addFolder method (DLFolderLocalServiceUtil class)
  • File save – addFileEntry method (DLAppServiceUtil class)

There are other ways to save into the Media Library, such as the methods from the class DLFileEntryLocalServiceUtil. But the one used in this tutorial is much simpler and enough for the task at hand. Although you are welcome to try them all and research further.

The main method deals with the FTP connection and calls the folder method and the actual download method. There’s also a check in place that verifies if the connection exists. Only then the login on the FTP sever and the file download will be called. Also don’t forget to set the server, user and pass values you are going to use. Remember to change the class name for the Log object as well.

The next 2 methods work together. One deals with the creation of the root folder (parentFolder) and the other checks if this folder exists already. If the folder doesn’t exist, then it will be added in the Portal’s media library. Also you will have to set the folder id, folder name and folder description as shown below.

Using the downloadFiles method, the stream will be read and the files will be added one by one in the folder already created. If the file already exists in the Media Library, the appropriate message will be displayed in the console.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.