Spring Boot Controller单元测试
2024-09-02 19:23:09
一、创建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
最新文章
- 【从html到算法框架】科技白学习计划书
- Android Support Library
- 在Cortex-M系列上如何准确地做us级延时?
- MS SQL Server存储过程
- 断言(ASSERT)的用法
- Marven笔记贴
- Android 建立文件夹、生成文件并写入文本文件内容
- Mongo中更新总结
- C# 学习之旅(2)--- 意外的收获
- html-----008
- 使用Partitioner实现输出到多个文件
- WebSphere之wasprofile.sh使用
- CFileDialog类与16进制格式的dat文件
- linux安装bind with DLZ <;NIOT>;
- Shell自学二(参数传递和数组)
- jackson把json转换成LIst
- 性能学习随笔(1)--负载均衡之f5负载均衡
- IIS7.5配置过程
- 如何简单快速的修改Bootstrap
- bootstrap面试题