一、创建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

  

最新文章

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

热门文章

  1. YUM方法安装mysql5.7版本
  2. Jmeter场景设置与监听
  3. pyechart基本使用大全
  4. Odoo中的ORM API(模型数据增删改查)
  5. centos 修改默认启动内核,及删除无用内核
  6. 最常用MySql数据库备份恢复
  7. docker配置镜像加速器
  8. pandas用法总结
  9. 自定义微信小程序swiper轮播图面板指示点的样式
  10. django小知识(2)