- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑到我在数据库中有大量的 csv 字符串,我需要根据客户的请求将它们传递给客户。
一种简单的方法:
@RequestMapping(value = "/{user}/ejournal", method = RequestMethod.GET, produces = "text/csv")
public ResponseEntity<ByteArrayResource> getEJournal(@PathVariable Long userId) {
final byte[] documentBody = EJournal
.findByUser(userId)
.stream()
.collect(Collectors.joining("\n"))
.getBytes();
return new ResponseEntity(
new ByteArrayResource(documentBody),
HttpStatus.OK);
}
从客户端来看,它很简单(kotlin 代码示例):
val inputStream = eJournalService.getJournal(userId)
inputStream.reader().forEachLine { line -> ... }
但是我被迫将整个数据加载到内存中,然后再将其传递给流,显然这效率不高。
所以我需要以某种方式对其进行缓冲,以使用自定义读取器分页读取数据并将缓冲区发送到客户端。我想实现我自己的 InputStream
但 InputStream#read()
返回 int
而不是 String
并且它是一个有点复杂。
有什么最佳实践可以做到这一点吗?我尝试搜索,但只有使用内存中已存在的流传递图片的示例,而不是在批量发送后使用顺序数据库查询的缓冲分页。
提前致谢。
最佳答案
The following are the supported return types:
[...]
- void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose)
[...]
- A StreamingResponseBody can be returned to write to the response OutputStream asynchronously; also supported as the body within a ResponseEntity.
所以你可以这样做
public StreamingResponseBody getEJournal(@PathVariable Long userId) {
return outputStream -> {
// write your lines to the outputStream here
};
}
您还可以通过将 HttpServletResponse
作为方法的参数来同步写入流,然后写入其输出流。
关于java - 逐渐填充InputStream并用SpringMVC返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36371303/
基于SpringBoot版本如下: org.springframework.boot spring-boot-starter-parent 2.5.2
@RestController public class TestController { @GetMapping("/download") public ResponseEntit
概述 记得之前跟前端同事联调接口的时候,后端SpringMVC需要接收数组类型的参数,然后跟前端说需要传数组类型过来。后来前端童鞋传了数组,但是后端接收不成功,联调失败。那时候由于时间关系没有仔细研究
web.xml 片段: contextConfigLocation /WEB-INF/applicationContext-security.xml a
目录 相关准备 功能清单 具体功能:访问首页 ①配置view-controller ②创建页面
Spring mvc是一个非常轻量的mvc框架,注解可以大大减少配置,让请求的拦截变得比较简单。这次记录下@RequestBody 注解接收参数尤其是数组参数的用法。 关于容器的配置不再多说,这里
目录 SpringMVC默认处理的几种异常 @ResponseStatus 异常处理的顺序 自定义异常类(SpringMVC的异常处理)
目录 SpringMVC 接收前端传递的参数四种方式 @RequestParam 获取注解 @PathVariable获取注解 Sp
目录 @PathVariable的用法解析 问题描述 解析过程 动态参数使用@PathVariable
目录 SpringMVC @NotNull校验不生效 加了两个依赖问题解决 @NotNull注解失效原因之一 Lo
springmvc―handlermapping三种映射 handlermapping负责映射中央处理器转发给controller的映射策略,简单说就是控制中央处理器的请求触发哪一个control
目录 使用ModelAndView向request域对象共享数据 使用Model向request域对象共享数据 使用map向request域对象共享数据
整合SSM 环境要求 环境: IDEA MySQL5.7.19 Tomcat9 Maven3.6 要求: 需要熟练掌握MySQL数据库,Spring,Ja
目录 1、SpringMVC简介 2、工作流程与介绍 3、代码截图 以下组件通常使用框架提供实现: 1、Di
简介 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。 过滤器
背景 举个例子,出现中文乱码的例子:提交表单的时候。 表单 ?
请求进入DispatcherServlet的doDispatch后,获取HandlerMethod。然后根据HandlerMethod来确认HandlerApapter,确认后执行HandlerAd
实现需求: 1.用户未登录,跳转到登录页,登录完成后会跳到初始访问页。 2.用户自定义处理(如需要激活),跳转到激活页面,激活完成后会跳到初始访问页。 使用到的框架 springmvc 的拦
为了实现用户登录拦截你是否写过如下代码呢? 1. 基于Filter ?
springmvc dao层和service层的区别 首先解释面上意思,service是业务层,dao是数据访问层 这个问题我曾经也有过,记得以前刚学编程的时候,都是在service里直接调用d
我是一名优秀的程序员,十分优秀!