一、创建Controller

一个方法是用传统IO来下载文件,一个是NIO下载文件

@Controller
public class FileController { private Logger log = LoggerFactory.getLogger(FileController.class); @RequestMapping(value="/download/oldio}", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response,String fileName) throws IOException{
String folder = "C://Users/xxx/Downloads/0714";
File file = new File(folder, fileName);
if(file.exists()){
response.setContentType("MimeType");
response.addHeader("Content-Disposition","attachment;filename=" +fileName);
response.setContentLength((int)file.length()); //Java IO
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
//Copy bytes from source to destination(outputstream in this example), closes both streams.
FileCopyUtils.copy(inputStream, response.getOutputStream()); log.info("download success! ---" + fileName); }else {
throw new Error("file not exist");
}
} @RequestMapping(value="/download/nio}", method = RequestMethod.GET)
public void downloadfornio(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException{
String folder = "C://Users/xxx/Downloads/0714";
File file = new File(folder, fileName);
if(file.exists()){
response.setContentType("MimeType");
response.addHeader("Content-Disposition","attachment;filename=" +fileName);
response.setContentLength((int)file.length()); //128 * 1024 = 128K
int bufferSize = 131072 * 6;
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
// 6 * 128K = 768K = 786432
ByteBuffer buffer = ByteBuffer.allocateDirect(786432);
byte[] byteArr = new byte[bufferSize];
int nRead, nGet; try {
while ((nRead = fileChannel.read(buffer)) != -1){
if(nRead == 0){
continue;
}
buffer.position(0);
buffer.limit(nRead);
while (buffer.hasRemaining()){
nGet = Math.min(buffer.remaining(), bufferSize);
// read bytes from disk
buffer.get(byteArr,0,nGet);
//write bytes to output
response.getOutputStream().write(byteArr);
}
buffer.clear(); }
log.info("download success! ---" + fileName);
}catch (IOException e){
e.printStackTrace();
}finally {
buffer.clear();
fileChannel.close();
fileInputStream.close();
} }else {
throw new Error("file not exist");
}
}
}

  

二、创建单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class FileControllerTest { private MockMvc mockMvc; @Autowired
private WebApplicationContext wac; private FileController fc; @Before
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
fc = this.wac.getBean(FileController.class);
} @Test
public void compareTime() throws Exception { String fileName = "11.tar.gz";
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse(); long c = System.currentTimeMillis();
fc.downloadfornio(request, response, fileName);
long d = System.currentTimeMillis();
System.out.println("nio download takes :" + (d - c) ); long a = System.currentTimeMillis();
fc.download(request, response,fileName);
long b = System.currentTimeMillis();
System.out.println("io download takes :" + (b - a) ); } }

  输出结果

nio download takes :144
io download takes :164

  

Spring Boot Controller单元测试的更多相关文章

  1. Spring Boot学习——单元测试

    本随笔记录使用Spring Boot进行单元测试,主要是Service和API(Controller)进行单元测试. 一.Service单元测试 选择要测试的service类的方法,使用idea自动创 ...

  2. Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)

    前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...

  3. Spring Boot 的单元测试

    Spring Boot 的单元测试 引入依赖 testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-tes ...

  4. Spring Boot使用单元测试

    一.Service层单元测试: 代码如下: package com.dudu.service;import com.dudu.domain.LearnResource;import org.junit ...

  5. 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试

    前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...

  6. Spring Boot Mock单元测试学习总结

    单元测试的方法有很多种,比如使用Postman.SoapUI等工具测试,当然,这里的测试,主要使用的是基于RESTful风格的SpringMVC的测试,我们可以测试完整的Spring MVC流程,即从 ...

  7. Spring Boot 的单元测试和集成测试

    学习如何使用本教程中提供的工具,并在 Spring Boot 环境中编写单元测试和集成测试. 1. 概览 本文中,我们将了解如何编写单元测试并将其集成在 Spring Boot 环境中.你可在网上找到 ...

  8. Spring Boot 2 单元测试

    开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 IDEA新建一个Spring Boot项目后,pom.xml默认包含了Web应用和单元测试两个依赖包.如下 ...

  9. Spring MVC Controller 单元测试

    简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...

随机推荐

  1. Cheat Engine 指针

    打开游戏 扫描时间的流程就不多说了 扫描结果 寻找基地址 右击扫描到的地址,选择什么改写了这个地址 会弹出如下窗口 不用管这个窗口,去改变一下游戏时间,出现如下图 随便打开一个,找到了数据块地址和偏移 ...

  2. jsp 获取后端配置文件.properties的某个配置内容

    如后端有个叫做config.properties的配置文件: sys.img=st_sp jsp中引用的方式是: <%@ page language="java" impor ...

  3. Jackson 将数组json转List泛型

    闲话不多说,直接上干活,见代码就懂. package com.zzcloud.job.common; import java.util.ArrayList; import java.util.List ...

  4. go build -tags 的使用

    go build 使用tag来实现编译不同的文件 go-tooling-workshop 中关于go build的讲解可以了解到go bulid的一些用法,这篇文章最后要求实现一个根据go bulid ...

  5. Centos 实战-MySQL定时全量备份(1)

    /usr/bin/mysqldump -uroot -p123456 --lock-all-tables --flush-logs test > /home/backup.sql 如上一段代码所 ...

  6. python笔记43-加解密AES/CBC/pkcs7padding

    前言 有些公司对接口的安全要求比较高,传参数的时候,不会明文的传输,先对接口加密,返回的数据也加密返回. 目前比较常见的加密方式是AES/CBC/pkcs7padding. AES五种加密模式 在AE ...

  7. httprunner学习22-正则表达式提取(extract)与校验(validate)

    前言 有些接口返回的并不是json格式的内容,返回的是html或者xml格式的内容,这种就不能用前面的 content.的方法去提取了. httprunner 支持正则表达式提取(extract)参数 ...

  8. 不能走路(walk)

    [题目背景] 小G 同学总是在树上走路.小S 看不下去了,决定阻止小G 同学. [题目描述] 有一棵 n 个点的树,树上有 m 条路径,每条路径为 x[i]到y[i] 的树上最短路径(不经过相同的边) ...

  9. 23、matplotlib数据可视化、绘图库模块

    matplotlib官方文档:https://matplotlib.org/contents.html?v=20190307135750 matplotlib是一个绘图库,它可以创建常用的统计图,包括 ...

  10. woocommerce调用产品相册gallery图片如何操作?wordpress技巧

    wordpress官网有很多woocommerce模板,但有些客户要求定制模板,这时可能会碰到产品相册图片调用的问题,如果根据自带的Storefront主题来改很麻烦,那我们就自己定义吧!下来就随yt ...