登入下載網路資源之Java代碼,網路資源java代碼

來源:互聯網
上載者:User

登入下載網路資源之Java代碼,網路資源java代碼

DevStore,一個源碼的下載站。下載源碼一個一個下載太麻煩了,所以就想著寫個批量下載的。

首先看看單個下載的過程。

例如按關鍵詞轉寄簡訊至指定手機號,當下載時需要登入,這裡註冊一個帳號,進行下載。





這裡會有兩個Post

一個是檢查花費的活力值,一個是扣除活力值

Post->Get

兩個地址分別為:


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



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

響應:{
    "status": 1,
    "url": "http://ds.devstore.cn/dev_store/user/souce_code/file/20150227043055827e0vbr74d/按關鍵詞轉寄簡訊至指定手機號.zip?87d45372b2babb3a34570fd107cac9747900e64e21876aa613ac0910e7d5d689b9a4cdf80e67f3bac16cad6fa4ca5b88d818e8a07f8f44e19604f2362a677492501b7eed6bbbc114e1687347f00ea624a069c08baf721201f502b82d3ccc3197"
}


真實的是

http://ds.devstore.cn/dev_store/user/souce_code/file/20150227043055827e0vbr74d/按關鍵詞轉寄簡訊至指定手機號.zip

下載的代碼

/** *  * @param url   * @param localPath 儲存的路徑 */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.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();}}}

下載的代碼好了,接下來就是登陸和擷取的代碼

直接把代碼貼上吧

public class DownLoad {/** * @param 使用者名稱 * @param 密碼 * @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(true).execute();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}cookies = response.cookies();return cookies;}/** * 擷取下載的連結 * @param url  下載的原地址 * @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(true).execute();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;}/** * 截取 * @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 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();}}}}

測試代碼

Map<String, String> cookies = DownLoad.login("使用者名稱", "密碼");String url = DownLoad.getdownUrl("http://www.devstore.cn/code/info/715.html", cookies);DownLoad.downLoad(url, "G:\\src");


批量下載的話可以先擷取頁面中所有的下載連結



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"));}

代碼如上。

返回結果類似

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

連結有了,然後迴圈執行下載代碼即可


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.