httpurl下载

小咪咪 2022-12-17 07:20 254阅读 0赞
  1. httpurl下载
  2. File descFile = new File(MainActivity.this.getCacheDir(), fileName);
  3. // Android 4.0 之后不能在主线程中请求HTTP请求
  4. if (downflag){
  5. download(descFile);
  6. }
  7. new Thread(new Runnable(){
  8. @Override
  9. public void run() {
  10. try {
  11. HttpURLConnection conn = null;
  12. trustAllHosts();
  13. URL httpUrl = new URL(url);
  14. HttpsURLConnection https = (HttpsURLConnection)httpUrl.openConnection();
  15. if (httpUrl.getProtocol().toLowerCase().equals("https")) {
  16. https.setHostnameVerifier(new HostnameVerifier() {
  17. public boolean verify(String hostname, SSLSession session) {
  18. return true;
  19. }
  20. });
  21. conn = https;
  22. } else {
  23. conn = (HttpURLConnection)httpUrl.openConnection();
  24. }
  25. conn.setRequestProperty("Cookie",cookieStr);
  26. conn.setRequestProperty("Host","mail.ejianlong.com");
  27. // 设置连接主机超时时间
  28. conn.setConnectTimeout(5 * 1000);
  29. //设置从主机读取数据超时
  30. conn.setReadTimeout(5 * 1000);
  31. // 设置是否使用缓存 默认是true
  32. conn.setUseCaches(true);
  33. // 设置为Post请求
  34. conn.setRequestMethod("GET");
  35. // conn.connect();
  36. // 判断请求是否成功
  37. if (conn.getResponseCode() == 200) {
  38. FileOutputStream fos = new FileOutputStream(descFile);;
  39. byte[] buffer = new byte[1024];
  40. int len;
  41. InputStream inputStream = conn.getInputStream();
  42. while ((len = inputStream.read(buffer)) != -1) {
  43. // 写到本地
  44. fos.write(buffer, 0, len);
  45. }
  46. Log.e("文件下载完成", "文件下载完成");
  47. // Intent handlerIntent = new Intent(Intent.ACTION_VIEW);
  48. downflag = true;
  49. handlerIntent.setDataAndType(uri, mimeType);
  50. // // 以下固定写法
  51. // final Intent intent = new Intent(Intent.ACTION_VIEW,
  52. // uri);
  53. // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
  54. // | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  55. // startActivity(intent);
  56. startActivity(handlerIntent);
  57. } else {
  58. Log.e("文件下载失败", "文件下载失败");
  59. }
  60. // 关闭连接
  61. conn.disconnect();
  62. } catch (Exception e) {
  63. Log.e("文件下载失败", e.toString());
  64. }
  65. }
  66. private void trustAllHosts() {
  67. final String TAG = "trustAllHosts";
  68. // Create a trust manager that does not validate certificate chains
  69. TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
  70. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  71. return new java.security.cert.X509Certificate[] {};
  72. }
  73. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  74. Log.i(TAG, "checkClientTrusted");
  75. }
  76. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  77. Log.i(TAG, "checkServerTrusted");
  78. }
  79. } };
  80. // Install the all-trusting trust manager
  81. try {
  82. SSLContext sc = SSLContext.getInstance("TLS");
  83. sc.init(null, trustAllCerts, new java.security.SecureRandom());
  84. HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }).start();

发表评论

表情:
评论列表 (有 0 条评论,254人围观)

还没有评论,来说两句吧...

相关阅读