Java obtains network files and Inserts Database code

Source: Internet
Author: User

Get Baidu's song name, artist and link !! CopyCode The Code is as follows: Package WebTools;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. Io. unsupportedencodingexception;
Import java.net. malformedurlexception;
Import java.net. url;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import dbtools. dbtools;
Public class iotoweb {
Public String gethtmlcontent (string htmlurl ){
URL url = NULL;
String rowcontent = "";
Stringbuffer htmlcontent = new stringbuffer ();
Try {
Url = new URL (htmlurl );
Bufferedreader in = new bufferedreader (New inputstreamreader (URL
. Openstream (), "gb2312 "));
While (rowcontent = in. Readline ())! = NULL ){
Htmlcontent. append (rowcontent );
}
In. Close ();
} Catch (malformedurlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (unsupportedencodingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return htmlcontent. tostring ();
}
Public list getlink (string htmlcontent ){
Arraylist listlink = new arraylist ();
String RegEx = "<TD [^>] *> [\ (] * <A [^>] * href = (\" ([^ \ "] *) \ "| \ '([^ \'] *) \ '| ([^ \ s>] *) [^>] *> (. *?) [\)] * [\ S] * </TD> ";
Pattern pattern = pattern. Compile (RegEx, pattern. dotall );
Matcher = pattern. matcher (htmlcontent );
While (matcher. Find ()){
Listlink. Add (matcher. Group ());
}
Return listlink;
}
Public list <string> gethref (string htmlcontent ){
String RegEx;
List listthref = new arraylist ();
RegEx = "href = (\" ([^ \ "] *) \" | \ '([^ \'] *) \ '| ([^ \ s>] *) \ "";
Pattern Pa = pattern. Compile (RegEx, pattern. dotall );
Matcher MA = pa. matcher (htmlcontent );
While (MA. Find ()){
Listthref. Add (MA. Group (). replacefirst ("href = \" "," "). Replace ("\"",
""));
}
Return listthref;
}
Public list <string> getperson (string htmlcontent ){
String RegEx;
List list = new arraylist ();
RegEx = "\ (<A [^>] * href = (\" ([^ \ "] *) \" | \ '([^ \'] *) \ '| ([^ \ s>] *) [^>] *> (. *?) \\)";
Pattern Pa = pattern. Compile (RegEx, pattern. dotall );
Matcher MA = pa. matcher (htmlcontent );
While (MA. Find ()){
List. Add (MA. Group (). replacefirst ("href = \" "," "). Replace ("\"",""));
}
Return list;
}
Public list <string> getsongname (string htmlcontent ){
String RegEx;
List listperson = new arraylist ();
RegEx = "<A [^>] * href = (\" ([^ \ "] *) \" | \ '([^ \'] *) \ '| ([^ \ s>] *) [^>] *> (. *?) </A> \ s ";
Pattern Pa = pattern. Compile (RegEx, pattern. dotall );
Matcher MA = pa. matcher (htmlcontent );
While (MA. Find ()){
Listperson. Add (MA. Group ());
}
Return listperson;
}
Public String getmaincontent (string htmlcontent ){
String RegEx = "<Table width = \" 100% \ "align = \" center \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "class = \" list \ "> (. *?) </Table> ";
Stringbuffer maincontent = new stringbuffer ();
Pattern pattern = pattern. Compile (RegEx, pattern. dotall );
Matcher = pattern. matcher (htmlcontent );
While (matcher. Find ()){
Maincontent. append (matcher. Group ());
}
Return maincontent. tostring ();
}
Public String outtag (final string s ){
Return S. replaceall ("<. *?> ","");
}
Dbtools = new dbtools ();
Public void getfrombaidumap3 (string htmlurl) throws throwable {
Hashmap htmlcontentmap = new hashmap ();
String htmlcontent = gethtmlcontent (htmlurl );
String maincontent = getmaincontent (htmlcontent );
List listlink = getlink (maincontent );
For (Int J = 0; j <listlink. Size (); j ++ ){
String tdtag = listlink. Get (j). tostring ();
List songnamelist = getsongname (tdtag );
String songname = outtag (songnamelist. Get (0). tostring ());
List personlist = getperson (tdtag );
String songperson = "";
If (personlist. Size ()! = 0 ){
For (INT n = 0; n <personlist. Size (); N ++ ){
// System. Out. println (personlist. Get (N). tostring ());
Songperson = outtag (personlist. Get (N). tostring ());
}
} Else {
Songperson = "NONE ";
}
// System. Out. Print (songnamelist. Get (0). tostring ());
List hreflist = gethref (songnamelist. Get (0). tostring ());
String songhref = hreflist. Get (0). tostring ();
System. Out. println ();
String SQL = "insert into song (songname, songperson, songhref) values (?,?,?) ";
Arraylist list_values = new arraylist ();
List_values.add (songname );
List_values.add (songperson );
List_values.add (songhref );
Dbtools. Update (SQL, list_values );
}
}
}

Dbtools database link class: Copy code The Code is as follows: Package dbtools;
Import java. util. arraylist;
Import java. SQL .*;
Public class dbtools {
Private preparedstatement;
Private resultset;
Private connection;
Public dbtools (){
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
} Catch (classnotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
Connection = drivermanager. getconnection (
"JDBC: mysql: // localhost: 3306/testurl", "root", "zhuyi ");
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Public arraylist query (string SQL, arraylist list_values) throws throwable {
Arraylist listrows = new arraylist ();
Preparedstatement = connection. preparestatement (SQL );
For (INT I = 0; I <list_values.size (); I ++ ){
Preparedstatement. setobject (I + 1, list_values.get (I ));
}
Resultset = preparedstatement.exe cutequery ();
While (resultset. Next ()){
String [] rowinfo = new string [resultset. getmetadata ()
. Getcolumncount ()];
For (INT I = 0; I <rowinfo. length; I ++ ){
Rowinfo [I] = resultset. getstring (I + 1 );
}
Listrows. Add (rowinfo );
}
Return listrows;
}
Public void Update (string SQL, arraylist list_values) throws throwable {
Preparedstatement = connection. preparestatement (SQL );
For (INT I = 0; I <list_values.size (); I ++ ){
Preparedstatement. setobject (I + 1, list_values.get (I ));
}
Preparedstatement.exe cuteupdate ();
Preparedstatement. Close ();
}
}

Servlet call:Copy code The Code is as follows: Package controller;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. List;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import WebTools. iotoweb;
Public class testurl extends httpservlet {
/**
* Constructor of the object.
*/
Public testurl (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}
/**
* The doget method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to get.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws servletexception
* If an error occurred
* @ Throws ioexception
* If an error occurred
*/
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Try {
Iotoweb = new iotoweb ();
Iotoweb. getfrombaidumap3 ("http://list.mp3.baidu.com/topso/mp3topsong.html? Id = 1? Top2 ");
} Catch (throwable e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
/**
* The dopost method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals
* Post.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws servletexception
* If an error occurred
* @ Throws ioexception
* If an error occurred
*/
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out
. Println ("<! Doctype HTML public \ "-// W3C // dtd html 4.01 transitional // en \"> ");
Out. println ("<HTML> ");
Out. println ("Out. println ("<body> ");
Out. Print ("this is ");
Out. Print (this. getclass ());
Out. println (", using the POST method ");
Out. println ("</body> ");
Out. println ("Out. Flush ();
Out. Close ();
}
/**
* Initialization of the servlet. <br>
*
* @ Throws servletexception
* If an error occurs
*/
Public void Init () throws servletexception {
// Put your code here
}
}

Obtain the name of a book on the golden book Website: Copy code The Code is as follows: Package WebTools;
Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import java.net. url;
Import java. util. arraylist;
Import java. util. List;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import dbtools. dbtools;
Public class getbook {
Public String gethtmlcontent (string htmlurl) throws throwable {
URL url = NULL;
String rowcontent = "";
Stringbuffer htmlcontent = new stringbuffer ();
Url = new URL (htmlurl );
Bufferedreader in = new bufferedreader (New inputstreamreader (URL
. Openstream (), "gb2312 "));
While (rowcontent = in. Readline ())! = NULL ){
Htmlcontent. append (rowcontent );
}
In. Close ();
Return htmlcontent. tostring ();
}
Public String getbookname (string htmlcontent ){
String bookname = "";
String RegEx = "<SPAN class = \" style15 \ "> [^>] * </span> ";
Pattern pattern = pattern. Compile (RegEx, pattern. dotall );
Matcher = pattern. matcher (htmlcontent );
If (matcher. Find ()){
Bookname = matcher. Group ();
}
Return bookname;
}
Public String outtag (final string s ){
Return S. replaceall ("<. *?> ","");
}
Dbtools = new dbtools ();
Public void getfromjinshu (string htmlurl) throws throwable {
String htmlcontent = gethtmlcontent (htmlurl );
String bookname = outtag (getbookname (htmlcontent ));
If (bookname! = NULL &&! "". Equals (bookname )){
System. Out. println (bookname );
String SQL = "insert into bookinfo (bookname) values (?) ";
Arraylist list_values = new arraylist ();
List_values.add (bookname );
Dbtools. Update (SQL, list_values );
}
}
}

Call servlet: Copy code The Code is as follows: Package controller;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import WebTools. getbook;
Public class testbook extends httpservlet {
/**
* Constructor of the object.
*/
Public testbook (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}
/**
* The doget method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to get.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws servletexception
* If an error occurred
* @ Throws ioexception
* If an error occurred
*/
Int I = 1;
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Getbook bookinfo = new getbook ();
For (; I <10000; I ++ ){
String bookurl = "http://www.golden-book.com/booksinfo/12/" + I
+ ". Html ";
Try {
Bookinfo. getfromjinshu (bookurl );
} Catch (throwable e ){
I ++;
Dopost (request, response );
}
}
}
/**
* The dopost method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals
* Post.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws servletexception
* If an error occurred
* @ Throws ioexception
* If an error occurred
*/
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Getbook bookinfo = new getbook ();
For (; I <10000; I ++ ){
String bookurl = "http://www.golden-book.com/booksinfo/12/" + I
+ ". Html ";
Try {
Bookinfo. getfromjinshu (bookurl );
} Catch (throwable e ){
I ++;
Doget (request, response );
}
}
}
/**
* Initialization of the servlet. <br>
*
* @ Throws servletexception
* If an error occurs
*/
Public void Init () throws servletexception {
// Put your code here
}
}

There are many implementation methods for each function. I hope you can share different ideas and methods. You can add qq412546724. Haha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.