From 3948708da352beac3a91eac28744d423f4e39cc9 Mon Sep 17 00:00:00 2001 From: dqn Date: Mon, 6 Oct 2025 01:50:15 +0800 Subject: [PATCH] download file --- .../com/dengqn/app/lingyinapi/apis/Seed.java | 16 ++++++++++++++++ .../com/dengqn/app/lingyinapi/http/HttpTool.java | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/dengqn/app/lingyinapi/apis/Seed.java b/src/main/java/com/dengqn/app/lingyinapi/apis/Seed.java index d11d7fd..48cf053 100644 --- a/src/main/java/com/dengqn/app/lingyinapi/apis/Seed.java +++ b/src/main/java/com/dengqn/app/lingyinapi/apis/Seed.java @@ -5,11 +5,17 @@ import com.dengqn.app.lingyinapi.beans.api.Page; import com.dengqn.app.lingyinapi.beans.api.Resp; import com.dengqn.app.lingyinapi.html.HtmlTool; import com.dengqn.app.lingyinapi.http.HttpTool; +import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import okhttp3.OkHttpClient; +import okhttp3.Response; +import okhttp3.ResponseBody; +import org.apache.tomcat.util.http.fileupload.IOUtils; import org.jetbrains.annotations.NotNull; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; +import org.mockito.internal.util.io.IOUtil; +import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,6 +23,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; +import java.io.InputStream; import java.util.List; /** @@ -36,6 +43,15 @@ public class Seed { this.okHttpClient = okHttpClient; } + @GetMapping("/downloadByUrl") + public void downloadSeedFile(@RequestParam("url") String url, HttpServletResponse response) throws IOException { + Response downloadedResp = HttpTool.downloadSeedFile("https://pt.soulvoice.club/" + url, okHttpClient); + response.setStatus(HttpServletResponse.SC_OK); + response.setContentType(downloadedResp.headers().get("Content-Type")); + response.setHeader("Content-Disposition", downloadedResp.headers().get("Content-Disposition")); + IOUtils.copy(downloadedResp.body().byteStream(), response.getOutputStream()); + } + @GetMapping("/book/list") public ResponseEntity>> getBookSeedPage( @RequestParam(required = false, defaultValue = "0", name = "inclbookmarked") String inclbookmarked, diff --git a/src/main/java/com/dengqn/app/lingyinapi/http/HttpTool.java b/src/main/java/com/dengqn/app/lingyinapi/http/HttpTool.java index 2757c49..759d1b5 100644 --- a/src/main/java/com/dengqn/app/lingyinapi/http/HttpTool.java +++ b/src/main/java/com/dengqn/app/lingyinapi/http/HttpTool.java @@ -8,6 +8,7 @@ import okhttp3.ResponseBody; import okhttp3.internal.http.HttpMethod; import java.io.IOException; +import java.io.InputStream; /** * @@ -16,11 +17,14 @@ import java.io.IOException; */ @Slf4j public class HttpTool { + + private final static String UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36"; + public static String getHTML(String url, OkHttpClient okHttpClient) { try { Response response = okHttpClient.newCall(new Request.Builder() .url(url) - .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36") + .header("User-Agent", UA) .method("GET", null) .build()) .execute(); @@ -34,4 +38,13 @@ public class HttpTool { return ""; } } + + + public static Response downloadSeedFile(String seedUrl, OkHttpClient okHttpClient) throws IOException { + return okHttpClient.newCall(new Request.Builder() + .url(seedUrl) + .header("User-Agent", UA) + .build()) + .execute(); + } }