通用图像加载器,从 FTP 下载图像

Universal Image Loader, download image from FTP

我正在尝试使用 UIL 从 FTP 服务器上传图像。为此,我创建了一个 FTPImageDownloader。我使用 apache-commons-ftpclient。代码如下:

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

这有效,但速度很慢。当我们同时下载多张图片时,这一点尤其明显。

我认为ImageLoader工作缓慢的原因是每张图片打开一个新连接。如果是这种情况,请告诉我如何像单例一样建立连接。谢谢

对像这样的多个网络调用使用队列https://github.com/path/android-priority-jobqueue

尝试在您的 AndroidManifest.xml 文件中添加互联网权限:

uses-permission android:name="android.permission.INTERNET"

问题是使用 FTP 服务器。我们现在迁移到使用Http,一切都很好。问题中显示的代码有效,如果你想从 FTP.

下载图像,你可以将它与 Universal Image Loader 结合使用