java 图片 https_https://www.meitulu.com/ java下载图片

1 packagedownloadpic;2

3 importorg.jsoup.Jsoup;4 importorg.jsoup.nodes.Document;5 importorg.jsoup.select.Elements;6

7 importjavax.print.Doc;8 importjava.io.File;9 importjava.io.FileOutputStream;10 importjava.io.IOException;11 importjava.io.InputStream;12 importjava.net.URL;13 importjava.net.URLConnection;14

15 public class donloadBeauty extendsconstantString {16 public static int target = 18;17 public static void main(String argv[]) throwsIOException {18 Document document = Jsoup.connect(newconstantString().url_root).get();19 //System.out.println(document.text());

20 Elements number_beauty_elements = document.getElementsByClass("p_title");21 int number_beauty =number_beauty_elements.size();22 System.out.println(number_beauty);23

24 //to get all the name and url

25 String[] beauty = newString[number_beauty];26 String[] beauty_title = newString[number_beauty];27 for (int i = 0; i < number_beauty; i++) {28 beauty[i] =number_beauty_elements.get(i)29 .select("a[href]").attr("href").toString();30 beauty_title[i] =number_beauty_elements.get(i)31 .text();32 System.out.println(i + ":\tfile name:" + beauty_title[i] + "\n" + "url is: " +beauty[i]);33 }34

35 //test just one url36 //to get user name

37 String user_home = "/home/" + System.getProperty("user.name") + "/Beauty/";38 //to creat folder

39 String file_full_path = user_home + beauty_title[target].replaceAll("/", "");40 File file = newFile(file_full_path);41 if(file.exists()) {42 System.out.println("file already exits");43 } else{44 file.mkdirs();45 }46

47 //get first element content

48 Document document1_beauty =Jsoup.connect(beauty[target]).get();49 String pic_number_string = null;50 //pic_number.startsWith("图片数量:");51 //int pic_number = Integer.parseInt(pic_number_string);

52 Elements elements_p = document1_beauty.getElementsByClass("c_l").select("p");53 int size_p =elements_p.size();54 int position = 0;55 for(int n = 0; n < size_p; n ++){56 pic_number_string =elements_p.get(n).text().toString();57 if(pic_number_string.endsWith("张")){58 position =n;59 break;60 }61 }62 pic_number_string = document1_beauty.getElementsByClass("c_l")63 .select("p").get(position).text().toString();64 System.out.println(pic_number_string);65

66 //to get the number

67 int index_last_0 = pic_number_string.lastIndexOf(" ");68 String temp_string = pic_number_string.substring(0, index_last_0);69 System.out.println(temp_string);70 int index_last_1 = temp_string.lastIndexOf(" ");71 //System.out.println(pic_number_string.substring(index_last_1));

72

73 String number_string =pic_number_string.substring(index_last_1, index_last_0)74 .replaceAll(" ", "");75 System.out.println(number_string);76 int pic_number =Integer.parseInt(number_string);77 System.out.println("the pic number is: " +pic_number);78

79 //get specific url

80 Document document_1 =Jsoup.connect(beauty[target]).get();81 Elements elements = newElements();82 int i = document_1.getElementsByClass("content").select("img").size();83

84 System.out.println("" +i);85 for (int j = 0; j < i; j++) {86 String href = document_1.getElementsByClass("content").select("img")87 .get(j).attr("src").toString();88 System.out.println(j + "," + "the url is:" +href);89 }90 String url_people = document_1.getElementsByClass("content").select("img")91 .get(0).attr("src").toString();92 System.out.println(url_people);93

94 //to get all the pic file url

95 int pic_index_last_0 = url_people.lastIndexOf("/");96 String temp_string_temp = url_people.substring(0, pic_index_last_0);//to gethttps://mtl.gzhuibei.com/images/img/20779

97 String userful_string = url_people.substring(pic_index_last_0);//to get 1.jpg

98 int to_get_dot = userful_string.lastIndexOf(".");99 String extension_string = userful_string.substring(to_get_dot);//to get .jpg

100 System.out.println(extension_string);101 System.out.println(userful_string);102 System.out.println(temp_string_temp);103 String[] beauty_full_url = newString[pic_number];104 for (int k = 0; k < pic_number; k++) {105 beauty_full_url[k] = temp_string_temp + "/" + (k+1) +extension_string;106 System.out.println(k +"\t" +beauty_full_url[k]);107 }108

109 //to download the file

110 int bytesum = 0;111 int byteread = 0;112 System.out.println(file_full_path + "\turl is:\t" + target + ":\t"+beauty[target]);113 //System.out.println(target + beauty[target]);

114 for(int l = 0; l < pic_number; l ++){115 URL url = newURL(beauty_full_url[l]);116 String file_name = l + 1 +extension_string;117 String file_full_name = file_full_path + "/" +file_name;118 File file_new = newFile(file_full_name);119 if(file_new.exists()){120 System.out.println("file already exists!");121 }else{122 try{123 URLConnection conn =url.openConnection();124 InputStream inStream =conn.getInputStream();125 FileOutputStream fs = newFileOutputStream(file_full_name);126

127 byte[] buffer = new byte[2048];128 intlength;129 while ((byteread = inStream.read(buffer)) != -1) {130 bytesum +=byteread;131 //System.out.println(bytesum / 1024);

132 fs.write(buffer, 0, byteread);133 }134 System.out.println("**********" + file_name + "**********");135

136 } catch(Exception e) {137 e.printStackTrace();138 }139 }140 }141 }142 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值