Volley的NetworkImageView与FTP上的图像

时间:2015-08-18 10:06:18

标签: ftp android-volley networkimageview

场景是:"有适用于HTTP的适配器,被要求添加FTP支持" - 听起来很合理 有没有办法在同样合理的时间范围内完成? 或者类似的机制/定制的Volley库来交换Volley?

FTP受密码保护,如果这有任何区别。

到目前为止尝试过:

  • HttpClientStack:Scheme 'ftp' not registered
  • HurlStack" libcore.net.url.FtpURLConnection cannot be cast to java.net.HttpURLConnection
  • 尝试通过自定义URLonnection进行隧道传输(多个困难,例如libcore包不公开)
  • 自定义ImageCache,它可以通过Volley的手进行FTP处理(真正的目的是什么?)

1 个答案:

答案 0 :(得分:0)

到目前为止,我只提出了一些事情:覆盖performRequest为ftp链接做一些完全不同的事情。
记住,这里的代码很糟糕,它几乎没有达到我的目的:

public class HurlStackFtp extends HurlStack {

    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {

        String urlString = request.getUrl();
        if (urlString != null && urlString.startsWith("ftp://")) {
            return performFTPRequest(request, additionalHeaders);
        } else {
            return super.performRequest(request, additionalHeaders);
        }
    }


    public HttpResponse performFTPRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
        String url = request.getUrl();
        HashMap<String, String> map = new HashMap<String, String>();
        map.putAll(request.getHeaders());
        map.putAll(additionalHeaders);
        // UrlRewriter not supported

        InputStream input = getStreamFromFTP(url);
        StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
                /*connection.getResponseCode()*/200, /*connection.getResponseMessage()*/"OK");
        BasicHttpResponse response = new BasicHttpResponse(responseStatus);
        response.setEntity(makeEntity(input));
        return response;
    }

    public static InputStream getStreamFromFTP(String url) throws IOException {
        URL parsedUrl = new URL(url);
        URLConnection cn = parsedUrl.openConnection();
        cn.connect();
        return cn.getInputStream();
    }

    private static HttpEntity makeEntity(InputStream inputStream) throws IOException {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(inputStream);
        entity.setContentLength(inputStream.available());
        entity.setContentType("binary/octet-stream"); // sigh...
        return entity;
    }
}