欢迎来到天天文库
浏览记录
ID:37710743
大小:35.00 KB
页数:6页
时间:2019-05-29
《Java指定编码生成静态网页技术》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.io.File;importjava.io.FileWriter;importjava.io.InputStream;importjava.io.PrintWriter;importjava.net.HttpURLConnection;importjava.net.URL;publicclassHtmlPage{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubif(saveHtmlPage(getHtmlPage("http://www.baidu.com","UTF- 8")
2、,"F:/index.html")){System.out.println("生成成功");}else{System.out.println("生成失败");}if(deleteFile("F:/index.html")){System.out.print("删除成功");}else{System.out.print("删除失败");}}/****@paramhttpURL网页地址**@paramencode默认为UTF-8*@returnHtml页面的代码*/publicstaticStringgetHtmlPage(StringhttpURL,Stringencoding){
3、StringhtmlCode="";try{InputStreaminputStream;URLurl=newURL(httpURL);HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.connect();inputStream=connection.getInputStream();bytebytes[]=newbyte[1024*2000];intindex=0;intcount=inputStream.read(bytes,index,1024*2000);while
4、(count!=-1){index+=count;count=inputStream.read(bytes,index,1);}htmlCode=newString(bytes,encoding);//htmlCode=newString(bytes);connection.disconnect();}catch(Exceptionex){ex.printStackTrace();returnnull;}returnhtmlCode.trim();}publicstaticbooleansaveHtmlPage(StringhtmlCode,StringfileName){//S
5、tringfileName="";//文件的路径包含文件名//fileName="F:/mywork/printsd/WebRoot/aaa.html";try{Filefile=newFile(fileName);FileWriterresultFile=newFileWriter(file);//PrintWritermyFile=newPrintWriter(resultFile,"UTF-8");PrintWritermyFile=newPrintWriter(file,"UTF-8");//写文件myFile.println(htmlCode);resultFile.c
6、lose();myFile.close();}catch(Exceptione){e.printStackTrace();returnfalse;}finally{}returntrue;}publicstaticbooleansaveHtmlPage(StringhtmlCode,StringfileName,String encoding){//StringfileName="";//文件的路径包含文件名//fileName="F:/mywork/printsd/WebRoot/aaa.html";try{Filefile=newFile(fileName);FileWrit
7、erresultFile=newFileWriter(file);//PrintWritermyFile=newPrintWriter(resultFile,"UTF-8");PrintWritermyFile=newPrintWriter(file,encoding);//写文件myFile.println(htmlCode);resultFile.close();myFile.close();}catch(Exceptione){e.printStackTrace();ret
此文档下载收益归作者所有