好友
阅读权限10
听众
最后登录1970-1-1
|
天王老子
发表于 2019-11-26 10:47
你还在为没有还看的壁纸而烦恼吗?
你还在为查找壁纸要进网站而烦恼吗?
那么 ,今天它来了
纯粹的由Java语言开发,公布源码 ,你也可以成为一代"大师"
那么,动手试试吧!
[Java] 纯文本查看 复制代码 package com.wsc;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import cn.hutool.core.codec.Base64Decoder;
public class MyCanvas extends Canvas{
private String urlPath;
private int width;
private int height;
MyCanvas(int width,int height){
this.width = width;
this.height = height;
}
public void setUrlPath(String urlPath) {
this.urlPath = urlPath;
}
public void setBounds(int left, int top) {
// TODO Auto-generated method stub
super.setBounds(left, top, width, height);
}
public Image getImgBytes() {
if(urlPath==null) {
return null;
}
int offset = urlPath.lastIndexOf("/");
if(offset==-1){
return null;
}
String fileName = urlPath.substring(offset+1);
InputStream inputStream = null;
try {
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
inputStream = urlConnection.getInputStream();
Image read = ImageIO.read(inputStream);
return read;
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}finally {
try {
if(inputStream!=null) {
inputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
Image img = getImgBytes();
if(img!=null) {
g.drawImage(img, 0, 0,this.width, this.height, null);
}
}
public static void decodeBase64(String str) throws IOException {
// 判断是否为base64加密的地址
int offset;
int index=1;
File file = new File("C:\\"+index+".png");
while(true) {
if(file.exists()) {
file = new File("C:\\"+(++index)+".png");
}else {
break;
}
}
FileOutputStream out = new FileOutputStream(file);
if((str.indexOf("data"))!=-1 &&(offset = str.indexOf("base64"))!=-1) {
str = str.substring(offset+7);
byte[] bytes = Base64Decoder.decode(str);
out.write(bytes);
JOptionPane.showMessageDialog(null, "");
}
out.close();
}
}
[Java] 纯文本查看 复制代码 package com.wsc;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
public class imgList extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JButton button;
private JList list;
private MyCanvas canvas;
private JButton download;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
imgList frame = new imgList();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public imgList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1060, 412);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUrl = new JLabel("URL:");
lblUrl.setFont(new Font("宋体", Font.PLAIN, 15));
lblUrl.setBounds(10, 10, 34, 22);
contentPane.add(lblUrl);
textField = new JTextField("http://www.netbian.com/baidu/");
textField.setBounds(53, 10, 476, 21);
contentPane.add(textField);
textField.setColumns(10);
button = new JButton("\u67E5\u8BE2");
button.setBounds(563, 10, 93, 23);
contentPane.add(button);
list = new JList();
list.setBounds(20, 47, 628, 205);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setBounds(10, 47, 646, 316);
scrollPane.add(list);
contentPane.add(scrollPane);
canvas = new MyCanvas(359, 245);
canvas.setBounds(675, 47);
contentPane.add(canvas);
download = new JButton("\u4E0B\u8F7D");
download.setBounds(673, 340, 93, 23);
contentPane.add(download);
init();
}
public void init() {
Object[] objs = {"目前只支持GET请求,还是不带请求头的那种,虽然那种自带请求头的不难,但是不想写了,SWing已经落后了"};
list.setListData(objs);
event();
}
public void event() {
String urlPath = null;
// 点击查询事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String text = textField.getText();
Vector<String> set = imgList(text);
list.setListData(set);
}
});
// 列表里的item被选中事件
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Object[] selectedValues = list.getSelectedValues();
if(selectedValues.length==1) {
canvas.setUrlPath((String)selectedValues[0]);
canvas.repaint();
}
}
});
download.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub[/font][/size]
[size=4][font=宋体][size=4][font=宋体]String savePath = null;[/font][/size][/font][/size]
[size=4][font=宋体] Object[] selectedValues = list.getSelectedValues();
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// 开启选择器,如果选中了
int flag = choose.showOpenDialog(contentPane);
//如果是确定按钮
if(flag==JFileChooser.APPROVE_OPTION){
File file = choose.getSelectedFile();
savePath = file.getAbsolutePath();
}else{[/font][/size]
[size=4][font=宋体]return;
[/font][/size]
[size=4][font=宋体]}
[/font][/size]
[size=4][font=宋体] for(Object obj:selectedValues){
String uri = (String)obj;
boolean b = downFile(uri, savePath);
if(!b){
JOptionPane.showMessageDialog(null, "图片:"+uri+"下载失败");
}
}
JOptionPane.showMessageDialog(null, "下载完成");
}
});
}
public Vector<String> imgList(String url){
Vector<String> set = new Vector<>();
Document doc = null;
Connection connect;
connect = Jsoup.connect(url);
// Elements lis = doc.select("#main").select(".list").select("ul").select("li");
Elements lis = doc.select("img");
for(Element li :lis){
// String flag = li.select("a").select("img").get(0).attr("src");
String flag = li.attr("src");
if(!flag.startsWith("http") && !flag.isEmpty()) {
flag = "https:"+flag;
}
set.add(flag);
}
return set;
}
public static boolean downFile( String urlPath, String savePath){
if(urlPath==null || savePath==null){
return false;
}
int offset = urlPath.lastIndexOf("/");
if(offset==-1){
return false;
}
String fileName = urlPath.substring(offset+1);
if(!fileName.endsWith(".png")||!fileName.endsWith(".jgp")) {
fileName = fileName +".png";
}
File file = new File(savePath,fileName);
try {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
URL url = new URL(urlPath);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
if(urlConnection.getResponseCode() != 200){
return false;
}
// 设置请求头处理urlConnection.setRequestProperty("key","value");
BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
byte[] bytes = new byte[1024];
int len =0;
while ((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
inputStream.close();
outputStream.close();
return true;
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
}[/font][/size]
[size=4][font=宋体]
源码地址 https://gitee.com/deathgod/ourStudy.git
成品自己编译
效果:
支持批量下载
|
免费评分
-
查看全部评分
|