java手机壁纸_java-爬取手机高清壁纸

public class DownWallpaper extends JFrame implementsActionListener{private JButton down = null;publicDownWallpaper(){

down= new JButton("下载手机壁纸");

down.setFont(new Font("微软雅黑",Font.ITALIC,20));

down.addActionListener(this);this.setResizable(false);this.add(down);this.setTitle("高清壁纸下载");this.setSize(250, 150);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);this.setVisible(true);

}

@Overridepublic voidactionPerformed(ActionEvent e) {if (e.getSource()==down) {try{

JOptionPane.showMessageDialog(this, "正在下载请勿关闭主窗体!");

JOptionPane.showMessageDialog(this, "详细信息请查看:https://sj.enterdesk.com/");

JOptionPane.showMessageDialog(this, "下载完成后请到:d:/img下查看");

load("https://sj.enterdesk.com/");

}catch(Exception e1) {

e1.printStackTrace();

}

}

}public static voidmain(String[] args) {newDownWallpaper();

}/*** 加载链接

*@paramurls

*@throwsException*/

public static void load(String urls) throwsException{

Connection connect=Jsoup.connect(urls);

Document document=connect.get();

Elements links= document.getElementsByTag("img");//循环爬取图片

for(Element link : links){

String url= link.attr("src");//下载的url

String endWith = url.substring(url.lastIndexOf("."));//文件后缀名

String fileName = link.absUrl("alt").substring(link.absUrl("alt").lastIndexOf("/"));//文件名

download(url,endWith,fileName);

}//获取所有的

Elements select = document.select("a");

Elements addClass= select.addClass("next_p");for(Element element : addClass) {if (element.text().equals("下一页")) {//获取超链接

String attr = element.attr("href");//递归循环下载

load(attr);

}else{continue;

}

}

}/*** 下载图片

*@paramurl*/

public static void download(String url,String endWith,String fileName) throwsException{

File file= new File("d:/img/");if (!file.exists()){

file.mkdir();

}else{

file.delete();

file.mkdir();

}

URL url2= newURL(url);

InputStream is=url2.openConnection().getInputStream();

BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream(file.getPath()+fileName+endWith));byte[] bs = new byte[2048*2048];while((is.read(bs))!=-1){

bos.write(bs);

}

bos.flush();if(is != null) is.close();if(bos != null) bos.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值