大家好,有这么个需求。前台页面有多个多选框,用户选定那几个,就下载这几个。注:不是打包下载,是一个一个下载。下面的代码是单独下载一个文件的时候,多个文件的话问一下大家怎么实现,前台要怎么传参数,而且一个request对应一个response,前台只执行一次,怎么让后台多次执行?
String fileName = request.getParameter("fileName"); response.setContentType("application/OCTET-STREAM;charset=UTF-8"); response.setHeader("Content-Dispositon", "attachment;filename="+fileName); String path = "E:\file"; File file = new File(path,fileName); FileInputStream fis = null; BufferedOutputStream bos = null; try { fis = new FileInputStream(file); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[1024]; int len; while((len=fis.read(buffer))!=-1){ bos.write(buffer,0,len); bos.flush(); } } catch (Exception e) { e.printStackTrace(); }finally{ fis.close(); bos.close(); }
解决方案