【问题标题】:Universal Image Loader, download image from FTP通用图像加载器,从 FTP 下载图像
【发布时间】:2015-12-11 15:57:47
【问题描述】:

我正在尝试使用 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 工作缓慢的原因是每张图片都打开了一个新的连接。如果是这种情况,那么请告诉我如何像单例一样建立连接。谢谢。

【问题讨论】:

    标签: android ftp universal-image-loader


    【解决方案1】:

    将队列用于多个网络调用,例如https://github.com/path/android-priority-jobqueue

    【讨论】:

    • 感谢您的回答,但 Universal Image Loader lib(我使用)可以处理多个下载图像。问题出在 FTPImageDownloader 的实现上,是在获取 InputStream 图像。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      问题在于使用了 FTP 服务器。我们现在迁移到使用 Http,一切都很好。问题中显示的代码是有效的,如果你想从 FTP 下载图像,你可以将它与 Universal Image Loader 结合使用。

      【讨论】:

        猜你喜欢
        • 2012-12-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        • 2017-04-07
        • 2015-03-22
        • 1970-01-01
        • 2011-11-22
        • 1970-01-01
        相关资源
        最近更新 更多