Skip to content

Commit 2114d9c

Browse files
committed
下载图片
1 parent 6ee8c3e commit 2114d9c

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package info.xiaomo.core.untils;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
import java.net.URL;
8+
import java.net.URLConnection;
9+
10+
/**
11+
* @author 小莫 (https://xiaomo.info) (https://github.com/syoubaku)
12+
* @created : 2016/12/26 13:25
13+
*/
14+
public class DownUtil {
15+
16+
public static void download(String urlString) throws Exception {
17+
File file = new File(urlString);
18+
String filename = file.getName();
19+
// 构造URL
20+
URL url = new URL(urlString);
21+
// 打开连接
22+
URLConnection con = url.openConnection();
23+
// 输入流
24+
InputStream is = con.getInputStream();
25+
// 1K的数据缓冲
26+
byte[] bs = new byte[1024];
27+
// 读取到的数据长度
28+
int len;
29+
// 输出的文件流
30+
OutputStream os = new FileOutputStream(filename);
31+
// 开始读取
32+
while ((len = is.read(bs)) != -1) {
33+
os.write(bs, 0, len);
34+
}
35+
// 完毕,关闭所有链接
36+
os.close();
37+
is.close();
38+
}
39+
40+
41+
/**
42+
* 下载图片
43+
* @param urlString url
44+
* @param filePath 存储路径 D:\MIR\config\data\
45+
*/
46+
public static void download(String urlString,String filePath) throws Exception {
47+
File file = new File(urlString);
48+
String filename = file.getName();
49+
// 构造URL
50+
URL url = new URL(urlString);
51+
// 打开连接
52+
URLConnection con = url.openConnection();
53+
// 输入流
54+
InputStream is = con.getInputStream();
55+
// 1K的数据缓冲
56+
byte[] bs = new byte[1024];
57+
// 读取到的数据长度
58+
int len;
59+
// 输出的文件流
60+
OutputStream os = new FileOutputStream(filePath+filename);
61+
// 开始读取
62+
while ((len = is.read(bs)) != -1) {
63+
os.write(bs, 0, len);
64+
}
65+
// 完毕,关闭所有链接
66+
os.close();
67+
is.close();
68+
}
69+
70+
}

crawler/src/main/java/info/xiaomo/crawler/schedule/ScheduledTasks.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package info.xiaomo.crawler.schedule;
22

3+
import info.xiaomo.core.untils.DownUtil;
34
import info.xiaomo.crawler.model.ShikigamiModel;
45
import info.xiaomo.crawler.service.ShikigamaService;
56
import info.xiaomo.crawler.spider.OnnmyoujiSpider;
@@ -38,11 +39,23 @@ public void reportCurrentTime() {
3839
//每1分钟执行一次
3940
@Scheduled(cron = "0 */1 * * * * ")
4041
public void reportCurrentByCron() {
42+
LOGGER.debug("开始执行任务:");
4143
List<ShikigamiModel> shikigamiModel = OnnmyoujiSpider.getShikigamiModel();
4244
shikigamiModel.forEach(shikigamaService::save);
45+
}
46+
47+
@Scheduled(fixedRate = 1000)
48+
public void downImage() throws Exception {
4349
LOGGER.debug("开始执行任务:");
50+
List<ShikigamiModel> shikigamiModel = shikigamaService.findAll();
51+
for (ShikigamiModel aShikigamiModel : shikigamiModel) {
52+
String url = aShikigamiModel.getImage();
53+
DownUtil.download(url, "C:\\Users\\xiaomo\\Desktop\\yss\\");
54+
LOGGER.debug("开始下载图片:{}", url);
55+
}
4456
}
4557

58+
4659
private SimpleDateFormat dateFormat() {
4760
return new SimpleDateFormat("HH:mm:ss");
4861
}

crawler/src/main/java/info/xiaomo/crawler/service/ShikigamaService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import info.xiaomo.crawler.model.ShikigamiModel;
44

5+
import java.util.List;
6+
57
/**
68
* @author 小莫 (https://xiaomo.info) (https://github.com/syoubaku)
79
* @created : 2016/12/24 15:54
@@ -12,4 +14,6 @@ public interface ShikigamaService {
1214

1315
void save(ShikigamiModel model);
1416

17+
List<ShikigamiModel> findAll();
18+
1519
}

crawler/src/main/java/info/xiaomo/crawler/service/impl/ShikigamaServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.stereotype.Service;
1010

11+
import java.util.List;
12+
1113
/**
1214
* @author 小莫 (https://xiaomo.info) (https://github.com/syoubaku)
1315
* @created : 2016/12/24 15:54
@@ -39,4 +41,9 @@ public void save(ShikigamiModel model) {
3941
LOGGER.debug("插入数据:{}", JSON.toJSONString(model));
4042
}
4143
}
44+
45+
@Override
46+
public List<ShikigamiModel> findAll() {
47+
return dao.findAll();
48+
}
4249
}

0 commit comments

Comments
 (0)