在 Java 的 Spring 框架中,如果你需要从控制器(Controller)返回多个 PDF 文件,你可以通过以下几种方式实现。下面我将为你详细介绍每种方式的步骤流程,并提供相应的示例代码和 Maven/Gradle 依赖坐标。
假设你已经有一个 Spring Boot 项目,并且已经配置好了 Controller 和需要返回的 PDF 文件。
这种方式是将多个 PDF 文件打包成一个 ZIP 文件,然后将 ZIP 文件返回给客户端。
步骤流程:
示例代码:
@RestController
public class PdfController {
@GetMapping("/multiple-pdfs")
public ResponseEntity<Resource> getMultiplePDFs() throws IOException {
// Create a temporary directory
Path tempDirectory = Files.createTempDirectory("pdfs");
// Copy PDF files to the temporary directory
// Assuming you have pdf1.pdf and pdf2.pdf in resources folder
ClassPathResource pdf1 = new ClassPathResource("pdf1.pdf");
ClassPathResource pdf2 = new ClassPathResource("pdf2.pdf");
Files.copy(pdf1.getInputStream(), tempDirectory.resolve("pdf1.pdf"));
Files.copy(pdf2.getInputStream(), tempDirectory.resolve("pdf2.pdf"));
// Create a ZIP file
Path zipFile = Files.createTempFile("pdfs", ".zip");
try (FileOutputStream fos = new FileOutputStream(zipFile.toFile());
ZipOutputStream zipOut = new ZipOutputStream(fos)) {
Files.walk(tempDirectory)
.filter(path -> !Files.isDirectory(path))
.forEach(path -> {
try (FileInputStream fis = new FileInputStream(path.toFile())) {
ZipEntry zipEntry = new ZipEntry(tempDirectory.relativize(path).toString());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
// Return the ZIP file as ResponseEntity
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDisposition(ContentDisposition.attachment().filename("pdfs.zip").build());
return new ResponseEntity<>(new FileSystemResource(zipFile.toFile()), headers, HttpStatus.OK);
}
}
Maven 依赖:
<!-- Add Spring Web dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这种方式是将多个 PDF 文件分别作为多个部分添加到响应中,以 multipart 形式返回给客户端。
步骤流程:
org.springframework.http.ResponseEntity
和 org.springframework.http.HttpHeaders
。org.springframework.core.io.Resource
。示例代码:
@RestController
public class PdfController {
@GetMapping("/multiple-pdfs")
public ResponseEntity<MultipartBody> getMultiplePDFs() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_MIXED);
// Create a MultipartBody and add PDFs to it
MultipartBody multipartBody = new MultipartBody();
// Assuming you have pdf1.pdf and pdf2.pdf in resources folder
ClassPathResource pdf1 = new ClassPathResource("pdf1.pdf");
ClassPathResource pdf2 = new ClassPathResource("pdf2.pdf");
multipartBody.addPart(new InputStreamResource(pdf1.getInputStream()), "application/pdf", "pdf1.pdf");
multipartBody.addPart(new InputStreamResource(pdf2.getInputStream()), "application/pdf", "pdf2.pdf");
return new ResponseEntity<>(multipartBody, headers, HttpStatus.OK);
}
}
Maven 依赖:
<!-- Add Spring Web dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Add Spring HATEOAS for MultipartBody -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
这两种方式都可以实现在 Java 控制器中返回多个 PDF 文件。你可以根据你的具体需求和项目结构选择其中一种方式进行实现。记得根据项目需要适当调整代码和错误处理。