Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

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 引入静态文件