您现在的位置:首页 >> 基础 >> Java编程 >> 内容

java http/https后台上传服务器视频到接口

时间:2019-9-16 10:00:55

  核心提示:没有代码得帖子都是耍流氓,废话不说,上代码//上传视频 public static HttpEntity getMultiDefaultFileEntity(String pathlj,String...

没有代码得帖子都是耍流氓,废话不说,上代码

 

//上传视频
    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) {

    }
}

Java免费学习   Java自学网 http://www.javalearns.com

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java

作者:不详 来源:网络
    你是从哪里知道本网站的?
  • 网友介绍的
  • 百度搜索的
  • Google搜索的
  • 其它搜索过来的
  • 网址输错了进来的
  • 太忙了不记得了
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • java学习网(www.javalearns.com) © 2014 版权所有 All Rights Reserved.
  • Email:javalearns@163.com 站长QQ:1356121699 晋ICP备14003680号-3
  • java学习网部分内容来自网络或网友发布,如侵犯了您利益,请发邮件至:javalearns@126.com,我们尽快处理!
  • Java学习网
  • 网站统计
  • 晋公网安备 14042902000001号