Skip to content

Commit 2ecda4d

Browse files
author
litongjava
committed
add .
1 parent 9c70435 commit 2ecda4d

File tree

1 file changed

+124
-11
lines changed

1 file changed

+124
-11
lines changed

docs/zh/99_案例/03.md

+124-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# tio-boot 案例-文件上传和下载
22

3-
添加依赖
3+
## 添加依赖
44

55
```
66
<properties>
@@ -100,6 +100,8 @@
100100
</profiles>
101101
```
102102

103+
## 数据表
104+
103105
创建一张数据表,保持上传的数据
104106

105107
```sql
@@ -112,7 +114,8 @@ CREATE TABLE "sys_upload_file" (
112114
);
113115
```
114116

115-
配置文件
117+
## 配置文件
118+
116119
app.properties
117120

118121
```properties
@@ -125,7 +128,7 @@ jdbc.showSql=true
125128
jdbc.validationQuery=select 1
126129
```
127130

128-
启动类
131+
## 启动类
129132

130133
```java
131134
package com.litongjava.tio.boo.demo.file;
@@ -145,7 +148,7 @@ public class TioBootFileApp {
145148

146149
```
147150

148-
配置类
151+
## 配置类
149152

150153
```java
151154
package com.litongjava.tio.boo.demo.file.config;
@@ -234,7 +237,7 @@ public class TableToJsonConfig {
234237
}
235238
```
236239

237-
实体类
240+
## 实体类
238241

239242
```
240243
package com.litongjava.tio.boo.demo.file.model;
@@ -257,7 +260,7 @@ public class FileSaveResult {
257260
}
258261
```
259262

260-
常量类
263+
## 常量类
261264

262265
- UPlOAD_FOLDER_NAME 保持文件上传路径
263266

@@ -270,7 +273,7 @@ public interface AppConstants {
270273
271274
```
272275

273-
service
276+
## service
274277

275278
```java
276279
package com.litongjava.tio.boo.demo.file.services;
@@ -360,7 +363,7 @@ public class FileService {
360363

361364
```
362365

363-
controller
366+
## controller
364367

365368
```java
366369
package com.litongjava.tio.boo.demo.file.controller;
@@ -462,7 +465,117 @@ curl --location --request POST 'http://localhost/file/upload' \
462465
}
463466
```
464467

465-
下载
468+
- 下载文件 http://localhost/file/download/6632a4ba-ae03-4f05-adce-de6acdd5c560
469+
470+
## RequestHanlder 版本
471+
472+
tio-boot 的 controller 因为使用了反射,性能会低一些.如果想要提高性能,推荐使用 tio-boot request handler,使用方式如下
473+
474+
### 添加 Handler
475+
476+
```
477+
package com.litongjava.file.server.requesthandler;
478+
479+
import java.io.File;
480+
import java.io.FileInputStream;
481+
import java.io.IOException;
482+
import java.io.InputStream;
483+
484+
import com.litongjava.file.server.model.FileSaveResult;
485+
import com.litongjava.file.server.services.FileService;
486+
import com.litongjava.jfinal.aop.annotation.AAutowired;
487+
import com.litongjava.tio.http.common.HttpRequest;
488+
import com.litongjava.tio.http.common.HttpResponse;
489+
import com.litongjava.tio.http.common.UploadFile;
490+
import com.litongjava.tio.http.server.util.Resps;
491+
import com.litongjava.tio.utils.resp.RespVo;
492+
493+
import lombok.Cleanup;
494+
import lombok.extern.slf4j.Slf4j;
495+
496+
@Slf4j
497+
public class FileRequestHanlder {
498+
@AAutowired
499+
private FileService fileService;
500+
501+
public HttpResponse upload(HttpRequest request) {
502+
UploadFile file = request.getUploadFile("file");
503+
RespVo respVo;
504+
if (file != null) {
505+
String name = file.getName();
506+
byte[] fileData = file.getData();
507+
log.info("upload file size:{}", fileData.length);
508+
// save file
509+
FileSaveResult fileSaveResult = fileService.save(name, fileData);
510+
fileSaveResult.setFile(null);
511+
512+
log.info("save file finished");
513+
respVo = RespVo.ok(fileSaveResult);
514+
515+
} else {
516+
respVo = RespVo.fail("fail").code(-1);
517+
}
518+
519+
return Resps.json(request, respVo);
520+
}
521+
522+
public HttpResponse download(HttpRequest request) {
523+
String id = request.getParam("id");
524+
log.info("id:{}", id);
525+
File file = fileService.getUploadFile(id);
526+
int available;
527+
try {
528+
@Cleanup
529+
InputStream inputStream = new FileInputStream(file);
530+
available = inputStream.available();
531+
byte[] fileBytes = new byte[available];
532+
inputStream.read(fileBytes, 0, available);
533+
// String contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8";
534+
String contentType = "";
535+
HttpResponse response = Resps.bytesWithContentType(request, fileBytes, contentType);
536+
return response;
537+
} catch (IOException e) {
538+
e.printStackTrace();
539+
return Resps.json(request, RespVo.fail("Error generating captcha"));
540+
}
541+
}
542+
}
543+
544+
```
545+
546+
### 使用配置类配置 RequestHanlder
547+
548+
```
549+
package com.litongjava.file.server.config;
550+
551+
import com.litongjava.file.server.requesthandler.FileRequestHanlder;
552+
import com.litongjava.jfinal.aop.Aop;
553+
import com.litongjava.jfinal.aop.annotation.AInitialization;
554+
import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration;
555+
import com.litongjava.tio.boot.server.TioBootServer;
556+
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
557+
558+
@BeforeStartConfiguration
559+
public class HttpServerRequestHanlderConfig {
560+
561+
@AInitialization
562+
public void httpRoutes() {
563+
564+
// 创建simpleHttpRoutes
565+
SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes();
566+
// 创建controller
567+
FileRequestHanlder asrSubmitRequestHanlder = Aop.get(FileRequestHanlder.class);
568+
569+
// 添加action
570+
simpleHttpRoutes.add("/upload", asrSubmitRequestHanlder::upload);
571+
simpleHttpRoutes.add("/download", asrSubmitRequestHanlder::download);
572+
573+
// 将simpleHttpRoutes添加到TioBootServer
574+
TioBootServer.me().setHttpRoutes(simpleHttpRoutes);
575+
}
576+
577+
}
578+
```
466579

467-
- http://localhost/file/download/6632a4ba-ae03-4f05-adce-de6acdd5c560
468-
- 下载文件
580+
- 开源地址
581+
https://github.com/litongjava/tio-boot-file-server

0 commit comments

Comments
 (0)