如何从Android App中受保护的Web文件夹下载文件?

时间:2011-09-09 03:45:05

标签: android authentication download

我正在寻找有关如何将图像文件从受密码保护的文件夹下载到我的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

1 个答案:

答案 0 :(得分:2)

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

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