欢迎光临
我们一直在努力

用JSP抓取网址-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

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

class url2htm{

static private url2htm instance; // create the only instance of the class

public static string strurl=””; // announce the universial variable to mark the destination url

public static string strfile=””; // announce the universial variable to mark the local file path

/**
* construct the private function to prevent other
* application from creating the instance of this class
*/
private url2htm(){

init();

}
/**
* the main function that will invoke the application
*/
public static void main(string args[]){

url2htm insturl2htm = new url2htm(); // create the instance of the default class

insturl2htm.write(); // invoke the chief function
}

/**
* the setproperties() function will set the two major
* variables to the class
* @para propnames
* @para name
*/
private void setproperties(properties props){

enumeration propnames = props.propertynames();

while(propnames.hasmoreelements()){

string name = (string) propnames.nextelement();

if(name.endswith(“.url”)){

string webname = name.substring(0,name.lastindexof(“.”));

strurl = props.getproperty(webname + “.url”);

strfile = props.getproperty(webname + “.file”);

}
}
}

/**
* the geturl() function will return the url string
*/
private void geturl(){

system.out.println(strurl);

}

/**
* the getfile() function will return the local file and path
*/
private void getfile(){

system.out.println(strfile);

}

/**
* the write() function will read the dest url as input stream and
* write into a local file
* @para fileline
* @para url
*/
private static void write(){

string fileline;

string url = strurl;

try {

url desturl = new url(url);

inputstream in = desturl.openstream();

bufferedreader filedata = new bufferedreader(new inputstreamreader(in));

fileoutputstream out = new fileoutputstream(strfile);

printstream prtstream = new printstream(out);

while ((fileline = filedata.readline()) != null) {

prtstream.println(fileline + “\n”);

}
prtstream.close();

}

catch (ioexception e) {

system.out.println(“error in i/o:” + e.getmessage());

}

}

/**
* the init() function will read the property file and set the
* class constants
* @para is: the input stream
* @para webprop: the web site properties
*/
private void init()
{

inputstream is = getclass().getresourceasstream(“web.properties”); // data input stream

properties webprops = new properties();

try
{
webprops.load(is);
}

catch (exception e)
{
system.err.println(“cant read the property file. ” + “please make sure the property file is in its path”);
return;
}
setproperties(webprops);
}
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用JSP抓取网址-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章