|
1 |
| -# 返回图片 |
| 1 | +# 文件下载 |
2 | 2 |
|
3 |
| -#### 返回验证码 |
4 |
| -用到了hutool依赖,请自行添加 |
| 3 | +### 下载文档 |
| 4 | + |
| 5 | +用于说明如何在 Java 后端应用程序中实现 PDF 和 DOCX 文件的下载功能 |
| 6 | + |
| 7 | +#### 环境依赖 |
| 8 | + |
| 9 | +在开始之前,请确保您的项目已添加以下依赖: |
| 10 | + |
| 11 | +- hutool-core: 提供工具类,例如资源访问工具。 |
| 12 | + |
| 13 | +#### 下载 PDF 文件 |
| 14 | + |
| 15 | +实现步骤 |
| 16 | + |
| 17 | +- 定义请求路径:使用@RequestPath("/download/pdf/{id}")注解标注方法,定义了处理 PDF 文件下载的请求路径。 |
| 18 | +- 读取文件:使用 ResourceUtil.getStream("pdf/" + id + ".pdf")根据文件 ID 动态获取 PDF 文件的输入流。 |
| 19 | +- 发送响应:将 PDF 文件内容以字节形式发送给客户端,并设置 Content-Type 为 application/pdf; charset=utf-8。 |
| 20 | + |
| 21 | +示例代码 |
| 22 | + |
| 23 | +```java |
| 24 | +@RequestPath("/download/pdf/{id}") |
| 25 | +public HttpResponse downloadPdf(String id, HttpRequest request) { |
| 26 | + log.info("id:{}", id); |
| 27 | + InputStream inputStream = ResourceUtil.getStream("pdf/" + id + ".pdf"); |
| 28 | + int available; |
| 29 | + try { |
| 30 | + available = inputStream.available(); |
| 31 | + byte[] fileBytes = new byte[available]; |
| 32 | + inputStream.read(fileBytes, 0, available); |
| 33 | + HttpResponse response = Resps.bytesWithContentType(request, fileBytes, "application/pdf; charset=utf-8"); |
| 34 | + return response; |
| 35 | + } catch (IOException e) { |
| 36 | + e.printStackTrace(); |
| 37 | + return Resps.json(request, RespVo.fail("Error generating captcha")); |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +#### 下载 DOCX 文件 |
| 43 | + |
| 44 | +实现步骤 |
| 45 | + |
| 46 | +- 定义请求路径:使用@RequestPath("/download/docx/{id}")注解标注方法,定义了处理 DOCX 文件下载的请求路径。 |
| 47 | +- 读取文件:使用 ResourceUtil.getStream(id+".docx")根据文件 ID 动态获取 DOCX 文件的输入流。 |
| 48 | +- 发送响应:将 DOCX 文件内容以字节形式发送给客户端,并设置 Content-Type 为 application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8。 |
| 49 | + |
| 50 | +示例代码 |
| 51 | + |
| 52 | +```java |
| 53 | +@RequestPath("/download/docx/{id}") |
| 54 | +public HttpResponse download(String id, HttpRequest request) { |
| 55 | + InputStream inputStream = ResourceUtil.getStream(id+".docx"); |
| 56 | + int available; |
| 57 | + try { |
| 58 | + available = inputStream.available(); |
| 59 | + byte[] fileBytes = new byte[available]; |
| 60 | + inputStream.read(fileBytes, 0, available); |
| 61 | + HttpResponse response = Resps.bytesWithContentType(request, fileBytes, |
| 62 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8"); |
| 63 | + return response; |
| 64 | + } catch (IOException e) { |
| 65 | + e.printStackTrace(); |
| 66 | + return Resps.json(request, RespVo.fail("Error generating captcha")); |
| 67 | + } |
| 68 | +} |
5 | 69 | ```
|
6 |
| -package com.litongjava.tio.boot.hello.AController; |
| 70 | + |
| 71 | +结论 |
| 72 | +通过上述实现,您可以方便地在您的 Java Web 应用中添加文件下载功能,支持多种文件格式,如 PDF 和 DOCX。请注意,文件路径和命名方式可能需要根据实际项目情况进行调整。 |
| 73 | + |
| 74 | +#### 下载完整代码示例 |
| 75 | + |
| 76 | +```java |
| 77 | +import java.io.IOException; |
| 78 | +import java.io.InputStream; |
| 79 | + |
| 80 | +import com.litongjava.tio.http.common.HttpRequest; |
| 81 | +import com.litongjava.tio.http.common.HttpResponse; |
| 82 | +import com.litongjava.tio.http.server.annotation.RequestPath; |
| 83 | +import com.litongjava.tio.http.server.util.Resps; |
| 84 | +import com.litongjava.tio.utils.resp.RespVo; |
| 85 | + |
| 86 | +import cn.hutool.core.io.resource.ResourceUtil; |
| 87 | +import lombok.extern.slf4j.Slf4j; |
| 88 | + |
| 89 | +@Slf4j |
| 90 | +public class DownloadController { |
| 91 | + @RequestPath("/download/pdf/{id}") |
| 92 | + public HttpResponse downloadPdf(String id, HttpRequest request) { |
| 93 | + log.info("id:{}", id); |
| 94 | + InputStream inputSteam = ResourceUtil.getStream("pdf/" + id + ".pdf"); |
| 95 | + int available; |
| 96 | + try { |
| 97 | + available = inputSteam.available(); |
| 98 | + byte[] captchaBytes = new byte[available]; |
| 99 | + inputSteam.read(captchaBytes, 0, available); |
| 100 | + |
| 101 | + HttpResponse response = Resps.bytesWithContentType(request, captchaBytes, "application/pdf; charset=utf-8"); |
| 102 | + return response; |
| 103 | + } catch (IOException e) { |
| 104 | + e.printStackTrace(); |
| 105 | + return Resps.json(request, RespVo.fail("Error generating captcha")); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @RequestPath("/download/docx/{id}") |
| 110 | + public HttpResponse download(String id, HttpRequest request) { |
| 111 | + log.info("id:{}", id); |
| 112 | + InputStream inputSteam = ResourceUtil.getStream(id + ".docx"); |
| 113 | + int available; |
| 114 | + try { |
| 115 | + available = inputSteam.available(); |
| 116 | + byte[] captchaBytes = new byte[available]; |
| 117 | + inputSteam.read(captchaBytes, 0, available); |
| 118 | + // Content-Type: |
| 119 | + HttpResponse response = Resps.bytesWithContentType(request, captchaBytes, |
| 120 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8"); |
| 121 | + return response; |
| 122 | + } catch (IOException e) { |
| 123 | + e.printStackTrace(); |
| 124 | + return Resps.json(request, RespVo.fail("Error generating captcha")); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +### 下载图片 |
| 132 | + |
| 133 | +#### 返回验证码 |
| 134 | + |
| 135 | +用到了 hutool 依赖,请自行添加 |
| 136 | + |
| 137 | +```java |
7 | 138 |
|
8 | 139 | import java.io.ByteArrayOutputStream;
|
9 | 140 | import java.io.IOException;
|
@@ -82,17 +213,15 @@ public class CaptchaController {
|
82 | 213 |
|
83 | 214 | 添加依赖
|
84 | 215 |
|
85 |
| -``` |
| 216 | +```xml |
86 | 217 | <dependency>
|
87 | 218 | <groupId>com.google.zxing</groupId>
|
88 | 219 | <artifactId>core</artifactId>
|
89 | 220 | <version>3.4.1</version> <!-- 使用最新版本 -->
|
90 | 221 | </dependency>
|
91 | 222 | ```
|
92 | 223 |
|
93 |
| -``` |
94 |
| -package com.litongjava.tio.boot.hello.AController; |
95 |
| -
|
| 224 | +```java |
96 | 225 | import java.io.ByteArrayOutputStream;
|
97 | 226 | import java.io.IOException;
|
98 | 227 |
|
@@ -169,4 +298,4 @@ public class QrController {
|
169 | 298 | 6. **请求示例**:
|
170 | 299 | - 访问 `http://localhost/qr?content=tio-boot` 将会触发这个控制器方法,`content=tio-boot` 表示生成包含文本 “tio-boot” 的二维码。
|
171 | 300 |
|
172 |
| -这个控制器的作用是接收一个文本内容,并将其转换为二维码图片,然后将这个图片作为 HTTP 响应返回给客户端。如果在生成二维码的过程中出现问题,它会返回一个包含错误信息的 JSON 响应。 |
| 301 | +这个控制器的作用是接收一个文本内容,并将其转换为二维码图片,然后将这个图片作为 HTTP 响应返回给客户端。如果在生成二维码的过程中出现问题,它会返回一个包含错误信息的 JSON 响应。 |
0 commit comments