农场销售

一: 请求URL

public JSONObject HttpPostFromWorklight(String requestParm, String xslFileName, String strURL) {

        System.out.println("strURL=" + strURL);
        System.out.println("请求参数:" + requestParm);

        ResultNew resultNew = new ResultNew();
        JSONObject json = new JSONObject();
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST");// 设置请求方式
            connection.setRequestProperty("Accept", "text/xml; charset=utf-8");// 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");// 设置发送数据的格式
            connection.connect();
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");// utf-8编码
            out.append(requestParm);
            out.flush();
            out.close(); // 读取响应
            int length = (int) connection.getContentLength();// 获取长度
            InputStream is = connection.getInputStream();
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                String result = new String(data, "UTF-8");
                System.out.println("RE:" + result);
                
                String src = returnXml(result, xslFileName, "utf-8");
                //System.out.println("src:" + src);

                JSONObject jsonObject = JSON.parseObject(src);
                //System.out.println("json:" + jsonObject.getString("Items"));

                resultNew.setStatusReason("OK");
                resultNew.setIsSuccessful(true);
                resultNew.setStatusCode(connection.getResponseCode());
                json = (JSONObject) JSONObject.toJSON(resultNew);

                // 判断json
                if (jsonObject.getString("Items").startsWith("[")) {
                    json.put("Items", jsonObject.getJSONArray("Items"));
                } else if (jsonObject.getString("Items").startsWith("{")) {
                    json.put("Items", jsonObject.getJSONObject("Items"));
                }
                //System.out.println("JSONObject:" + json);

            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return json;
    }

二: xml通过xsl转换

/**
     * 转换文件(xml通过xsl转换)
     * @param xml 转换文件
     * @param xsl 转换模板
     * @param encoding  字符编码
     * @return String 返回转换后的xml字符串
     * @throws Exception
     */
    public static String returnXml(String xml, String xsl, String encoding) throws Exception {
        String xslname = CRMService.class.getResource("/").getPath() + "xslFile/" + xsl;
        System.out.println("xslname:" + xslname);
        // 获取字符串输入流
        StringWriter stringWriter = new StringWriter();
        // 获取打印输出流,并设置输出为字符流形式
        PrintWriter printWriter = new PrintWriter(stringWriter);
        // 根据输入的String,获取XML字符串是输入源
        Source srcSource = new StreamSource(new ByteArrayInputStream(xml.getBytes(encoding)));
        // 设置转换结果输出为打印流
        Result destResult = new StreamResult(printWriter);
        // 获取转换模板
        // ClassLoader cl = CRMService.class.getClassLoader();
        // InputStream is = cl.getResourceAsStream(xsl);
        File file = new File(xslname);
        InputStream is = new FileInputStream(file);
        Source xslSource = new StreamSource(is);
        // 创建转换工厂
        TransformerFactory transFact = TransformerFactory.newInstance();
        // 创建转换对象
        Transformer trans = transFact.newTransformer(xslSource);
        // 实行转换
        trans.transform(srcSource, destResult);
        // 把转换结果赋值到 返回的字符串中
        String xmlParsed = stringWriter.toString();
        // 关闭打印流
        printWriter.close();
        return xmlParsed;
    }

 

posted @ 2018-03-19 19:38  Mr.Duanxj  阅读(141)  评论(0编辑  收藏  举报