Search code examples
androidftpuniversal-image-loader

Universal Image Loader, download image from FTP


I'm trying to upload an image from an FTP server using the UIL. To do this, I created a FTPImageDownloader. I use apache-commons-ftpclient. The code is given below:

public class FTPImageDownloader implements ImageDownloader {
public static String FTP_SERVER_HOST = "xx.xx.xxx.xxx";
public static int FTP_SERVER_PORT =xx;
public static String FTP_LOGIN = "xxxxxxx";
public static String FTP_PASSWORD = "xxxxxxx";

@Override
public InputStream getStream(String imageUri, Object extra) throws IOException {


    return getFTPStream(imageUri);
}

public InputStream getFTPStream(String url) throws IOException {
    FTPClient con = null;
    final String imageUrl = url;

    try
    {
        con = new FTPClient();
        con.connect(FTP_SERVER_HOST);

        if (con.login(FTP_LOGIN, FTP_PASSWORD))
        {
            con.enterLocalPassiveMode();
            con.setFileType(FTP.BINARY_FILE_TYPE);

            return con.retrieveFileStream(imageUrl);
        }
    }
    catch (Exception e)
    {
        Log.v("download result","failed");
        e.printStackTrace();
    } finally {
        con.logout();
        con.disconnect();
    }

 return null;
}}

This works, but is very slow. This is especially noticeable when we download multiple images simultaneously.

I think that the reason for the slow work of ImageLoader have that each picture opens a new connection. If this is the case, then tell me how to make the connections like singleton. Thanks.


Solution

  • The problem was the use of an FTP server. We are now migrated to use Http, all is well. The code shown in the issue is worked, you can use it in conjunction with Universal Image Loader, if you want to download images from FTP.