Android应用:实现JSP页面超链接在WebView内完成PDF下载
Got it, let's get this sorted so your WebView handles PDF downloads directly instead of jumping to the default browser. Here's a step-by-step solution tailored to your setup:
Step 1: Intercept Download Requests with DownloadListener
The core fix is overriding the WebView's default download behavior by attaching a DownloadListener. This lets you catch the PDF download request before it triggers the system browser. Add this code where you initialize your WebView:
webView.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) { // Use Android's built-in DownloadManager to handle the download DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(url)); // Match the WebView's user agent to avoid server-side issues downloadRequest.setUserAgent(userAgent); // Make the download visible in the system Downloads app with notifications downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // Set a save location (customize the path/filename as needed) downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, extractFileName(contentDisposition)); // Enqueue the download request DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); if (downloadManager != null) { downloadManager.enqueue(downloadRequest); } // Optional: Show a quick toast to confirm the download started Toast.makeText(getApplicationContext(), "PDF download started", Toast.LENGTH_SHORT).show(); } });
Step 2: Add a Helper to Extract the PDF Filename
The contentDisposition header from the server usually includes the PDF's filename. Use this helper method to parse it:
private String extractFileName(String contentDisposition) { if (contentDisposition == null) { return "downloaded_pdf.pdf"; // Fallback name if parsing fails } Pattern filenamePattern = Pattern.compile("filename=\"?([^\"]+)\"?"); Matcher matcher = filenamePattern.matcher(contentDisposition); return matcher.find() ? matcher.group(1) : "downloaded_pdf.pdf"; }
Step 3: Declare Required Permissions
Add these permissions to your AndroidManifest.xml to enable internet access and download functionality:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" /> <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
Step 4: Lock Links to the WebView (Optional but Recommended)
Ensure all links (including your JSP-to-JSP hyperlink) stay within the WebView instead of opening the browser by setting a WebViewClient:
webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { view.loadUrl(request.getUrl().toString()); return true; } }); // Also enable basic WebView settings for JSP compatibility WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); // Enable if your JSP uses JavaScript webSettings.setDomStorageEnabled(true);
How This Works
- The
DownloadListenercatches every download request from the WebView, including the PDF link from your target JSP page. - Android's
DownloadManagerhandles the download in the background, which is reliable and integrates with the system's download notifications. - By overriding
shouldOverrideUrlLoading, we keep all navigation within your app's WebView, eliminating the default browser switch entirely.
内容的提问来源于stack exchange,提问作者Satyawan

