1. 程式人生 > >https 協議下服務器根據網絡地址下載上傳文件問題

https 協議下服務器根據網絡地址下載上傳文件問題

ipa pla ogg sco except mod public 所有 null

https 協議下服務器根據網絡地址下載上傳文件遇到(PKIX:unable to find valid certification path to requested target 的問題

使用httpclient 所有站點全部信任 不做身份鑒定:

技術分享
 1 public static CloseableHttpClient getHttpClient() throws Exception {
 2         SSLConnectionSocketFactory sslsf = null;
 3         PoolingHttpClientConnectionManager cm = null
; 4 SSLContextBuilder builder = null; 5 builder = new SSLContextBuilder(); 6 // 全部信任 不做身份鑒定 7 builder.loadTrustMaterial(null, new TrustStrategy() { 8 @Override 9 public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
10 return true; 11 } 12 }); 13 sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE); 14 CloseableHttpClient httpClient = HttpClients.custom() 15 .setSSLSocketFactory(sslsf)
16 .setConnectionManager(cm) 17 .setConnectionManagerShared(true) 18 .build(); 19 return httpClient; 20 }
View Code

上傳文件:

技術分享
 1 public static void postParams(String filepath) {
 2         String url = "http://192.188.188.190:8080/dcs.web/upload";//
 3         CloseableHttpClient  httpclient = null;
 4         try {
 5             httpclient = getHttpClient();
 6         }catch (Exception e){
 7             e.printStackTrace();
 8         }
 9         CloseableHttpResponse response = null;
10         String result = null;
11         try {
12             HttpPost httpPost = new HttpPost(url+"upload");
13             MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
14             mEntityBuilder.setCharset(Charset.forName(HttpUtil.ENC_UTF_8));
15             FileBody file = new FileBody(new File(filepath));
16             mEntityBuilder.addPart("file", file);
17             StringBody comment = new StringBody(type, ContentType.APPLICATION_JSON);
18             mEntityBuilder.addPart("convertType", comment);
19             HttpEntity reqEntity = mEntityBuilder.build();
20             httpPost.setEntity(reqEntity);
21             response = httpclient.execute(httpPost);
22             int statusCode = response.getStatusLine().getStatusCode();
23             if (statusCode == HttpStatus.SC_OK) {
24                 HttpEntity resEntity = response.getEntity();
25                 result = EntityUtils.toString(resEntity);
26                 EntityUtils.consume(resEntity);
27                 JSONObject resultJson = JSONObject.fromObject(result);
28                //返回是否成功等信息
29             } else {
30                 exeResultEntity.message = "轉換pdf服務無響應!";
31             }
32         } catch (Exception e) {
33             exeResultEntity.message = "pdf轉換異常,錯誤信息:" + e.getMessage();
34             logger.error("請求DCS轉化pdf服務錯誤!", e);
35         } finally {
36             HttpClientUtils.closeQuietly(httpclient);
37             HttpClientUtils.closeQuietly(response);
38         }
39         return exeResultEntity;
40     }
View Code

下載文件:

技術分享
 1 public static byte[] downloadFileFromNet(String url){
 2         try {
 3             HttpGet httpget = new HttpGet(url);
 4             HttpClient httpClient = getHttpClient();
 5             HttpResponse response = httpClient.execute(httpget);
 6             int statusCode = response.getStatusLine().getStatusCode();
 7             if (statusCode == HttpStatus.SC_OK) {
 8                 HttpEntity entity = response.getEntity();
 9                 InputStream is = entity.getContent();
10                 byte[] fileByte = readInputStream(is);
11                 is.close();
12                 return  fileByte;
13             }
14         }catch (Exception e){
15             e.printStackTrace();
16         }
17         return  null;
18     }
View Code

https 協議下服務器根據網絡地址下載上傳文件問題