1、建立Controllerjava
一個方法是用傳統IO來下載文件,一個是NIO下載文件web
@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"); } } }
2、建立單元測試app
@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