{"id":391,"date":"2012-12-13T21:04:00","date_gmt":"2012-12-13T13:04:00","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=391"},"modified":"2020-06-06T21:05:33","modified_gmt":"2020-06-06T13:05:33","slug":"%e4%b8%80%e4%b8%aa%e7%ae%80%e6%98%93%e7%9a%84%e7%bd%91%e9%a1%b5%e7%88%ac%e8%99%ab%ef%bc%8c%e5%8f%af%e7%94%a8%e4%ba%8e%e4%b8%8b%e8%bd%bd%e5%9c%a8%e7%ba%bfapi%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=391","title":{"rendered":"\u4e00\u4e2a\u7b80\u6613\u7684\u7f51\u9875\u722c\u866b\uff0c\u53ef\u7528\u4e8e\u4e0b\u8f7d\u5728\u7ebfAPI\u6587\u6863"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package wkx;\n \nimport java.io.File;\nimport java.io.FileNotFoundException;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.util.HashSet;\nimport java.util.Set;\n \nimport org.apache.commons.httpclient.HttpClient;\nimport org.apache.commons.httpclient.HttpMethod;\nimport org.apache.commons.httpclient.HttpStatus;\nimport org.apache.commons.httpclient.methods.PostMethod;\n \npublic class Main {\n \n\tprivate static Set&lt;String> filenames = new HashSet&lt;String>();\n \n\tpublic static String getSource(String url) {\n\t\tString response = null;\n\t\tHttpClient client = new HttpClient();\n\t\tHttpMethod method = null;\n\t\ttry {\n\t\t\tmethod = new PostMethod(url);\n\t\t\tclient.executeMethod(method);\n\t\t\tif (method.getStatusCode() == HttpStatus.SC_OK) {\n\t\t\t\tresponse = method.getResponseBodyAsString();\n\t\t\t}\n\t\t} catch (IOException e) {\n\t\t\tSystem.out.println(\"Get Source Error!\");\n\t\t} finally {\n\t\t\tif (method != null)\n\t\t\t\tmethod.releaseConnection();\n\t\t}\n\t\treturn response;\n\t}\n \n\tpublic static void create(String url, String froot, String cur) {\n\t\tString curUrl = url + \"\/\" + cur;\n\t\tif (filenames.contains(curUrl)) {\n\t\t\treturn;\n\t\t}\n\t\tfilenames.add(curUrl);\n\t\tString cont = getSource(curUrl);\n\t\tif (cont == null) {\n\t\t\treturn;\n\t\t}\n\t\tFile f = null;\n\t\tFileOutputStream fos = null;\n\t\ttry {\n\t\t\tf = new File(froot);\n\t\t\tif (!f.exists()) {\n\t\t\t\tf.mkdirs();\n\t\t\t}\n\t\t\tf = new File(froot + \"\\\\\" + cur);\n\t\t\tf.createNewFile();\n\t\t\tfos = new FileOutputStream(f);\n\t\t\tfos.write(cont.getBytes());\n\t\t\tfos.flush();\n\t\t} catch (FileNotFoundException e) {\n\t\t\te.printStackTrace();\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t} finally {\n\t\t\tif (fos != null) {\n\t\t\t\ttry {\n\t\t\t\t\tfos.close();\n\t\t\t\t} catch (IOException e) {\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tString&#91;] files = cont.split(\"\\\"\");\n\t\tfor (String file : files) {\n\t\t\tif (file.matches(\".*\\\\.html$\") &amp;&amp; !file.startsWith(\"http\")) {\n\t\t\t\tif (file.contains(\" \"))\n\t\t\t\t\tcontinue;\n\t\t\t\tString turl = url;\n\t\t\t\tString tfroot = froot;\n\t\t\t\tfile.replaceAll(\"\/\/\", \"\/\");\n\t\t\t\tint tindex = file.lastIndexOf(\"?\");\n\t\t\t\tif (tindex != -1) {\n\t\t\t\t\tfile = file.substring(0, tindex);\n\t\t\t\t}\n\t\t\t\tint index = 0;\n\t\t\t\twhile ((index = file.indexOf(\"\/\")) != -1) {\n\t\t\t\t\tif (file.charAt(index - 1 &lt; 0 ? 0 : index - 1) == '.') {\n\t\t\t\t\t\tturl = turl.substring(0, turl.lastIndexOf(\"\/\"));\n\t\t\t\t\t\ttfroot = tfroot.substring(0, tfroot.lastIndexOf(\"\\\\\"));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tturl = turl + \"\/\" + file.substring(0, index);\n\t\t\t\t\t\ttfroot = tfroot + \"\\\\\" + file.substring(0, index);\n\t\t\t\t\t}\n\t\t\t\t\tfile = file.substring(index + 1);\n\t\t\t\t}\n\t\t\t\tcreate(turl, tfroot, file);\n\t\t\t}\n\t\t}\n\t}\n \n\tpublic static void main(String&#91;] args) {\n\t\tString url = \"http:\/\/localhost\";\n\t\tString froot = \"C:\\\\Users\\\\Jack_Wong\\\\Desktop\\\\api\";\n\t\tcreate(url, froot, \"index.html\");\n\t}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-06-03-play-ground"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=391"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}