分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

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

发布时间:2023-09-06 01:09责任编辑:胡小海关键词:http

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 协议下服务器根据网络地址下载上传文件问题

原文地址:http://www.cnblogs.com/wangshunyao/p/7484076.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved