有下面一段代码,希望哪个高手帮我改成一个带两个参数的方法(url,surl),我是实在不会,谢谢了!
package shengang.bole;import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.Date;
public class MakeHtml { private static long star = 0; private static long end = 0; private static long ttime = 0; //返回html代码 public static String getHtmlCode(String httpUrl){ Date before = new Date(); star = before.getTime(); String htmlCode = ""; try { InputStream in; URL url = new java.net.URL(httpUrl); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent","Mozilla/4.0"); connection.connect(); in = connection.getInputStream(); java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in , "gb2312")); String currentLine; while((currentLine=breader.readLine())!=null){ htmlCode+=currentLine; } } catch (Exception e) { e.printStackTrace(); }finally{ Date after = new Date(); end = after.getTime(); ttime = end-star ; System.out.println("执行时间:"+ttime +"秒"); } return htmlCode; } //存储文件 public static synchronized void writeHtml(String filePath,String info,String flag) { PrintWriter pw = null; try { File writeFile = new File(filePath); boolean isExit = writeFile.exists(); if (isExit != true) { writeFile.createNewFile(); } else { if (!flag.equals("NO")) { writeFile.delete(); writeFile.createNewFile(); } } pw = new PrintWriter(new FileOutputStream(filePath, true)); pw.println(info); pw.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); }finally{ pw.close(); } } public static void main(String[] args) { String url = "http://localhost:8080/mynews/index.jsp";
String surl="c:/demo.htm";
writeHtml(surl,getHtmlCode(url),"YES"); } }
package shengang.bole;import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.Date;
public class MakeHtml { private static long star = 0; private static long end = 0; private static long ttime = 0; //返回html代码 public static String getHtmlCode(String httpUrl){ Date before = new Date(); star = before.getTime(); String htmlCode = ""; try { InputStream in; URL url = new java.net.URL(httpUrl); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent","Mozilla/4.0"); connection.connect(); in = connection.getInputStream(); java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in , "gb2312")); String currentLine; while((currentLine=breader.readLine())!=null){ htmlCode+=currentLine; } } catch (Exception e) { e.printStackTrace(); }finally{ Date after = new Date(); end = after.getTime(); ttime = end-star ; System.out.println("执行时间:"+ttime +"秒"); } return htmlCode; } //存储文件 public static synchronized void writeHtml(String filePath,String info,String flag) { PrintWriter pw = null; try { File writeFile = new File(filePath); boolean isExit = writeFile.exists(); if (isExit != true) { writeFile.createNewFile(); } else { if (!flag.equals("NO")) { writeFile.delete(); writeFile.createNewFile(); } } pw = new PrintWriter(new FileOutputStream(filePath, true)); pw.println(info); pw.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); }finally{ pw.close(); } } public static void main(String[] args) { String url = "http://localhost:8080/mynews/index.jsp";
String surl="c:/demo.htm";
writeHtml(surl,getHtmlCode(url),"YES"); } }
楼主【sgbole123】截止到2008-06-28 10:49:19的历史汇总数据(不包括此帖):
发帖数:4 发帖分:300
结贴数:1 结贴分:80
未结数:3 未结分:220
结贴率:25.00 % 结分率:26.67 %
楼主该结一些帖子了
{
String info = getHtmlCode(httpUrl);
PrintWriter pw = null;
try
{
File writeFile = new File(filePath);
boolean isExit = writeFile.exists();
if (isExit != true)
{
writeFile.createNewFile();
}
else
{
writeFile.delete();
writeFile.createNewFile();
} pw = new PrintWriter(new FileOutputStream(filePath, true));
pw.println(info);
pw.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
finally
{
pw.close();
}
}
* url:网址
* surl:保存到哪一个文件*/
public saveHTML(String url,String surl){
writeHtml(surl,getHtmlCode(url),"NO");//YES或NO,自己确定
}
调用的时候:
String url = "http://localhost:8080/mynews/index.jsp";
String surl="c:/demo.htm";
saveHTML(url,surl);