• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

java.io.IOException: Error writing to server

private Map invokePostFile(String ip,int port,String uri,boolean allowNoReponse,Map params,File f,String...requKey) throws Exception{
        if(StringUtils.isBlank(ip) || port == 0){
            throw new UEException("未获取到调度引擎地址");
        }
        final String BOUNDARY = UUID.randomUUID().toString();
        final String PREFFIX = "--", LINEND = "\r\n";
        String address = "https://"+ip+":"+port+uri;
        HttpURLConnection connection = (HttpURLConnection)new URL(address).openConnection();
        DataOutputStream outputStream = null;
        try {
            connection.setConnectTimeout(TIMEOUT);
            connection.setReadTimeout(TIMEOUT);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setInstanceFollowRedirects(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("connection", "keep-alive");
            connection.setRequestProperty("Charset", "UTF-8");
            connection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + BOUNDARY);
            StringBuilder sb = new StringBuilder();
            for (Map.Entry entry : params.entrySet()) {
                sb.append(PREFFIX);
                sb.append(BOUNDARY);
                sb.append(LINEND);
                sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
                sb.append("Content-Type: text/plain; charset=UTF-8" + LINEND);
                sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
                sb.append(LINEND);
                sb.append(entry.getValue());
                sb.append(LINEND);
            }
            //参数
            connection.setRequestProperty(SIGN_KEY,SecretUtil.encryptHMAC(SecretUtil.HMAC_SHA256,sb.toString().getBytes("UTF8"),ENGINE_PASSWORD));
            //如果是https
            if(connection instanceof HttpsURLConnection){
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null,new TrustManager[]{new X509TrustManager(){
                    @Override
                    public void checkClientTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {
                    }
                    @Override
                    public void checkServerTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {
                    }
                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }},new java.security.SecureRandom());
                ((HttpsURLConnection)connection).setSSLSocketFactory(sc.getSocketFactory());
                ((HttpsURLConnection)connection).setHostnameVerifier(new HostnameVerifier(){
                    @Override
                    public boolean verify(String arg0, SSLSession arg1) {
                        return true;
                    }
                });
            }
            try {
                connection.connect();
            } catch (Exception e) {
                LOG.error("引擎连接失败:"+address,e);
                throw new UEException("引擎连接失败",e);
            }
            //写入参数
           outputStream = new DataOutputStream(connection.getOutputStream());
           outputStream.write(sb.toString().getBytes("UTF8"));
            if(f !=null && f.exists()){
                String fileName = f.getName();
                StringBuilder sb1 = new StringBuilder();
                sb1.append(PREFFIX);
                sb1.append(BOUNDARY);
                sb1.append(LINEND);
                sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + LINEND);
                sb1.append("Content-Type: application/octet-stream;chartset=UTF-8" + LINEND);
                sb1.append(LINEND);
                // 写入到输出流中
                outputStream.write(sb1.toString().getBytes());
                DataInputStream is = null;
                try {
                    // 将文件读入输入流中
                    is = new DataInputStream(new FileInputStream(f));;
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = is.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, len);
                    }
                    //footer
                    outputStream.write((PREFFIX + BOUNDARY + PREFFIX + LINEND).getBytes("UTF8"));
                    outputStream.flush();
                } catch (Exception e) {
                    throw e;
                }finally{
                    IOUtils.closeQuietly(is);
                }
            }
            //读取结果
            return parseRepose(connection,requKey,allowNoReponse);
        }catch(Exception e){
            if(e instanceof UEException){
                throw e;
            }
            throw new UEException("引擎执行失败",e);
        }finally{
            IOUtils.closeQuietly(outputStream);
            connection.disconnect();
        }
    }
错误信息:
154003bopcejj4ps4s2gzz.jpg

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031168位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!