JAVA编码(16)——通过HTTP传输协议传输数据

public static boolean xml2accby(String filestr) {
              System.out.println("开始往财务系统传xml文件");
              String backMsg = null;//从财务系统返回过来的信息
              InputStreamReader isr = null;
              HttpClient httpClient = null;
              PostMethod postMethod = null;
              try {
                      //模拟http请求传递文件到财务系统
                      httpClient = new HttpClient();
                      //读取src下的location.properties中财务系统路径
                      postMethod = new PostMethod(LoadLocationProperties.getPropertiesValue("/location.properties", "accby"));
                     System.out.println("财务系统接口路径"+LoadLocationProperties.getPropertiesValue("/location.properties", "accby"));
                    postMethod.setRequestHeader("accept", "*/*");
                    postMethod.setRequestHeader("connection", "Keep-Alive");
                    postMethod.setRequestHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
                    postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
                    postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");
                    } catch (Exception e) {
                                 e.printStackTrace();
                                 setError("GenvoucherUtils", "loanintfdata2xml", "财务系统路径链接访问不了,请确定财务系统是正常启动状态");
                                  return false;
                    }
                   //读取接口文件
                   File file = new File(filestr);
                   if(!file.exists()){
                            setError("GenvoucherUtils", "loanintfdata2xml", filestr+"文件不存在,请重新生成");
                            return false;
                   }
                   //使用流把数据文件传过去
                  BufferedReader reader = null;
                  StringBuffer sb = null;
                  BufferedReader br = null;
                 String temp = null;
                 int chari;
                 try {
                           reader = new BufferedReader(new FileReader(file));
                           StringBuffer buffer = new StringBuffer();
                           while((temp=reader.readLine())!= null){
                           buffer.append(temp+"\n\r");
                 }
               NameValuePair[] data = {new NameValuePair("fileContent", buffer.toString())};
               postMethod.setRequestBody(data);// 将表单的值放入postMethod中
               int statusCode = httpClient.executeMethod(postMethod);// 执行postMethod
               // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
             if (statusCode == HttpStatus. SC_NOT_FOUND) {
                        setError("GenvoucherUtils", "xml2accby", "请确认财务系统在启动!!");
                        return false;
              }
              isr = new InputStreamReader(postMethod.getResponseBodyAsStream(), postMethod.getRequestCharSet());
              br = new BufferedReader(isr);
              sb = new StringBuffer();
              while((temp=br.readLine())!= null){
                          sb.append(temp);
               }
              br.close();
              reader.close();
              postMethod.releaseConnection();
   
             } catch (Exception e) {
                          e.printStackTrace();
                          setError("GenvoucherUtils", "loanintfdata2xml", "链接财务系统和向财务系统传文件异常,请联系运维解决");
                        return false;
          }
          backMsg = sb.toString();
          if(backMsg.contains("单位")&&backMsg.contains("会计月度")&&backMsg.contains("凭证号")){
                           voucherNo=backMsg.substring(backMsg.lastIndexOf(":")+1,backMsg.lastIndexOf(";"));
          }else{
                         voucherNo="$$$$"+backMsg;
         }
                        System.out.println(backMsg);
                        return true;
        }

 

==============================================================

public static String HttpPostRoadWealth(String urls, String parameter, String fundInterType) throws ClientProtocolException, IOException{

  System.out.println(urls +"起始时间:"+new Date());

  String url =Config.getyilucaifuPath()+urls+".html";

  System.out.println("请求路径:" + url);

  System.out.println("请求参数:"+ parameter);

  DefaultHttpClient httpclient = null;

  try {

    httpclient = new DefaultHttpClient();

    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);//连接时间

    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);//数 据传输时间

    HttpPost httppost = new HttpPost(url);

    StringEntity reqEntity = new StringEntity(parameter);// 构造最简单的字符串数据

    reqEntity.setContentType("application/x-www-form-urlencoded");// 设置类型

    httppost.setEntity(reqEntity);// 设置请求的数据

    HttpResponse response = httpclient.execute(httppost);// 执行

    HttpEntity entity = response.getEntity();

    BufferedReader reader = new BufferedReader(new InputStreamReader(

    entity.getContent(), "UTF-8"));// 解析显示结果

    String line = null;

    StringBuilder buff = new StringBuilder();

    while ((line = reader.readLine()) != null) {

      buff.append(line);

    }

    String result = buff.toString();

    if (entity != null) {

      entity.consumeContent();

    }

    //分别处理基金接口数据

    detailIntfdataProcessFund(result,fundInterType);

   } catch (Exception e) {

    backErrorMessage = e.getLocalizedMessage();

    FFundInterErrorLog errorLog = new FFundInterErrorLog();

    errorLog.saveInfErrorLog(url+"?"+parameter,backErrorMessage);

    e.printStackTrace();

    } finally {

    if (httpclient!=null) {

      httpclient.clearRequestInterceptors();

    }

  }

  System.out.println(urls +"结束时间:"+new Date());

  return backErrorMessage;

}

 

  

posted @ 2015-04-05 19:15  xu_shuyi  阅读(209)  评论(0)    收藏  举报