1
1
# tio-boot 案例-文件上传和下载
2
2
3
- 添加依赖
3
+ ## 添加依赖
4
4
5
5
```
6
6
<properties>
100
100
</profiles>
101
101
```
102
102
103
+ ## 数据表
104
+
103
105
创建一张数据表,保持上传的数据
104
106
105
107
``` sql
@@ -112,7 +114,8 @@ CREATE TABLE "sys_upload_file" (
112
114
);
113
115
```
114
116
115
- 配置文件
117
+ ## 配置文件
118
+
116
119
app.properties
117
120
118
121
``` properties
@@ -125,7 +128,7 @@ jdbc.showSql=true
125
128
jdbc.validationQuery =select 1
126
129
```
127
130
128
- 启动类
131
+ ## 启动类
129
132
130
133
``` java
131
134
package com.litongjava.tio.boo.demo.file ;
@@ -145,7 +148,7 @@ public class TioBootFileApp {
145
148
146
149
```
147
150
148
- 配置类
151
+ ## 配置类
149
152
150
153
``` java
151
154
package com.litongjava.tio.boo.demo.file.config ;
@@ -234,7 +237,7 @@ public class TableToJsonConfig {
234
237
}
235
238
```
236
239
237
- 实体类
240
+ ## 实体类
238
241
239
242
```
240
243
package com.litongjava.tio.boo.demo.file.model;
@@ -257,7 +260,7 @@ public class FileSaveResult {
257
260
}
258
261
```
259
262
260
- 常量类
263
+ ## 常量类
261
264
262
265
- UPlOAD_FOLDER_NAME 保持文件上传路径
263
266
@@ -270,7 +273,7 @@ public interface AppConstants {
270
273
271
274
```
272
275
273
- service
276
+ ## service
274
277
275
278
``` java
276
279
package com.litongjava.tio.boo.demo.file.services ;
@@ -360,7 +363,7 @@ public class FileService {
360
363
361
364
```
362
365
363
- controller
366
+ ## controller
364
367
365
368
``` java
366
369
package com.litongjava.tio.boo.demo.file.controller ;
@@ -462,7 +465,117 @@ curl --location --request POST 'http://localhost/file/upload' \
462
465
}
463
466
```
464
467
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
+ ```
466
579
467
- - http://localhost/file/download/6632a4ba-ae03-4f05-adce-de6acdd5c560
468
- - 下载文件
580
+ - 开源地址
581
+ https://github.com/litongjava/tio-boot-file-server
0 commit comments