十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
今天就跟大家聊聊有关使用springboot如何实现下载单或多的zip文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
单文件下载
//下载单个文件 public void downloadFile(HttpServletResponse response){ String path = "D:\test\ce\1.txt" File file = new File(path); if(file.exists()){ String fileName = file.getName(); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); download(response,file); } } public void download(HttpServletResponse response,File file){ FileInputStream fis = null; BufferedInputStream bis = null; OutputStream os = null; try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); byte[] buffer = new byte[bis.available()]; int i = bis.read(buffer); while(i != -1){ os.write(buffer, 0, i); i = bis.read(buffer); } } catch (Exception e) { e.printStackTrace(); } try { bis.close(); fis.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } }