/** * 永诚测试 * * @param args */ public static void main(String[] args) throws Exception { CompanyInfo companyInfo = new CompanyInfo(); companyInfo.setTransIp("222.66.48.163"); companyInfo.setTransPort("8001"); /** 接口 用户密码 alltrust_USER=fhbx alltrust_PASSWORD=fhbx */ // 生产接口 // companyInfo.setTransIp("www.95552.cc"); // companyInfo.setTransPort("18000"); EdiAction ediAction = new EdiAction(); ediAction.setTimeOut(60 * 1000); ParamsVo paramsVo = new ParamsVo(); paramsVo.setCompanyInfo(companyInfo); paramsVo.setEdiAction(ediAction); paramsVo.setB2CInputInfo(new B2CInputInfo()); ScoketTransmissionServiceImpl scoketTransmissionService = new ScoketTransmissionServiceImpl(); System.out.println(scoketTransmissionService.process(paramsVo, testmessage)); }
@Override public ResponseVo process(ParamsVo paramsVo, String sendStr) throws IOException { ResponseVo responseVo = new ResponseVo(); Long start = System.currentTimeMillis(); try { CompanyInfo companyInfo = paramsVo.getCompanyInfo(); Socket sclient = new Socket(); sclient.connect( new InetSocketAddress( companyInfo.getTransIp(), Integer.valueOf(companyInfo.getTransPort())), 60 * 1000); sclient.setSoTimeout(paramsVo.getEdiAction().getTimeOut()); // 设置编码 ,默认为GBK String sendEnCode = "GBK"; String backEnCode = "GBK"; if (paramsVo.getChannelInfo() != null && StringUtils.isNotBlank(paramsVo.getChannelInfo().getSendEncode())) { sendEnCode = paramsVo.getChannelInfo().getSendEncode(); } if (paramsVo.getChannelInfo() != null && StringUtils.isNotBlank(paramsVo.getChannelInfo().getBackEncode())) { backEnCode = paramsVo.getChannelInfo().getBackEncode(); } OutputStream os = sclient.getOutputStream(); InputStream is = sclient.getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(is, sendEnCode)); // 发送 os.write((sendStr).getBytes(backEnCode)); os.flush(); // 接收 StringBuffer str = new StringBuffer(); String ret = input.readLine(); while (ret != null) { str.append(ret); ret = input.readLine(); } String returnStr = str.toString(); responseVo.setResponseStr(returnStr); responseVo.setSuccessFlag(true); responseVo.setErrorType(ResponseVo.ERRORTYPE_SUCCESS); responseVo.setHttpResponseCode("200"); } catch (Exception e) { logger.error( "{},scoket 请求时出错:{}", paramsVo.getB2CInputInfo().getLogProcessId(), e.getMessage()); logger.error(e.getLocalizedMessage(), e); responseVo.setErrorType(ResponseVo.ERRORTYPE_ERROR); if (e instanceof ConnectException) { responseVo.setErrorType(ResponseVo.ERRORTYPE_CONNET); } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) { responseVo.setErrorType(ResponseVo.ERRORTYPE_TIMEOUT); } responseVo.setException(e); responseVo.setSuccessFlag(false); responseVo.setHttpResponseCode("500"); } finally { logger.info( "{},socket 处理用时 :{}", paramsVo.getB2CInputInfo().getLogProcessId(), System.currentTimeMillis() - start); } return responseVo; }