@RequestMapping("/downloadxls.action") public void downloadxls(HttpServletRequest request, HttpServletResponse response) { //获取请求参数 Map<String, Object> params = ParamsUtil.getParams(request); String contextPath = request.getSession().getServletContext().getRealPath(File.separator + "report"); String excelName = xxxxx; String excelFullName = contextPath + File.separator + excelName + ".xls"; InputStream inStream = null, fileInStream = null; ServletOutputStream outStream = null; int byteRead; try { fileInStream = new FileInputStream(excelFullName); inStream = new BufferedInputStream(fileInStream); response.reset(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-disposition", "attachment; filename=" + excelName + ".xls"); outStream = response.getOutputStream(); byte[] buffer = new byte[1024]; while ((byteRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteRead); } response.flushBuffer(); outStream.close(); inStream.close(); fileInStream.close(); } catch (Exception e) { LOGGER.error( e); }finally{ try { if(outStream!=null){ outStream.close(); } } catch (IOException e2) { LOGGER.error(e2); } try { if(inStream!=null){ inStream.close(); } } catch (IOException e2) { LOGGER.error(e2); } try { if(fileInStream!=null){ fileInStream.close(); } } catch (IOException e2) { LOGGER.error(e2); } } }
来源:https://www.cnblogs.com/ruanian/p/11060227.html