依賴
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
呼叫
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<NameValuePair> nameValuePairList = Lists.newArrayList();
nameValuePairList.add(new BasicNameValuePair("q", "x"));
URI uri = new URIBuilder(url).addParameters(nameValuePairList).build();
List<Header> 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(<script>標簽內的)代碼:
showTime();
function showTime()
{
var current_time=new Date().toLocaleString(); //獲取當前時間并將其轉化成字字串顯示
// alert(current_time);
var ele=document.getElementById("id1"); //獲取id1這個標簽的內容
ele.value=https://www.cnblogs.com/shanghaiweb/p/current_time; //將獲取的id1標簽的內容設定為輸入框的值,實作動態更新時間
}
var click1
function begin()
{
if(click1==undefined) //防止多個click串行
{
showTime(); //實作無延遲效果
click1= setInterval(showTime,1000); //實作一秒更新一次時間
}
}
function end()
{
clearInterval(click1); //清除定時間隔
}
外部再加兩個標簽:
<input type="text" id="id1" onclick="begin()">
<button onclick="end()">停止!</button> <!-- 系結一個end函式 -->

- setTimeout() 在指定的毫秒數后呼叫函式或計算運算式,
- clearTimeout() 取消由 setTimeout() 方法設定的 timeout,
function func()
{
console.log("別點我了!");
}
var c= setTimeout(func,3000); //三秒鐘之后只執行一次
//清除setTimeout 的內容
// clearTimeout(c);
scrollTo() 把內容滾動到指定的坐標,(少用)
三、history物件
1、簡介
History 物件包含用戶(在瀏覽器窗口中)訪問過的 URL,
History 物件是 window 物件的一部分,可通過 window.history 屬性對其進行訪問,
2、history物件的方法
- back() 加載 history 串列中的前一個 URL,
- forward() 加載 history 串列中的下一個 URL,
- go() 加載 history 串列中的某個具體頁面,
3、測驗代碼例如:進口氣動球閥頁面
history1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="https://www.cnblogs.com/shanghaiweb/p/JS之history2.html">通往history2</a>
<button onclick="history.forward()"> >>>>>>>> </button>
<script>
</script>
</body>
</html>
history2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button onclick="history.back()">Back</button>
</body>
</html>
這樣就可以實作一個基本的前進和回傳的效果(這個其實也不太常用)
四、location物件
1、簡介
Location 物件包含有關當前 URL 的資訊,
Location 物件是 Window 物件的一個部分,可通過 window.location 屬性來訪問,
2、location下的方法
- location.assign(URL)
- location.reload()
- location.replace(newURL)//注意與assign的區別
3、測驗代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button onclick="f()">點我!</button>
<script>
function f1()
{
// location.assign("www.baidu.com"); //默認跳轉的頁面
// location.reload(); //重繪當前頁面
location.replace("www.baidu.com"); //替換當前頁面
//assign可以后退,replace不行
}
</script>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500730.html
標籤:Java
