Log on to download the Java code for network resources and the java code for network resources.

Source: Internet
Author: User

Log on to download the Java code for network resources and the java code for network resources.

DevStore, a source code download site. It is too troublesome to download the source code one by one, so I want to write a batch download.

First, let's take a look at the process of a single download.

For example, if you want to forward text messages to a specified mobile phone number by keyword, You need to log on when downloading the phone number. register an account here to download the phone number.





Here there will be two Post

One is to check the activity value spent, and the other is to deduct the activity value

Post-> Get

The two addresses are:


Http://www.devstore.cn/code/checkDownSourceCodeUserPointsAjax? SourceCodeId = 715 response: <pre role = "list"> <code class = "wrappedText focusRow" role = "listitem"> {"price": 0, "status ": 2} </code>



http://www.devstore.cn/code/costSourceCodeUserPointsAjax?sourceCodeId=715

Response :{
"Status": 1,
"Url": "success? Else"
}

 

Actually

Bytes

Downloaded code

/***** @ Param url * @ param localPath saved path */public static void downLoad (String url, String localPath) {URL fileUrl = null; HttpURLConnection httpUrl = null; bufferedInputStream bis = null; BufferedOutputStream bos = null; File file = new File (localPath); try {fileUrl = new URL (url); httpUrl = (HttpURLConnection) fileUrl. openConnection (); httpUrl. connect (); bis = new BufferedInputStream (httpUrl. getInputStre Am (); bos = new BufferedOutputStream (new FileOutputStream (file); int len = 2048; byte [] B = new byte [len]; while (len = bis. read (B ))! =-1) {bos. write (B, 0, len);} bos. flush (); bis. close (); httpUrl. disconnect ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {try {bis. close (); bos. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

The downloaded code is ready. The next step is to log on and obtain the code.

Paste the Code directly.

Public class DownLoad {/*** @ param userName * @ param passWord * @ return cookie */public static Map <String, String> login (String userName, String passWord) {Map <String, String> cookies = null; Map <String, String> map = new HashMap <String, String> (); map. put ("pwd", passWord); map. put ("userName", userName); Response response = null; try {response = Jsoup. connect ("http://www.devstore.cn/user/login "). timeout (5000 ). data (Map).ignorecontenttype(true0000.exe cute ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} cookies = response. cookies (); return cookies ;} /*** get the download link * @ param url the original address for download * @ param cookies * @ return */public static String getdownUrl (String url, Map <String, string> cookies) {String result = null; Response response = null; JSONObject json = null; String id = (getInfoByUrl (url )). Split ("\\. ") [0]; try {response = Jsoup. connect ("http://www.devstore.cn/code/checkDownSourceCodeUserPointsAjax "). timeout (5000 ). data ("sourceCodeId", id).cookies(cookies).ignorecontenttype(true0000.exe cute (); json = new JSONObject (response. body (); result = json. get ("url "). toString (). split ("\\? ") [0];} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (JSONException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return result;}/*** intercept * @ param url * @ return */public static String getInfoByUrl (String url) {String result = null; int length = url. split ("\\/"). length; result = url. split ("\/") [length-1]; return result;}/***** @ param url * @ param Path saved by localPath */public static void downLoad (String url, String localPath) {String fileName = null; URL fileUrl = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; bufferedOutputStream bos = null; fileName = getInfoByUrl (url); File file = new File (localPath + File. separator + fileName); try {fileUrl = new URL (url); httpUrl = (HttpURLConnection) fileUrl. openConnection (); httpUrl. connect (); Bis = new BufferedInputStream (httpUrl. getInputStream (); bos = new BufferedOutputStream (new FileOutputStream (file); int len = 2048; byte [] B = new byte [len]; while (len = bis. read (B ))! =-1) {bos. write (B, 0, len);} bos. flush (); bis. close (); httpUrl. disconnect ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {try {bis. close (); bos. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}

Test code

Map <String, String> cookies = DownLoad. login ("username", "password"); String url = DownLoad. getdownUrl ("http://www.devstore.cn/code/info/715.html", cookies); DownLoad. downLoad (url, "G: \ src ");


For batch download, you can obtain all the download links on the page first.



Document doc = Jsoup.connect("http://www.devstore.cn/code/list/pn1-or0.html").get();Element element = doc.select("ul.source_list_ul").first();//System.out.println(element);Elements links = element.select("h4").select("a");for (Element link : links) {System.out.println(link.attr("href"));}

The Code is as above.

The returned results are similar.

/code/info/715.html/code/info/712.html/code/info/713.html/code/info/714.html/code/info/708.html........

The link is available, and the download code can be executed cyclically.


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.