您好 我是菜鸟 刚路过

duqiu155 2008-06-18 11:09:06
怎样用Java实现 输入一URL显示返回的HTML内容的JAR
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sd5816690 2008-06-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gray820928 的回复:]
这问题应该去java区问吧
[/Quote]
同意!!!

但是我刚好有这个代码,还是发上来
将http://www.sina.com/的HTML保存到c:\\shengchengdeHTML.html

import java.net.*;
import java.io.*;
import java.util.regex.Pattern;

public class CreateHTML {
public static void main(String[] args) {
CreateHTML uc = new CreateHTML();
uc.creatHTML("http://www.sina.com/", "c:\\shengchengdeHTML.html");
}

public void creatHTML(String webURL, String local) {
//new File(local);
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(local);
bw = new BufferedWriter(fw);
} catch (Exception ex) {
ex.printStackTrace();
}
StringBuffer document = new StringBuffer();
try {
URL url = new URL(webURL);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + " ");
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(document.toString());
String strTemp = document.toString();
String temp = null;
int j = 0;
for (int i = 0; i < strTemp.length(); i++) {
if (i<strTemp.length()&&strTemp.charAt(i) == '>') {
j = i;
i++;
if (i > strTemp.length() - 2) {
temp = strTemp;
addLine(temp, bw);
break;
}
while (Pattern.compile("\\s{1}").matcher("" + strTemp.charAt(i)).find()) { //跳过空格
i++;
}
if (i<strTemp.length()&&strTemp.charAt(i) == '<') {
temp = strTemp.substring(0, i);
strTemp = strTemp.substring(i, strTemp.length());
addLine(temp, bw);
i = 0;
} else {
i = j;
}
}

if (strTemp.substring(0, 6).equalsIgnoreCase("<style")){
while(true){
if (strTemp.charAt(i) == '}') {
temp = strTemp.substring(0, i + 1);
strTemp = strTemp.substring(i + 1, strTemp.length());
addLine(temp, bw);
i = 0;
}
if(strTemp.substring(i,i+8).equalsIgnoreCase("</style>")){
break;
}
i++;
}
i=0;
}

if (strTemp.substring(0, 7).equalsIgnoreCase("<script")){
while(true){
if (strTemp.charAt(i) == '{'||strTemp.charAt(i) == '}'||strTemp.charAt(i) == ';') {
temp = strTemp.substring(0, i + 1);
strTemp = strTemp.substring(i + 1, strTemp.length());
addLine(temp, bw);
i = -1;
}
i++;
if(i>=strTemp.length()-9){
break;
}
if(strTemp.substring(i,i+9).equalsIgnoreCase("</script>")){
temp = strTemp.substring(0, i);
strTemp = strTemp.substring(i, strTemp.length());
addLine(temp, bw);
i = 0;
break;
}
}
}
}
//将上面步骤忽略的代码加入HTML页面
while(Pattern.compile("\\s{1}").matcher("" + strTemp.charAt(0)).find()){//去掉首空格、\t等
strTemp=strTemp.substring(1);
}
if(strTemp.toLowerCase().indexOf("</html>")>0){//去掉首空格等后,如果不以</html>开头,则表示上面的步骤,没有完成了所有代码的格式化。这种情况需要将剩余代码加进HTML中
addLine(strTemp, bw);
}
try {
bw.flush();
bw.close();
fw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

private void addLine(String strLine, BufferedWriter bw) {
try {
bw.write(strLine);
bw.newLine();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
gray820928 2008-06-18
  • 打赏
  • 举报
回复
这问题应该去java区问吧
浴火_凤凰 2008-06-18
  • 打赏
  • 举报
回复
建议还是去java区问吧

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧