站想要讓搜索引擎搜到,必須設(shè)置好keywords,description,title,做好SEO優(yōu)化,同時(shí)sitemap.xml也是不可缺少的通過(guò)Google,得到一些幫助,把自己做的整理一下:第一、對(duì)文件進(jìn)行查找: &nbs...
站想要讓搜索引擎搜到,必須設(shè)置好keywords,description,title,做好SEO優(yōu)化,同時(shí)sitemap.xml也是不可缺少的
通過(guò)Google,得到一些幫助,把自己做的整理一下:
第一、對(duì)文件進(jìn)行查找:
public class FileDemo {
File myDir;
File[] contents;
List fileList;
Iterator currentFileIt;
File currentFile;
String path;
/**
* 無(wú)參的構(gòu)造函數(shù)
* */
public FileDemo() {
path = new String("");
fileList = new ArrayList();
}
/**
* 有參的構(gòu)造函數(shù)
* */
public FileDemo(String path) {
this.path = path;
fileList = new ArrayList();
}
/**
* 設(shè)置要查看的文件路徑
*/
public void setPath(String path) {
this.path = path;
}
/***************************************************************************
* 返回當(dāng)前目錄的路徑
*/
public String getDirectory() {
return myDir.getPath();
}
public void refreshList() {
if (this.path.equals(""))
path = "c:\\";
myDir = new File(path);
fileList.clear();
contents = myDir.listFiles();
// 重新裝入路徑下文件
for (int i = 0; i < contents.length; i++) {
fileList.add(contents[i]);
}
currentFileIt = fileList.iterator();
}
/**
* 指到下一個(gè)條目
*/
public boolean nextFile() {
while (currentFileIt.hasNext()) {
currentFile = (File) currentFileIt.next();
return true;
}
return false;
}
/**
* 返回當(dāng)前指向的文件對(duì)象的文件名稱(chēng)
*/
public String getFileName() {
return currentFile.getName();
}
/**
* 返回當(dāng)前指向的文件對(duì)象的文件尺寸
*/
public String getFileSize() {
return convertFileSize(currentFile.length());
}
/**
* 轉(zhuǎn)換文件尺寸為指定格式。
*/
private String convertFileSize(long size) {
int divisor = 1;
String unit = "bytes";
if (size >= 1024 * 1024) {
divisor = 1024 * 1024;
unit = "MB";
} else if (size >= 1024) {
divisor = 1024;
unit = "KB";
}
if (divisor == 1)
return size / divisor + " " + unit;
String aftercomma = "" + 100 * (size % divisor) / divisor;
if (aftercomma.length() == 1)
aftercomma = "0" + aftercomma;
return size / divisor + "." + aftercomma + " " + unit;
}
/**
* 返回文件的最后修改日期
*/
public String getFileTimeStamp() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(new Date(currentFile.lastModified()));
return dateString;
}
/**
* 返回文件是不是一個(gè)目錄
*/
public boolean isDirectory() {
return currentFile.isDirectory();
}
}
第二、創(chuàng)建一個(gè)任務(wù)類(lèi),繼承TimerTask
public class XMLParese extends TimerTask{
private ServletContext context;
public XMLParese(ServletContext context){
this.context=context;
}
@Override
public void run() {
// TODO Auto-generated method stub
createSiteMap();
}
public void createSiteMap() {
String priority = "0.75";// 級(jí) 別
String changefreq = "daily";// "weekly";//頻 率
String xmlpath = "e:/sitemap.xml";// sitemap名稱(chēng)以及位置
String homeurl = "http://www.jsedu114.com"; // 欄目首頁(yè)
String []directory={"promotion","news","brand","goods","services","shop","winelive"};
FileDemo fp = new FileDemo();
try {
Document document = DocumentHelper.createDocument();
Element urlsetElement = document.addElement("urlset");
urlsetElement.addAttribute("xmlns ",
"http://www.sitemaps.org/schemas/sitemap/0.9"); // "xmlns
// "必須要有空格,否則會(huì)報(bào)錯(cuò)
urlsetElement.addAttribute("xmlns",
"http://www.sitemaps.org/schemas/sitemap/0.9");
urlsetElement.addAttribute("xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance");
urlsetElement.addAttribute("xsi:schemaLocation",
"http://www.sitemaps.org/schemas/sitemap/0.9 " +
"http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
//創(chuàng)建url根元素
Element urlElement ;
//為url根元素創(chuàng)建loc網(wǎng)頁(yè)地址,lastmod更新時(shí)間,changefreq更改頻率和priority級(jí)別
Element locElement ;
Element lastmodElement;
Element changefreqElement;
Element priorityElement;
fp.setPath("D:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\ROOT\\");
fp.refreshList();
while (fp.nextFile()) {
if (!fp.isDirectory()) {
homeurl="http://www.jsedu114.com/";
String f=fp.getFileName();
String fname=f.substring(f.lastIndexOf("."));
if((fname.equals(".html")||fname.equals(".htm")) && !f.equals("login.html")){
urlElement = urlsetElement.addElement("url");
locElement = urlElement.addElement("loc");
lastmodElement = urlElement.addElement("lastmod");
changefreqElement = urlElement.addElement("changefreq");
priorityElement = urlElement.addElement("priority");
//導(dǎo)航賦值
homeurl = homeurl + fp.getFileName();
locElement.setText(homeurl);
lastmodElement.setText(fp.getFileTimeStamp());// 這里時(shí)間是你更新時(shí)間,這里暫時(shí)統(tǒng)一
changefreqElement.setText(changefreq);
priorityElement.setText(priority);
}
}
}
//各個(gè)目錄下的文件
for(int i=0;i<directory.length;i++){
fp.setPath("D:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\ROOT\\"+directory[i]+"\\");
fp.refreshList();
while (fp.nextFile()) {
homeurl="http://www.jsedu114.com/"+directory[i]+"/";
urlElement = urlsetElement.addElement("url");
locElement = urlElement.addElement("loc");
lastmodElement = urlElement.addElement("lastmod");
changefreqElement = urlElement.addElement("changefreq");
priorityElement = urlElement.addElement("priority");
homeurl = homeurl + fp.getFileName();
locElement.setText(homeurl);
lastmodElement.setText(fp.getFileTimeStamp());// 這里時(shí)間是你更新時(shí)間,這里暫時(shí)統(tǒng)一
changefreqElement.setText(changefreq);
priorityElement.setText(priority);
}
}
XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
xmlpath)));
writer.write(document);
writer.close();
document = null;
// 格式化
formatXMLFile(xmlpath, "UTF-8");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 格式化XML文檔,并解決中文問(wèn)題
* @param xmlpath:xml文件路徑
* @param charSet:格式化的字符集
* @return
*/
public static boolean formatXMLFile(String xmlpath, String charSet) {
boolean returnValue = false;
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(xmlpath));
XMLWriter output = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(charSet);
output = new XMLWriter(new FileWriter(new File(xmlpath)), format);
output.write(document);
output.close();
document = null;
saxReader = null;
returnValue = true;
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
}
第三、創(chuàng)建一個(gè)任務(wù)監(jiān)聽(tīng)類(lèi),實(shí)現(xiàn)ServletContextListener 接口
public class TimerListener implements ServletContextListener {
// 設(shè)置啟動(dòng)時(shí)間為23點(diǎn);
private static final int hours = 23;
private static final int minutes = 0;
private static final int seconds = 0;
private Timer timer = null;
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
event.getServletContext().log("定時(shí)器銷(xiāo)毀");
}
public void contextInitialized(ServletContextEvent event) {
timer=new Timer(true);
event.getServletContext().log("定時(shí)器已啟動(dòng)");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
//SiteMap生成
//間隔時(shí)間1天生成一次
timer.schedule(new XMLParese(event.getServletContext()),calendar.getTime(),
1*24*60*60*1000);
}
}
第四、在web中添加一個(gè)監(jiān)聽(tīng)器
web.xml里的配置
<!-- 定時(shí)器 -->
<listener>
<listener-class>timer.TimerListener</listener-class>
</listener>
來(lái)源:本文內(nèi)容搜集或轉(zhuǎn)自各大網(wǎng)絡(luò)平臺(tái),并已注明來(lái)源、出處,如果轉(zhuǎn)載侵犯您的版權(quán)或非授權(quán)發(fā)布,請(qǐng)聯(lián)系小編,我們會(huì)及時(shí)審核處理。
聲明:江蘇教育黃頁(yè)對(duì)文中觀點(diǎn)保持中立,對(duì)所包含內(nèi)容的準(zhǔn)確性、可靠性或者完整性不提供任何明示或暗示的保證,不對(duì)文章觀點(diǎn)負(fù)責(zé),僅作分享之用,文章版權(quán)及插圖屬于原作者。
Copyright?2013-2024 JSedu114 All Rights Reserved. 江蘇教育信息綜合發(fā)布查詢(xún)平臺(tái)保留所有權(quán)利
蘇公網(wǎng)安備32010402000125
蘇ICP備14051488號(hào)-3技術(shù)支持:南京博盛藍(lán)睿網(wǎng)絡(luò)科技有限公司
南京思必達(dá)教育科技有限公司版權(quán)所有 百度統(tǒng)計(jì)