从 Web 视图下载文件返回的是 HTML 内容,而不是实际文件。

huangapple go评论92阅读模式
英文:

Downloading file from webview returns HTML content not the actual file

问题

我正在使用一个downloadListener从webview下载文件。文件名被正确识别,但是html内容被下载。如果相关的话,我正在尝试下载一个.apk文件。

  1. webView.setDownloadListener(new DownloadListener() {
  2. @Override
  3. public void onDownloadStart(String url, String userAgent,
  4. String contentDisposition, String mimetype,
  5. long contentLength) {
  6. new Thread(new Runnable() {
  7. @Override
  8. public void run() {
  9. InputStream is;
  10. try {
  11. final String filename= URLUtil.guessFileName(url, contentDisposition, mimetype);
  12. Log.d("download","filename: "+filename);//filename is correct
  13. URL u = new URL(url);
  14. HttpURLConnection con = (HttpURLConnection) u.openConnection();
  15. con.setRequestMethod("GET");
  16. con.connect();
  17. is = con.getInputStream();
  18. // Path and File where to download the APK
  19. File apk = new File(getFilesDir(),filename);
  20. FileOutputStream output = new FileOutputStream(apk);
  21. // Save file from URL
  22. byte[] buffer = new byte[1024];
  23. int len = 0;
  24. long total=0;
  25. while ((len = is.read(buffer)) != -1) {
  26. output.write(buffer, 0, len);
  27. total += len;
  28. }
  29. output.close();
  30. is.close();
  31. }
  32. catch (Exception e){
  33. e.printStackTrace();
  34. }
  35. }
  36. }).start();
  37. }
  38. });
英文:

I am using a downloadListener to download a file from a webview. The filename is correctly recognized, however the html content is downloaded. In case it is relevant, I am trying to download an .apk file.

  1. webView.setDownloadListener(new DownloadListener() {
  2. @Override
  3. public void onDownloadStart(String url, String userAgent,
  4. String contentDisposition, String mimetype,
  5. long contentLength) {
  6. new Thread(new Runnable() {
  7. @Override
  8. public void run() {
  9. InputStream is;
  10. try {
  11. final String filename= URLUtil.guessFileName(url, contentDisposition, mimetype);
  12. Log.d("download","filename: "+filename);//filename is correct
  13. URL u = new URL(url);
  14. HttpURLConnection con = (HttpURLConnection) u.openConnection();
  15. con.setRequestMethod("GET");
  16. con.connect();
  17. is = con.getInputStream();
  18. // Path and File where to download the APK
  19. File apk = new File(getFilesDir(),filename);
  20. FileOutputStream output = new FileOutputStream(apk);
  21. // Save file from URL
  22. byte[] buffer = new byte[1024];
  23. int len = 0;
  24. long total=0;
  25. while ((len = is.read(buffer)) != -1) {
  26. output.write(buffer, 0, len);
  27. total += len;
  28. }
  29. output.close();
  30. is.close();
  31. }
  32. catch (Exception e){
  33. e.printStackTrace();
  34. }
  35. }
  36. }).start();
  37. }
  38. });

答案1

得分: 0

更新:解决方案是将cookie和用户代理(user-agent)一并添加到请求属性中。

  1. String cookie = CookieManager.getInstance().getCookie(url);
  2. con.setRequestProperty("cookie", cookie);
  3. con.setRequestProperty("User-Agent", userAgent);
英文:

Update: The solution is to add the cookie as well as the user-agent to the request property.

  1. String cookie = CookieManager.getInstance().getCookie(url);
  2. con.setRequestProperty("cookie",cookie);
  3. con.setRequestProperty("User-Agent",userAgent);

huangapple
  • 本文由 发表于 2020年9月16日 03:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63908567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定