private Map<String,Object> invokePostFile(String ip,int port,String uri,boolean allowNoReponse,Map<String,Object> 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<String, Object> 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(); } }错误信息:
添加回答
举报
0/150
提交
取消