如何从 web 视图中的动态 URL 下载 PDF [英] How to download a PDF from a dynamic URL in a webview

查看:51
本文介绍了如何从 web 视图中的动态 URL 下载 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器上 PDF 的 URL 是:

The URL to the PDF on the server is:

https://xyz.abc.com/qwer/leterpdf?Key=43,1&AppId=9520&usecaseID=195

不以 .pdf 扩展名结尾.
如何在 WebView 中下载它?

Not ending with .pdf extension.
How do I download this in a WebView?

推荐答案

根据您在服务器端使用的技术,您可以下载一个文件,强制它传递适当的标头.就像我在我的情况下使用 PHP &这样做-

Depending on the technology you are using on server side, you can download a file forcing it passing appropriate headers. Like i am using PHP in my case & do it like this-

$filepath = "pdfs/android.pdf";
$length = filesize($filepath);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="android.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header("Content-Length: ".$length);

下载监听器-

w.setDownloadListener(new DownloadListener() {
                public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {

                    String fileName,cookie;
                    try {
                        fileName = contentDisposition.replace("inline; filename=", "");
                        fileName = fileName.replaceAll("\"", "");
                        downloadFileAsync(url, fileName);
                    }catch (Exception e){

                    }
                }
            });

下载代码-

private void downloadFileAsync(String url, String filename){

        new AsyncTask<String, Void, String>() {
            String SDCard;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(String... params) {
                try {
                    URL url = new URL(params[0]);
                    HttpURLConnection urlConnection = null;
                    urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);
                    urlConnection.connect();
                    int lengthOfFile = urlConnection.getContentLength();
                    SDCard = Environment.getExternalStorageDirectory() + File.separator + "downloads";
                    int k = 0;
                    boolean file_exists;
                    String finalValue = params[1];
                    do {
                        if (k > 0) {
                            if (params[1].length() > 0) {
                                String s = params[1].substring(0, params[1].lastIndexOf("."));
                                String extension = params[1].replace(s, "");

                                finalValue = s + "(" + k + ")" + extension;
                            } else {
                                String fileName = params[0].substring(params[0].lastIndexOf('/') + 1);
                                String s = fileName.substring(0, fileName.lastIndexOf("."));
                                String extension = fileName.replace(s, "");
                                finalValue = s + "(" + k + ")" + extension;
                            }
                        }
                        File fileIn = new File(SDCard, finalValue);
                        file_exists = fileIn.exists();
                        k++;
                    } while (file_exists);

                    File file = new File(SDCard, finalValue);
                    FileOutputStream fileOutput = null;
                    fileOutput = new FileOutputStream(file, true);
                    InputStream inputStream = null;
                    inputStream = urlConnection.getInputStream();
                    byte[] buffer = new byte[1024];
                    int count;
                    long total = 0;
                    while ((count = inputStream.read(buffer)) != -1) {
                        total += count;
                        //publishProgress(""+(int)((total*100)/lengthOfFile));
                        fileOutput.write(buffer, 0, count);
                    }
                    fileOutput.flush();
                    fileOutput.close();
                    inputStream.close();
                }catch (MalformedURLException e){
                    Log.d(AppConfig.APP_TAG, e.getMessage());
                } catch (ProtocolException e){
                    Log.d(AppConfig.APP_TAG, e.getMessage());
                } catch (FileNotFoundException e){
                    Log.d(AppConfig.APP_TAG, e.getMessage());
                } catch (IOException e){
                    Log.d(AppConfig.APP_TAG, e.getMessage());
                } catch (Exception e){
                    Log.d(AppConfig.APP_TAG, e.getMessage());
                }
                return params[1];
            }
            @Override
            protected void onPostExecute(final String result) {

            }

        }.execute(url, filename);
    }

这篇关于如何从 web 视图中的动态 URL 下载 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆