【问题标题】:Downloading a pdf generated by a plugin in a webview下载由 webview 中的插件生成的 pdf
【发布时间】:2018-05-14 14:04:24
【问题描述】:

我正在创建一个使用通用 Android WebView 应用程序来访问网页的应用程序。此网页有一些可下载的 pdf,仅供登录用户使用。 内置的下载管理器不起作用,因为 pdf 是由插件生成的,并在 http 响应中发送。 我尝试自己实现连接,调整此代码http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-url

它工作正常,连接建立得很好,但文件没有下载,因为显然收到的 Content-Length 是-1。可能是什么问题?

代码如下:

public class HttpDownloadUtility extends AsyncTask<String, Integer, String> {
private static final int BUFFER_SIZE = 4096;

/**
 * Downloads a file from a URL
 * @param fileURL HTTP URL of the file to be downloaded
 * @param saveDir path of the directory to save the file
 * @throws IOException
 */
public String downloadFile(String fileURL, String saveDir) throws IOException {

    String fileName = "";
    URL url = new URL(fileURL);

    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setRequestProperty("Cookie", android.webkit.CookieManager.getInstance().getCookie("http://mywebsite.com"));

    httpConn.connect();

    int responseCode = httpConn.getResponseCode();

    // always check HTTP response code first
    if (responseCode == HttpURLConnection.HTTP_OK) {

        String disposition = httpConn.getHeaderField("Content-Disposition");
        String contentType = httpConn.getContentType();
        int contentLength = httpConn.getContentLength();

        if (disposition != null) {
            // extracts file name from header field

            int index = disposition.indexOf("filename=");
            if (index > 0) {
                fileName = disposition.substring(index + 10,
                        disposition.length() - 1);
            }
        } else {
            // extracts file name from URL
            fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                    fileURL.length());
        }

        System.out.println(url.toString());
        System.out.println("Content-Type = " + contentType);
        System.out.println("Content-Disposition = " + disposition);
        System.out.println("Content-Length = " + contentLength);
        System.out.println("fileName = " + fileName);

        // opens input stream from the HTTP connection
        InputStream inputStream = httpConn.getInputStream();
        String saveFilePath = saveDir + File.separator + fileName;

        // opens an output stream to save into file
        FileOutputStream outputStream = new FileOutputStream(saveFilePath);

        int bytesRead = -1;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.close();
        inputStream.close();

        System.out.println("File downloaded");



    } else {
        System.out.println("No file to download. Server replied HTTP code: " + responseCode);
    }
    httpConn.disconnect();

    return fileName;
}

@Override
protected String doInBackground(String... url) {

    String downloadedFile = "";

    try{
        downloadedFile = downloadFile(url[0], Environment.DIRECTORY_DOWNLOADS);
    }catch(Exception e){
       System.out.println(e.toString());
    }
    return downloadedFile;
}
}

【问题讨论】:

    标签: android pdf webview download


    【解决方案1】:

    我刚刚发现可以使用我需要的 cookie 来配置 DownloadManager。这是任何人需要的代码(我不再使用我在问题中提到的类)。

    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "mypdf.pdf");
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(1);
                    request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url));
                    DownloadManager manager = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                    manager.enqueue(request);
    

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      相关资源
      最近更新 更多