download file

This commit is contained in:
dqn 2025-10-06 01:50:15 +08:00
parent c5cb9fca5b
commit 3948708da3
2 changed files with 30 additions and 1 deletions

View File

@ -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<Resp<Page<SeedListItem>>> getBookSeedPage(
@RequestParam(required = false, defaultValue = "0", name = "inclbookmarked") String inclbookmarked,

View File

@ -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();
}
}