【问题标题】:How to download a file from a protected web folder in Android App?如何从 Android App 中受保护的 Web 文件夹下载文件?
【发布时间】:2011-09-09 03:45:05
【问题描述】:

我正在寻求有关如何将图像文件从受密码保护的文件夹下载到我的 Android 应用程序的帮助。我拥有的代码使用 URLConnection 和 getInputStream/BufferedInputStream 但我看不到如何在其中获取用户名/密码身份验证。我看到 HttpClient 有 UsernamePasswordCredentials - 但我不知道如何使用 HttpClient 下载文件,所以这对我没有多大帮助。

这是我目前找到的代码,如何使用它下载文件?

public class ClientAuthentication {

    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("localhost", 443),
                    new UsernamePasswordCredentials("username", "password"));

            HttpGet httpget = new HttpGet("https://localhost/protected");

            System.out.println("executing request" + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
}

或者,我有这个用于下载文件的代码——我将如何向它添加凭据:

http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

谢谢!

编辑:好吧,在这里没有得到太多帮助。我找到了这个答案,我将尝试为我的目的返工:Download a file with DefaultHTTPClient and preemptive authentication

【问题讨论】:

  • 你有没有试过把它放到像https://user:password@host.domain/page/这样的URL中?
  • 是的,我做到了,它给了我文件 io 异常。不过,这在 iOS 中有效!
  • 好问题,我开始认为这实际上是一个文件 i/o 问题——我可能对我使用的代码示例太信任了!但我更喜欢使用 HttpClient 身份验证代码而不是内联身份验证,所以我看看我是否可以让它工作。

标签: android authentication download


【解决方案1】:

嗯,这是我想出的代码,似乎有效。我发布它是因为我在网络上的任何地方都很难找到这样的代码。我欢迎有关如何改进它的建议:)

public void downloadHTTPC(Activity act, String imageURL, String fileName) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        String pathDir = act.getExternalFilesDir(null).toString() + "/" + fileName;
        File file = new File(pathDir);
        long startTime = System.currentTimeMillis();
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(null, -1),
                new UsernamePasswordCredentials("user", "password"));

        HttpGet httpget = new HttpGet(IMGURL + imageURL);

        System.out.println("executing request" + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            InputStream is = entity.getContent();

            BufferedInputStream bis = new BufferedInputStream(is);

            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).
             */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            /* Convert the Bytes read to a Stream. */
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            Log.d("ImageManager", "download ready in"
                    + ((System.currentTimeMillis() - startTime) / 1000)
                    + " sec");
        }

        //EntityUtils.consume(entity);
    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2014-01-22
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2015-07-30
    • 1970-01-01
    相关资源
    最近更新 更多