Spring Boot使用RestTemplate批量下载文件

Spring Boot使用RestTemplate批量下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
private TestRestTemplate template = null;

@Before
public void testBefore() {
template = new TestRestTemplate();
}

@Test
public void getGoodsPicUrlList() throws IOException {
File dir = new File("E:/appblog.cn/goods/pic");
if (!dir.exists()) {
dir.mkdirs();
}

LitemallGoodsExample example = new LitemallGoodsExample();
List<LitemallGoods> goodsList = goodsMapper.selectByExampleSelective(example, LitemallGoods.Column.picUrl);
LitemallGoods goods;
for (int i = 0; i < goodsList.size(); i++) {
goods = goodsList.get(i);
String picUrl = goods.getPicUrl();
log.info(goods.getPicUrl());

HttpHeaders headers = new HttpHeaders();
HttpEntity<Resource> httpEntity = new HttpEntity<>(headers);
ResponseEntity<byte[]> response = template.exchange(picUrl, HttpMethod.GET, httpEntity, byte[].class);
log.info("状态码 >> {}", response.getStatusCodeValue());
//log.info(">> {}", response.getHeaders().getContentType());
//log.info(">> {}", response.getHeaders().getContentType().getSubtype());
try {
File file = new File(dir, picUrl.substring(picUrl.lastIndexOf('/')));
log.info(file.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(file);
fos.write(response.getBody());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Test
public void getGoodsGalleryList() throws IOException, InterruptedException {
File dir = new File("E:/litemall/goods/gallery");
if (!dir.exists()) {
dir.mkdirs();
}

LitemallGoodsExample example = new LitemallGoodsExample();
List<LitemallGoods> goodsList = goodsMapper.selectByExampleSelective(example, LitemallGoods.Column.gallery);
LitemallGoods goods;
for (int i = 0; i < goodsList.size(); i++) {
goods = goodsList.get(i);
String[] galleries = goods.getGallery();
for (int j = 0; j < galleries.length; j++) {
String gallery = galleries[j];
log.info(gallery);

HttpHeaders headers = new HttpHeaders();
HttpEntity<Resource> httpEntity = new HttpEntity<>(headers);
ResponseEntity<byte[]> response = template.exchange(gallery, HttpMethod.GET, httpEntity, byte[].class);
log.info("状态码 >> {}", response.getStatusCodeValue());
//log.info(">> {}", response.getHeaders().getContentType());
//log.info(">> {}", response.getHeaders().getContentType().getSubtype());
try {
File file = new File(dir, gallery.substring(gallery.lastIndexOf('/')));
log.info(file.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(file);
fos.write(response.getBody());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
Thread.sleep(100);
}
}
}

@Test
public void updateGoodsPicUrlList() throws IOException {
LitemallGoodsExample example = new LitemallGoodsExample();
List<LitemallGoods> goodsList = goodsMapper.selectByExampleSelective(example, LitemallGoods.Column.id, LitemallGoods.Column.picUrl);
LitemallGoods goods;
for (int i = 0; i < goodsList.size(); i++) {
goods = goodsList.get(i);
goods.setPicUrl(goods.getPicUrl().replace("http://www.yezhou.cc", "http://www.appblog.cn"));
goodsMapper.updateByPrimaryKeySelective(goods);
}
}

@Test
public void updateGoodsGalleryList() throws IOException, InterruptedException {
LitemallGoodsExample example = new LitemallGoodsExample();
List<LitemallGoods> goodsList = goodsMapper.selectByExampleSelective(example, LitemallGoods.Column.id, LitemallGoods.Column.gallery);
LitemallGoods goods;
for (int i = 0; i < goodsList.size(); i++) {
goods = goodsList.get(i);
String[] galleries = goods.getGallery();
String[] newGalleries = new String[galleries.length];
for (int j = 0; j < galleries.length; j++) {
newGalleries[j] = galleries[j].replace("http://www.yezhou.cc", "http://www.appblog.cn");
}
goods.setGallery(newGalleries);
goodsMapper.updateByPrimaryKeySelective(goods);
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :