java http/https后台上传服务器视频到接口
2019-09-17 10:29:28来源:博客园 阅读 ()
java http/https后台上传服务器视频到接口
没有代码得帖子都是耍流氓,废话不说,上代码
//上传视频
public static HttpEntity getMultiDefaultFileEntity(String pathlj,String ticketid) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
File file = new File(pathlj);
builder.addBinaryBody("imageFile", file);
try {
builder.addPart("ticketId", new StringBody(ticketid, Charset.forName("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return builder.build();
}
public String doLivenessAuthByVideoFile( String videoPath, String ticketId) {
if ("".equals(videoPath) || "".equals(videoPath)) {
errorMsg = "参数不能为空!";
return "";
}
String resJsonStr ="";
//跳过https证书验证
SkipHttpsUtil skipHttpsUtil=new SkipHttpsUtil();
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
httpclient = (CloseableHttpClient)skipHttpsUtil.wrapClient();
HttpPost post = new HttpPost(apiHost + "/iap-pcs/pcs/baiduFacelivenessVerify");
HttpEntity dataEntity = getMultiDefaultFileEntity(videoPath,ticketId);// File文件格式上传
post.setEntity(dataEntity);
response = httpclient.execute(post);
resJsonStr = EntityUtils.toString(response.getEntity());
logger.info(resJsonStr.length());
cardInfoJsonObj = JSONObject.fromObject(resJsonStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
skipHttpsUtil类
package com.life.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
/**
* <p>
* ClassName: SkipHttpsUtil
* </p>
* <p>
* Description: httpclient跳过https验证
* </p>
* <p>
* Copyright: Copyright (c) 2019
* </p>
* <p>
* Company: service
* </p>
* @author: maojinqiang
* @CreateDate: 2019-08-26
*/
public class SkipHttpsUtil {
private static Logger logger = Logger.getLogger(SkipHttpsUtil.class);
//绕过证书
public static HttpClient wrapClient() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(
ctx, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(ssf).build();
return httpclient;
} catch (Exception e) {
return HttpClients.createDefault();
}
}
public static void main(String[] args) {
}
}
原文链接:https://www.cnblogs.com/jtwbdm/p/11507952.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash