zoukankan      html  css  js  c++  java
  • 农场销售

    一: 请求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;
        }
  • 相关阅读:
    ICONS-图标库
    图形资源
    vue项目中,如果修改了组件名称,vscode编辑器会在引入修改组件的名字处提示红色波浪线 The file is in the program because:Imported via xxx Root file specified for compilation .
    接口在dev环境报跨域问题(has been blocked by CORS policy:Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header ispresent on the requested resource.),qa环境正常
    阿里云occ的图片文件URL用浏览器直接打开无法访问,提示This XML file does noe appear to have any style information associated with it. The document tree is shown below.
    vue 项目使用element ui 中tree组件 check-strictly 用法(父子不互相关联的反显情况)
    高德地图进行线路规划绘制标记点操作(vue)
    vue中实现拖拽调整顺序功能
    2021-01-22 浏览器相关知识
    2021-01-22 js 相关知识点
  • 原文地址:https://www.cnblogs.com/xinxin-ting/p/8604240.html
Copyright © 2011-2022 走看看