⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wapptr.java~331~

📁 jwap 协议 udp 可以用于手机通讯
💻 JAVA~331~
📖 第 1 页 / 共 5 页
字号:
        String dstr, wbfile, wmlfile, htmlfile;
        wbfile = wmlfile = htmlfile = "";
        int flag20 = 0;
        int kjava = 0;
        System.out.println("writing to a file");
        Calendar ca = Calendar.getInstance();
        Date dt = new Date();
        ca.setTime(dt);
        dstr = new String(String.valueOf(ca.get(Calendar.YEAR)));
        dstr = dstr + ca.get(Calendar.MONTH) + ca.get(Calendar.DAY_OF_MONTH) +
               ca.get(Calendar.HOUR) + ca.get(Calendar.MINUTE) +
               ca.get(Calendar.SECOND);
        String currentdir = System.getProperty("user.dir");

        int wmlflag = 0;
        if (FileType.equals("wml") || FileType.equals("") ||
            FileType.equals("null")) {
            wmlflag = 1;
            if (xhtml_wml == 1) {
                LogStr[11] = "df" + dstr + ".wbxml";
            } else {
                LogStr[11] = "df" + dstr + ".wml";
            }
            wbfile = currentdir + "\\df" + dstr + ".wbxml";
            wmlfile = currentdir + "\\df" + dstr + ".wml";
            // htmlfile=currentdir + "\\df" + dstr + ".html";


        } else {
            if (FileType.equals("JAR") || FileType.equals("WAR") ||
                FileType.equals("JAD")) {
                if (CheckJar(payload) == 1) {
                    LogStr[11] = "df" + dstr + "." + FileType;
                    wbfile = currentdir + "\\df" + dstr + "." + FileType;
                    wmlfile = currentdir + "\\df" + dstr + "." + FileType;

                } else {
                    wmlflag = 1;
                    if (xhtml_wml == 1) {
                        LogStr[11] = "df" + dstr + ".wbxml";
                    } else {
                        LogStr[11] = "df" + dstr + ".wml";
                    }
                    wbfile = currentdir + "\\df" + dstr + ".wbxml";
                    wmlfile = currentdir + "\\df" + dstr + ".wml";

                }
            } else {
                LogStr[11] = "df" + dstr + "." + FileType;
                wbfile = currentdir + "\\df" + dstr + "." + FileType;
                wmlfile = currentdir + "\\df" + dstr + "." + FileType;
            }
        }

        try {

            FileOutputStream wapContent = new FileOutputStream(wbfile);
            wapContent.write(payload);
            wapContent.close();
        } catch (Exception exp) {
            //exp.printStackTrace();
            System.out.println("写文件失败");
            System.exit(1);
        }

        // Decode the bytes in WBXML file to get wml file
        if (xhtml_wml == 1) {
            waitfor = 1;
        }

        if (wmlflag == 1) {
            if (xhtml_wml == 1) {
                return;
            }

            FileInputStream tokenStream1;

            try {

                tokenStream1 = new FileInputStream(wbfile);

                //check if 2.0 =================================
                BufferedReader reader = null;
                reader = new BufferedReader(new InputStreamReader(
                        tokenStream1, "UTF-8"));
                String oneline;
                StringBuffer sb = new StringBuffer("");
                int i = 0;
                while ((oneline = reader.readLine()) != null) {
                    if (!oneline.equals("")) {
                        sb.append(oneline.toUpperCase());
                        i++;
                        if (i > 4) {
                            break;
                        }
                    }
                }
                reader.close();
                tokenStream1.close();

                int pos1 = sb.indexOf("<!DOCTYPE");
                if (pos1 >= 0) {
                    int pos2 = sb.indexOf(">", pos1);
                    if (pos2 > pos1) {
                        String txt = sb.substring(pos1, pos2 + 1);
                        int pos3 = txt.indexOf("HTMLL 4.01");
                        int pos4 = txt.indexOf("XHTML");
                        if (pos3 >= 0 || pos4 >= 0) {
                            flag20 = 1;
                            LogStr[11] = "df" + dstr + ".wbxml";
                        }

                    }
                } else {
                    int pos51 = sb.indexOf("HTML");
                    if (pos51 >= 0) {
                        flag20 = 1;
                        LogStr[11] = "df" + dstr + ".wbxml";
                    }

                }

                //=============================================
                if (flag20 == 1) {

                    //FileOutputStream wapContent20 = new FileOutputStream(htmlfile);
                    //wapContent20.write(payload);
                    //wapContent20.close();

                } else {
                    FileInputStream tokenStream = new FileInputStream(wbfile); ;
                    FileOutputStream xmlStream = new FileOutputStream(wmlfile);
                    Document document = WBXMLDecoder.getInstance().decode(
                            tokenStream);
                    if (!document.hasChildNodes()) {
                        LogStr[11] = "df" + dstr + ".wbxml";
                        return;
                    }
                    OutputFormat of = new OutputFormat(document);
                    XMLSerializer serial = new XMLSerializer(xmlStream, of);
                    serial.setOutputByteStream(xmlStream);
                    serial.setOutputFormat(of);
                    serial.asDOMSerializer();
                    serial.serialize(document);
                    xmlStream.close();
                    tokenStream.close();
                }
            } catch (Exception e) {
                //e.printStackTrace();
                System.out.println("写wml文件异常");
            }
        }

        if (xhtml_wml == 1) {
            waitfor = 1;
        }
    }

    // -----------------------------------------------------------
    private class UpperLayerImpl implements IWSPUpperLayer2 {
        // Connection established
        public void s_connect_cnf() {
            complete(sessionLock, CONNECTED);
        }

        public void s_disconnect_ind(short reason) {
            if (log.isDebugEnabled()) {
                log.debug("s_disconnect_ind(" + reason + ")");
            }
            complete(sessionLock, "DISCONNECTED: " + reason);
            session = null;
        }

        public void s_disconnect_ind(CWSPSocketAddress[] redirectInfo) {
            complete(sessionLock, redirectInfo);
        }

        public void s_methodResult_ind(CWSPResult result) {

            //writeBytesToFile(result.getPayload());
            if (Sendflg == 1) {

            } else {
                if (CWTPStatue.GetResultState() == 32) {
                    if (result.getPayload().equals("") ||
                        result.getPayload().length == 0) {
                        downloadflg = 0;
                    } else {
                        writeBytesToFile(result.getPayload());
                        downloadflg = 1;
                    }
                } else {
                    //System.out.println("下载失败");
                    downloadflg = 0;
                    //System.exit(1);
                }
            }
            Response response = new Response(result);
            CWSPMethodManager mgr = result.getMethodManager();
            // Acknowledge reception (TODO: arg should not be null)
            mgr.s_methodResult(null);
            complete(mgr, response);
        }

        public void s_suspend_ind(short reason) {

            if (reason == 1000) {
                downtime = System.currentTimeMillis();
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("s_suspend_ind(" + reason + ")");
                }
            }
        }

        public void s_resume_cnf() {
            log.debug("s_resume_cnf()");
        }

        public void s_disconnect_ind(InetAddress[] redirectInfo) {
        }

        public void s_methodResult_ind(byte[] payload, String contentType,
                                       boolean moreData) {

            if (xhtml_wml == 1) {

                if (Sendflg == 1) {
                    waitfor = 1;
                } else {
                    if (payload.equals("") || payload.length == 0 ||
                        payload.equals("下载失败") || !moreData) {
                        waitfor = 2;
                        downloadflg = 0;
                    } else {
                        writeBytesToFile(payload);
                        downloadflg = 1;
                    }
                }
            } else {

                /* if (Sendflg == 1) {
                     return;
                 } else {
                     if (CWTPStatue.GetResultState() == 32) {
                         if (payload.equals("") || payload.length == 0) {
                             downloadflg = 0;
                         } else {
                             writeBytesToFile(payload);
                             downloadflg = 1;
                         }
                     } else {
                         //System.out.println("下载失败");
                         downloadflg = 0;
                         //System.exit(1);
                     }
                 }*/

            }

        }
    }


    private class UpperLayerHttp implements HttpUpperLayer {
        public void tr_result(int pcode, byte[] poly) {
            if(pcode==-1)
            {
                waitfor=2;
            }
            else
            {
                waitfor=1;
            }
        }

        public void tr_abort(short abortReason) {
        }

        public void tr_download(int pcode) {
        }

    }


    public static void main(String[] args) throws FileNotFoundException,
            UnknownHostException, IOException, IllegalStateException,
            SocketException {

        if (args.length < 4) {
            System.err.println(USAGE);
            System.exit(1);
        }
        String wapGW = "10.0.0.172";
        int wapPort = 9201;
        int argp = 0;
        String method = args[argp];
        String userAgent = null;
        String output = null;
        boolean showHeaders = false;
        String contentType = null;
        String input = null;
        String url = null;
        String locaddr = null;
        InetAddress la = null;
        int lp = CWTPSocket.DEFAULT_PORT;
        long tc = DEFAULT_CONNECT_TIMEOUT;
        long tx = DEFAULT_EXEC_TIMEOUT;

        int WorkMode;
        String ConfFile;
        String Logfilename;
        WorkMode = Integer.parseInt(args[0]);
        ConfFile = args[1];
        Logfilename = args[2];

        if (!FileWork.FW_ChkMakdir("JwapMsgLog")) {
            log.error("创建JwapMsgLog目录失败!");
            System.exit(1);
        }

        WapPtr wapptr = new WapPtr(la,
                                   lp, 0);

        if (!wapptr.filework.FW_ChkMakdir("downloadforder")) {
            log.error("创建downloadforder目录失败!");
            System.exit(1);
        }

        if (!wapptr.filework.FW_ChkMakdir("threadlog")) {
            log.error("创建threadlog目录失败!");
            System.exit(1);
        }

        Request request = null;
        OutputStream out = null;

        if (output == null || "-".equals(output)) {
            out = System.out;
        } else {
            out = new FileOutputStream(output);
        }

        if ("POST".equals(method)) {
            if (contentType == null) {
                System.err.println(
                        "Warning: no content-type specified, assuming " +
                        DEFAULT_CONTENT_TYPE);
                contentType = DEFAULT_CONTENT_TYPE;
            }
            byte[] postData = null;
            try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -