https 协议下服务器根据网络地址下载上传文件问题

Posted wangshunyao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了https 协议下服务器根据网络地址下载上传文件问题相关的知识,希望对你有一定的参考价值。

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 协议下服务器根据网络地址下载上传文件问题的主要内容,如果未能解决你的问题,请参考以下文章

Android网络功能开发——文件下载和上传

106 网络编程实战之基于socketserver实现多用户FTP服务器

Linux下使用libcurl实现FTP单个文件上传下载功能

jmeter之文件上传下载

HTTP协议

xshell Linux 上传文件