Spring Boot使用RestTemplate批量下载文件

Spring Boot使用RestTemplate批量下载文件

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);
    }
}
上一篇 Spring Boot使用RestTemplate发送get请求,获取不到参数的问题
下一篇 Spring Boot Thymeleaf 引入静态文件
目录
文章列表
1 DialogFragment调用show()报Can not perform this action after onSaveInstanceState的解决办法
DialogFragment调用show()报Can not perform this action after onSaveInstanceState的解决办法
2
Swift - 协议(protocol)
Swift - 协议(protocol)
3
高中数学基础:角的概念与三角常用公式
高中数学基础:角的概念与三角常用公式
4
Filebeat+ElasticSearch+Grafana实现Nginx日志监控
Filebeat+ElasticSearch+Grafana实现Nginx日志监控
5
Spring Boot中MyBatis打印sql
Spring Boot中MyBatis打印sql
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。