使用NIO快速下载文件

Observable.fromIterable(itemImageMapper.findLostUrlItemImageList(i, i + 100))
                        .filter(itemImageDO -> StringUtils.isNotEmpty(itemImageDO.getOriUrl()))
                        .observeOn(Schedulers.io())
                        .map(itemImageDO -> {
                            log.info("Randy-download-run on :" + Thread.currentThread().getName());
                            File imgFile = File.createTempFile("FIX_IMG_", ".jpg");
                            try (InputStream inputStream = new URL(itemImageDO.getOriUrl()).openStream()) {
                                Path target = Paths.get(imgFile.getParent(), imgFile.getName());
                                Files.copy(inputStream, target, StandardCopyOption.REPLACE_EXISTING);
                                QiNiuUploadResp qiNiuUploadResp = qiniuUploadService.upload(imgFile.getParent() + File.separator, imgFile.getName());
                                itemImageDO.setUrl(qiNiuUploadResp.getPath());
                            } catch (IOException e) {
                                log.warn("文件处理失败", e);
                            } finally {
                                imgFile.deleteOnExit();
                            }
                            return itemImageDO;
                        })
                        .filter(itemImageDO -> StringUtils.isNotEmpty(itemImageDO.getUrl()))
                        .observeOn(Schedulers.single())
                        .map(itemImageDO -> {
                            log.info("Randy-updateQiNiuUrl-run on :" + Thread.currentThread().getName());
                            int ret = itemImageMapper.updateQiNiuUrl(itemImageDO.getId(), itemImageDO.getUrl());
                            return ret;
                        })
                        .subscribe();

你可能感兴趣的:(使用NIO快速下载文件)