依赖
org.apache.httpcomponents httpmime 4.5.2 org.apache.poi poi 3.14
调用
public static void main(String[] args) throws ClientProtocolException, URISyntaxException, IOException { String s = writeWordFile("d:/", "ab.doc", getHttpData(s)); System.out.println("ok"); }
获取网页
// 请求网路自考网数据 public static String getHttpData(String url) throws URISyntaxException, ClientProtocolException, IOException { List nameValuePairList = Lists.newArrayList(); nameValuePairList.add(new BasicNameValuePair("q", "x")); URI uri = new URIBuilder(url).addParameters(nameValuePairList).build(); List headerList = Lists.newArrayList(); headerList.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate")); headerList.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive")); HttpClient httpClient = HttpClients.custom().setDefaultHeaders(headerList).build(); HttpUriRequest httpUriRequest = RequestBuilder.get().setUri(uri).build(); HttpResponse httpResponse = httpClient.execute(httpUriRequest); HttpEntity entity = httpResponse.getEntity(); String rawHTMLContent = EntityUtils.toString(entity); EntityUtils.consume(entity); return rawHTMLContent; }
写入本地
public static void writeWordFile(String path, String fileName, String content) { try { if (!"".equals(path)) { // 检查目录是否存在 File fileDir = new File(path); if (fileDir.exists()) { byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem poifs = new POIFSFileSystem(); DirectoryEntry directory = poifs.getRoot(); DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path + fileName); poifs.writeFilesystem(ostream); bais.close(); ostream.close(); } } } catch (IOException e) { e.printStackTrace(); } }
import
import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpServletRequest;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpHeaders;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.client.methods.RequestBuilder;import org.apache.http.client.utils.URIBuilder;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicHeader;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.assertj.core.util.Lists;
window.addEventListener来解决让一个js事件执行多个函数
本篇文章来简单介绍一下JS作用域,以及BOM对象中的三个基础对象,分别是window对象、history对象和location对象。
一、JS作用域
1、函数嵌套作用域
//函数作用域 var place="Beijing"; //外部定义的变量 function outer() { var place="Chengdu"; //outer函数下的重新定义 function inner() { var place="Guangzhou"; //inner函数下的定义 console.log(place); } inner(); } // //调用outer函数,同时会执行inner函数 outer();
执行结果:
Guangzhou
2、函数多调用
var name="路飞"; function bar() { console.log(name); } function func() { var name="索隆"; return bar; } var ret=func(); //将func函数执行的返回结果赋值给ret变量,即ret将代表bar函数 ret(); //调用ret函数,即bar函数 //又因为bar函数的作用域在外部,与它同级的name变量值为"路飞"
执行结果:
路飞
二、window窗口
1、BOM简介
BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作。使用 BOM,开发者可以移动窗口、改变状态栏中的文本以及执行其他与页面内容不直接相关的动作。使 JavaScript 有能力与浏览器”对话”。
2、window对象方法汇总
- alert() 显示带有一段消息和一个确认按钮的警告框。
window.alert("欢迎访问!");
- confirm() 显示带有一段消息以及确认按钮和取消按钮的对话框。
var ret= window.confirm("您确定要访问吗?"); //由用户做出选择,因此有一个返回值 console.log(ret);
- prompt() 显示可提示用户输入的对话框。
var ret= window.prompt("您的访问指令:"); //要去用户输入内容,在控制台显示 console.log(ret);
- open() 打开一个新的浏览器窗口或查找一个已命名的窗口。(不常用)
- close() 关闭浏览器窗口。(不常用)
- setInterval() 按照指定的周期(以毫秒计)来调用函数或计算表达式。
- clearInterval() 取消由 setInterval() 设置的 timeout。
setInterval(func,1000); //表示每隔1000毫秒(1秒)执行一次函数func() function func() { console.log("Hello JavaScript!"); }
可以看到这个地方会一直有”Hello JavaScript!”的出现。
下面是由setInterval和clearInterval实现的动态时间更新小案例…
JS(
history2:
TitleBack
这样就可以实现一个基本的前进和返回的效果(这个其实也不太常用)
四、location对象
1、简介
Location 对象包含有关当前 URL 的信息。
Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。
2、location下的方法
- location.assign(URL)
- location.reload()
- location.replace(newURL)//注意与assign的区别
3、测试代码
Title
Original: https://www.cnblogs.com/shanghaiweb/p/16544700.html
Author: 源计划crh
Title: java 获取网页写入本地文件来解决让一个js事件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/594193/
转载文章受原作者版权保护。转载请注明原作者出处!