Forest上传下载功能使用

Forest上传下载的使用

上传下载 Forest 从 1.4.0 *版本开始支持多种形式的文件上传和文件下载功能

pom引入

<dependency>
  <groupId>com.dtflys.forest</groupId>
  <artifactId>forest-spring-boot-starter</artifactId>
  <version>1.5.30</version>
</dependency>

yml基础配置

forest:
  bean-id: forestConfiguration # 在spring上下文中bean的id,默认值为forestConfiguration
  backend: okhttp3             # 后端HTTP框架(默认为 okhttp3)
  max-connections: 500         # 连接池最大连接数(默认为 500)
  max-route-connections: 500   # 每个路由的最大连接数(默认为 500)
  max-request-queue-size: 100  # [自v1.5.22版本起可用] 最大请求等待队列大小
  max-async-thread-size: 300   # [自v1.5.21版本起可用] 最大异步线程数
  max-async-queue-size: 16     # [自v1.5.22版本起可用] 最大异步线程池队列大小
  timeout: 3000                # [已不推荐使用] 请求超时时间,单位为毫秒(默认为 3000)
  connect-timeout: 3000        # 连接超时时间,单位为毫秒(默认为 timeout)
  read-timeout: 3000           # 数据读取超时时间,单位为毫秒(默认为 timeout)
  max-retry-count: 0           # 请求失败后重试次数(默认为 0 次不重试)
  ssl-protocol: TLS            # 单向验证的HTTPS的默认TLS协议(默认为 TLS)
  log-enabled: true            # 打开或关闭日志(默认为 true)
  log-request: true            # 打开/关闭Forest请求日志(默认为 true)
  log-response-status: true    # 打开/关闭Forest响应状态日志(默认为 true)
  log-response-content: true   # 打开/关闭Forest响应内容日志(默认为 false)
  async-mode: platform         # [自v1.5.27版本起可用] 异步模式(默认为 platform)
  variables:
    xxBaseUrl: http://xxx:xx/test
    xxContentType: application/json

文件上传功能实现案例

用@DataFile注解修饰要上传的参数对象,OnProgress 参数为监听上传进度的回调函数

    @Post(url = "/upload")
    Map testUpload(@DataFile("file") String filePath, OnProgress onProgress);

    /*** File类型对象*/
    @Post(url = "/upload")
    Map testUpload(@DataFile("file") File file, OnProgress onProgress);

    /*** byte数组* 使用byte数组和Inputstream对象时一定要定义fileName属性*/
    @Post(url = "/upload")
    Map testUpload(@DataFile(value = "file", fileName = "${1}") byte[] bytes, String filename);

    /*** Inputstream 对象* 使用byte数组和Inputstream对象时一定要定义fileName属性*/
    @Post(url = "/upload")
    Map testUpload(@DataFile(value = "file", fileName = "${1}") InputStream in, String filename);

    /*** Spring Web MVC 中的 MultipartFile 对象*/
    @PostRequest(url = "/upload")
    Map testUpload(@DataFile(value = "file") MultipartFile multipartFile, OnProgress onProgress);

    /*** Spring 的 Resource 对象*/
    @Post(url = "/upload")
    Map testUpload(@DataFile(value = "file") Resource resource);

批量上传案例

    /*** 上传Map包装的文件列表* 其中 ${_key} 代表Map中每一次迭代中的键值* @param byteArrayMap* @return*/
    @PostRequest(url = "/upload")
    ForestRequest<Map> uploadByteArrayMap(@DataFile(value = "file", fileName = "${_key}") Map<String, byte[]> byteArrayMap);

    /*** 上传List包装的文件列表* 其中 ${_index} 代表每次迭代List的循环计数(从零开始计)* @param byteArrayList* @return*/
    @PostRequest(url = "/upload")
    ForestRequest<Map> uploadByteArrayList(@DataFile(value = "file", fileName = "test-img-${_index}.jpg") List<byte[]> byteArrayList);

    /*** 上传数组包装的文件列表* 其中 ${_index} 代表每次迭代List的循环计数(从零开始计)* @param byteArrayArray* @return*/
    @PostRequest(url = "/upload")
    ForestRequest<Map> uploadByteArrayArray(@DataFile(value = "file", fileName = "test-img-${_index}.jpg") byte[][] byteArrayArray);

文件下载功能实现案例

  	/**
     * 方法上加上@DownloadFile注解
     * dir属性表示文件下载到哪个目录* filename属性表示文件下载成功后以什么名字保存,如果不填,这默认从URL中取得文件名* OnProgress参数为监听上传进度的回调函数
     */
    @Get(url = "http://localhost:8080/images/xxx.jpg")
    @DownloadFile(dir = "${0}", filename = "${1}")
    File testDownloadFile(String dir, String filename, OnProgress onProgress);

    /*** 如果您不想将文件下载到硬盘上,而是直接在内存中读取,可以去掉@DownloadFile注解,并且用以下几种方式定义接口:*/
    /*** 返回类型用byte[],可将下载的文件转换成字节数组* @return*/
    @GetRequest(url = "http://localhost:8080/images/test-img.jpg")
    byte[] downloadImageToByteArray();

    /*** 返回类型用InputStream,用流的方式读取文件内容* @return*/
    @GetRequest(url = "http://localhost:8080/images/test-img.jpg")
    InputStream downloadImageToInputStream();
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值