< |
< |
小于號 |
> |
> |
大于號 |
& |
& |
和 |
' |
' |
單引號 |
" |
" |
雙引號 |
(function(){
if(!/*@cc_on!@*/0)
return;
var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;
while(i--){document.createElement(e[i])}
})()
//然后在head中引入該js
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
package com.koyo.downloadphoto.service.impl;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;
import com.koyo.downloadphoto.service.Spider;
import com.koyo.downloadphoto.utils.HttpUtils;
import com.koyo.downloadphoto.utils.ParseUtils;
public class SpiderForRenRen extends Spider {
private Logger logger = Logger.getLogger(SpiderForRenRen.class);
@Override
public void execute() {
try {
String url = " + "/album/relatives";
// ===================請求登錄======================================================
HttpPost post = new HttpPost(" // 添加POST參數
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("email", loginName));
nvps.add(new BasicNameValuePair("password", loginPassword));
post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
HttpResponse response = httpClient.execute(post, httpContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
// 使用響應中的編碼來解釋響應的內容
String html1 = IOUtils.toString(is);
LinkTag linkTag1 = ParseUtils.parseTag(html1, LinkTag.class);
String url1 = linkTag1.getLink();
HttpGet get = new HttpGet(url1);
response = httpClient.execute(get, httpContext);
// 保存cookie
httpContext.setAttribute(ClientContext.COOKIE_STORE, httpClient
.getParams().getParameter("CookieStore"));
EntityUtils.consume(response.getEntity());
System.out.println("賬號:" + loginName);
System.out.println("密碼:" + loginPassword);
}
// ===================獲取相冊頁面信息===================================================
// 根據URL地址,獲取網頁內容
String html = HttpUtils.getHtml(httpClient, httpContext, url);
if (html == null) {
logger.error("無法獲取【" + url + "】網址的內容");
throw new RuntimeException("無法獲取【" + url + "】網址的內容");
}
//獲取好友名
Parser parser = new Parser();
parser.setInputHTML(html);
AndFilter andFilter = new AndFilter(new TagNameFilter("ul"), new HasAttributeFilter("class", "nav-tabs"));
NodeList nodes = parser.parse(andFilter);
String tempString = nodes.toHtml();
LinkTag tempTag = ParseUtils.parseTag(tempString, LinkTag.class);
String tempName = tempTag.getLinkText();
// String friendName = tempName.substring(tempName.indexOf("\n")+1,tempName.lastIndexOf("\n"));
String friendName = tempName.trim();
//獲取相冊名
String albumName;
List<LinkTag> linkTags = ParseUtils.parseTags(html, LinkTag.class,
"class", "album-cover");
List<Span> spans = ParseUtils.parseTags(html, Span.class,"class","album-name");
if (linkTags != null) {
for (int i=0; i<linkTags.size(); i++) {
tempName = spans.get(i).getStringText();
//由于頭像相冊前還有一個<span class="userhead"/> 故不能使用tempName.trim()
albumName = tempName.substring(tempName.lastIndexOf("\n")+1);
url = linkTags.get(i).getLink();
// 根據URL地址,獲取網頁內容
html = HttpUtils.getHtml(httpClient, httpContext, url);
if (html == null) {
logger.error("無法獲取【" + url + "】網址的內容");
throw new RuntimeException("無法獲取【" + url + "】網址的內容");
}
List<LinkTag> linkTags2 = ParseUtils.parseTags(html,
LinkTag.class, "class", "picture");
if (linkTags2 != null) {
for (LinkTag linkTag2 : linkTags2) {
url = linkTag2.getLink();
// 根據URL地址,獲取網頁內容
html = HttpUtils.getHtml(httpClient, httpContext,
url);
if (html == null) {
logger.error("無法獲取【" + url + "】網址的內容");
throw new RuntimeException("無法獲取【" + url
+ "】網址的內容");
}
// 網頁中所包含的圖片,并下載到upload目錄,然后創建Attachment對象
ImageTag imageTag = ParseUtils.parseTag(html,
ImageTag.class, "id", "photo");
if (imageTag != null) {
// 得到圖片所在的路徑目錄
// String baseUrl = url.substring(0,
// url.lastIndexOf("/") + 1);
// 這個是<img>標簽中的src的值
String imageUrl = imageTag.getImageURL();
String photoName = imageUrl.substring(imageUrl
.lastIndexOf("/"));
// 圖片的絕對路徑
// String absoluteUrl = baseUrl + imageUrl;
// : "文章標題/xxx.jpg"
String imageName = friendName + "/" +albumName + photoName;
// 把圖片保存到upload目錄
// 首先確定,保存到本地的圖片的路徑
String imageLocalFile = "D:/PhotosForRenRen/"
+ imageName;
// 如果圖片已經被下載到本地,則不再下載
if (!new File(imageLocalFile).exists()) {
// 下載圖片的信息
byte[] image = HttpUtils.getImage(
httpClient, httpContext, imageUrl);
// 直接使用new
// FileOutputStream(imageLocalFile)這種方式,創建一個
// 文件輸出流,存在的問題就是:如果這個文件所在的目錄不存在,則創建不了
// 輸出流,會拋出異常!
// 所以,使用輔助的工具類來創建一個文件輸出流:FileUtils.openOutputStream(new
// File(imageLocalFile))
// 通過這個方法,當文件所在的父目錄不存在的時候,將自動創建其所有的父目錄
IOUtils.write(image, FileUtils
.openOutputStream(new File(
imageLocalFile)));
System.out.println("圖片【" + imageUrl
+ "】已下載");
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script type="text/javascript">
var timer;
window.onscroll= function()
{
}
</script>
</head>
<body>
<div id="div1" style=" position:absolute; width:100px; height:100px; background:#FF0000"></div>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p><p><textarea rows="15" cols="50" id="text1"></textarea></p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
<p>ssssssssssss</p>
</body>
</html>
FormBean的驗證:
1、重寫FormBean的validate方法。
public ActionErrors validate(ActionMapping mapping,
2、在struts-config.xml中配置action節點的validate與input屬性,validate表示是否調formBean的validate方法,input是如果出錯的話跳轉的錯誤處理界面。注意:如果formBean重寫的了validate方法并且action節點validate屬性為true,那一定要添加input屬性。
3、在struts-config.xml中添加<message-resources parameter="org.koyo.views.login" />注意:其中的parameter屬性的值是login.properties文件的路徑。(容易被漏掉,多加注意)
4、在界面使用<html:errors />或<html:errors property="nameEmpty"/>顯示錯誤信息。
步驟2、3、4,尤其是步驟3,經常會漏掉,應多加注意?。?!
ActionBean的驗證
1、在ActionBean中添加代碼。
ActionMessages messages = new ActionMessages();//不要與FormBean混淆,此處直接就是ActionMessage
步驟2、3、4,與FormBean驗證相同。
主要區別:ActionBean驗證主要做業務,需要與數據庫交互,比如用戶名是否可用,轉賬余額是否可用,出貨余額等驗證;formBean雖然完成客戶端驗證功能,但需要與服務器交互。注意不同的區分標準。。
本文章大部分內容轉載自 西安云工廠http://www.xaygc.com/struts.html