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 = this.getClass().getResource("/static/"+filename).getPath();不支持中文

String realPath = new ClassPathResource("static/"+filename).getFile().getAbsolutePath();
System.out.println(realPath);

/* JavaWeb ServletContext
ServletContext servletContext = getServletContext();
String realPath = servletContext.getRealPath("/img/" + filename);*/

FileInputStream fis = new FileInputStream(realPath);

// response.setHeader("content-disposition","attachment;filename="+filename);
//设置中文乱码问题
response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes(), "iso8859-1"));
ServletOutputStream os = response.getOutputStream();

// IOUtils.copy(fis,os); //直接输入到输出
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
/**
* SpringMVC文件下载;
*/
@RequestMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws Exception{

//1、得到要下载的文件的流;
//找到要下载的文件的真实路径
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();

//2、将要下载的文件流返回
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对象(excel的文档对象)
HSSFWorkbook workbook = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet=workbook.createSheet("预验收信息");
//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1=sheet.createRow(0);
//设置头行高
row1.setHeightInPoints(22);
HSSFCell cellTiltle = row1.createCell(0);
//sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
cellTiltle.setCellStyle(columnTopStyle);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
//HSSFCell cell=row1.createCell(0);
//设置单元格内容
cellTiltle.setCellValue("预验收信息数据表");
//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));
//在sheet里创建第二行
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++) {
//String cldz = (String) ztbList.get(i).get("CLDZ").toString();
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.createCell(4).setCellValue((cldz == null ? "暂无数据" : cldz));
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.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;

}
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.setFontHeightInPoints((short)10);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
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;

}