JavaWeb方式
文件下载需求:
1. 页面显示超链接
2. 点击超链接后弹出下载提示框
3. 完成图片文件下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @RequestMapping("/download") public void downLoad(HttpServletRequest request, HttpServletResponse response) throws IOException {
String filename = request.getParameter("filename"); System.out.println(filename);
String realPath = new ClassPathResource("static/"+filename).getFile().getAbsolutePath(); System.out.println(realPath);
FileInputStream fis = new FileInputStream(realPath);
response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes(), "iso8859-1")); ServletOutputStream os = response.getOutputStream(); byte[] buff = new byte[1024 * 8]; int len = 0; while((len = fis.read(buff)) != -1){ os.write(buff,0,len); }
os.close(); fis.close(); }
|
SpringMVC实现下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@RequestMapping("/download") public ResponseEntity<byte[]> download(HttpServletRequest request) throws Exception{ ServletContext context = request.getServletContext(); String realPath = context.getRealPath("/scripts/jquery-1.9.1.min.js"); FileInputStream is = new FileInputStream(realPath); byte[] tmp = new byte[is.available()]; is.read(tmp); is.close(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Disposition", "attachment;filename="+"jquery-1.9.1.min.js"); return new ResponseEntity<byte[]>(tmp, httpHeaders, HttpStatus.OK); }
|
导出execl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| List<Map<String,Object>> ztbList = mapper.doYysExcel(param); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet=workbook.createSheet("预验收信息"); HSSFRow row1=sheet.createRow(0); row1.setHeightInPoints(22); HSSFCell cellTiltle = row1.createCell(0); HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue("预验收信息数据表"); sheet.addMergedRegion(new CellRangeAddress(0,0,0,4)); HSSFRow row2=sheet.createRow(1); row2.createCell(0).setCellValue("序号"); row2.createCell(1).setCellValue("项目名称"); row2.createCell(2).setCellValue("单位名称"); row2.createCell(3).setCellValue("已拨付资金(万元)"); row2.createCell(4).setCellValue("架构符合度评估报告"); row2.setHeightInPoints(18); sheet.setColumnWidth(0,10 * 256); sheet.setColumnWidth(1,20 * 256); sheet.setColumnWidth(2,20 * 256); sheet.setColumnWidth(3,20 * 256); sheet.setColumnWidth(4,20 * 256); int xuHao = 1; int rowNum = 2; for (int i = 0; i < ztbList.size(); i++) { HSSFRow row = sheet.createRow(rowNum); row.createCell(0).setCellValue(xuHao); row.createCell(1).setCellValue((ztbList.get(i).get("XMMC") == null ? "暂无数据" : ztbList.get(i).get("XMMC").toString())); row.createCell(2).setCellValue((ztbList.get(i).get("SQDWDM") == null ? "暂无数据" : ztbList.get(i).get("SQDWDM").toString())); row.createCell(3).setCellValue((ztbList.get(i).get("YBF") == null ? "暂无数据" : ztbList.get(i).get("YBF").toString())); row.setHeightInPoints(15); rowNum ++; xuHao ++; } OutputStream os =null; try { String fileName = "预验收信息数据表"; response.reset(); os = response.getOutputStream(); response.setContentType("application/x-download"); response.setCharacterEncoding("gbk"); response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"), "iso8859-1")+".xls"); workbook.write(os); } catch (Exception e) { e.printStackTrace(); } finally { try { if(os != null){ os.flush(); os.close(); } } catch (IOException e) { e.printStackTrace(); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { HSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short)11); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("Courier New"); HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font); style.setWrapText(false); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public HSSFCellStyle getStyle(HSSFWorkbook workbook) { HSSFFont font = workbook.createFont(); font.setFontName("Courier New"); HSSFCellStyle style = workbook.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setRightBorderColor(HSSFColor.BLACK.index); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setTopBorderColor(HSSFColor.BLACK.index); style.setFont(font); style.setWrapText(false); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; }
|