diff --git a/src/main/java/vip/comic18/finder/service/AsyncTaskService.java b/src/main/java/vip/comic18/finder/service/AsyncTaskService.java index 03111df81..1e97d744d 100644 --- a/src/main/java/vip/comic18/finder/service/AsyncTaskService.java +++ b/src/main/java/vip/comic18/finder/service/AsyncTaskService.java @@ -107,7 +107,7 @@ public Future getImage(String url) { while(httpResponse == null || bufferedImage == null) { try { ThreadUtil.sleep(RandomUtil.randomInt(2) * 1000L); - httpResponse = HttpUtil.createPost(url).cookie(cookie).setHttpProxy(proxyHost, proxyPort).execute(); + httpResponse = this.createPost(url).execute(); bufferedImage = ImgUtil.read(httpResponse.bodyStream()); } catch(Exception e) { log.error("getImage->下载图片失败:[{}]", e.getLocalizedMessage(), e); @@ -151,13 +151,13 @@ public void saveImage(String path, BufferedImage bufferedImage) { } @Async - public void saveImage(HttpRequest httpRequest, File photoFile) { + public void saveImage(String url, File photoFile) { HttpResponse httpResponse = null; while(httpResponse == null) { try { ThreadUtil.sleep(RandomUtil.randomInt(2) * 1000L); - httpResponse = httpRequest.cookie(cookie).setHttpProxy(proxyHost, proxyPort).execute(); - log.info("getImage->获取图片:[{}]成功", httpRequest.getUrl()); + httpResponse = this.createPost(url).execute(); + log.info("getImage->获取图片:[{}]成功", url); } catch(Exception e) { log.error("saveImage->下载图片失败:[{}]", e.getLocalizedMessage(), e); } @@ -165,4 +165,15 @@ public void saveImage(HttpRequest httpRequest, File photoFile) { httpResponse.writeBody(photoFile); log.info("saveImage->保存图片:[{}]成功", photoFile.getPath()); } + + public HttpRequest createPost(String url) { + HttpRequest post = HttpUtil.createPost(url); + if((proxyHost != null && proxyPort == 0)) { + post.setHttpProxy(proxyHost, proxyPort); + } + if(cookie != null) { + post.cookie(cookie); + } + return post; + } } diff --git a/src/main/java/vip/comic18/finder/service/ComicService.java b/src/main/java/vip/comic18/finder/service/ComicService.java index 6937cfeac..6393b74a3 100644 --- a/src/main/java/vip/comic18/finder/service/ComicService.java +++ b/src/main/java/vip/comic18/finder/service/ComicService.java @@ -4,7 +4,6 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpResponse; -import cn.hutool.http.HttpUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -30,15 +29,6 @@ public class ComicService { @Value("${comic.download.path}") private String downloadPath; - @Value("${comic.proxy.host}") - private String proxyHost; - - @Value("${comic.proxy.port}") - private int proxyPort; - - @Value("${comic.download.cookie}") - private String cookie; - /** * 下载漫画到本地 @@ -48,6 +38,10 @@ public class ComicService { */ public void downloadComic(ComicEntity comicEntity) throws ExecutionException, InterruptedException { String title = comicEntity.getTitle(); + if(downloadPath == null) { + log.error("downloadComic->下载路径downloadPath错误,无法下载文件,请确保配置文件中下载路径正确"); + return; + } File comicDir = FileUtil.mkdir(downloadPath + File.separatorChar + title); for(ChapterEntity chapter : comicEntity.getChapters()) { File chapterDir = FileUtil.file(comicDir.getPath() + File.separatorChar + chapter.getName()); @@ -67,7 +61,7 @@ public void downloadComic(ComicEntity comicEntity) throws ExecutionException, In image = taskService.reverseImage(image).get(); taskService.saveImage(chapterDir.getPath() + File.separatorChar + photo.getName(), image); } - taskService.saveImage(HttpUtil.createPost(photo.getUrl()), photoFile); + taskService.saveImage(photo.getUrl(), photoFile); } } } @@ -80,7 +74,7 @@ public void downloadComic(ComicEntity comicEntity) throws ExecutionException, In */ public ComicEntity getComicInfo(String comicHomePage) throws ExecutionException, InterruptedException { ComicEntity comicEntity = new ComicEntity(); - HttpResponse httpResponse = HttpUtil.createPost(comicHomePage).cookie(cookie).setHttpProxy(proxyHost, proxyPort).execute(); + HttpResponse httpResponse = taskService.createPost(comicHomePage).execute(); String body = httpResponse.body(); String title = StrUtil.subBetween(body, "
\n", "\n
"); title = StrUtil.replaceChars(title, new char[]{'/', '\\'}, StrUtil.DASHED);