论坛风格切换切换到宽版
  • 760阅读
  • 0回复

[Java]在线文件转Base64及解决地址为https [复制链接]

上一主题 下一主题
离线路灯
 

发帖
589
金币
4237
威望
1803
股份
208
关键词: java

  1.    /**
  2.      * 获取在线图片/音频BASE64格式
  3.      *
  4.      * @param filePath 图片/音频路径
  5.      * @return 图片base64位
  6.      */
  7.     public String base64File(String filePath) {
  8.         String str = "";
  9.         try {
  10.             URL url = new URL(filePath);
  11.             //SSL请求地址
  12.             trustAllHttpsCertificates();
  13.             HttpsURLConnection.setDefaultHostnameVerifier(hv);
  14.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  15.             DataInputStream in = new DataInputStream(conn.getInputStream());
  16.             ByteArrayOutputStream out = new ByteArrayOutputStream();
  17.             byte[] buffer = new byte[1024 * 4];
  18.             int n = 0;
  19.             while ((n = in.read(buffer)) != -1) {
  20.                 out.write(buffer, 0, n);
  21.             }
  22.             byte[] data = out.toByteArray();
  23.             BASE64Encoder encoder = new BASE64Encoder();
  24.             str = encoder.encode(data);
  25.             str = str.replaceAll("\r|\n", "");
  26.         } catch (Exception e) {
  27.             throw new ValidatorException(StatusCodeEnum.UNKNOW, "媒体资源获取失败!");
  28.         }
  29.         return str;
  30.     }
  31.     /**
  32.      * 兼容SSL地址请求
  33.      */
  34.     HostnameVerifier hv = new HostnameVerifier() {
  35.         @Override
  36.         public boolean verify(String urlHostName, SSLSession session) {
  37.             System.out.println("Warning: URL Host: " + urlHostName + " vs. "
  38.                     + session.getPeerHost());
  39.             return true;
  40.         }
  41.     };
  42.     private static void trustAllHttpsCertificates() throws Exception {
  43.         javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
  44.         javax.net.ssl.TrustManager tm = new SllManager();
  45.         trustAllCerts[0] = tm;
  46.         javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
  47.                 .getInstance("SSL");
  48.         sc.init(null, trustAllCerts, null);
  49.         javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
  50.                 .getSocketFactory());
  51.     }
  52.     static class SllManager implements javax.net.ssl.TrustManager,
  53.             javax.net.ssl.X509TrustManager {
  54.         @Override
  55.         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  56.             return null;
  57.         }
  58.         public boolean isServerTrusted(
  59.                 java.security.cert.X509Certificate[] certs) {
  60.             return true;
  61.         }
  62.         public boolean isClientTrusted(
  63.                 java.security.cert.X509Certificate[] certs) {
  64.             return true;
  65.         }
  66.         @Override
  67.         public void checkServerTrusted(
  68.                 java.security.cert.X509Certificate[] certs, String authType)
  69.                 throws java.security.cert.CertificateException {
  70.             return;
  71.         }
  72.         @Override
  73.         public void checkClientTrusted(
  74.                 java.security.cert.X509Certificate[] certs, String authType)
  75.                 throws java.security.cert.CertificateException {
  76.             return;
  77.         }
  78.     }






 
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个