/**
* 下载用户信息填写模板使用说明
* @param httpServletResponse response对象
* @return
* @throws Exception
*/
@RequestMapping("/downloadReadFile")
public ResponseEntity<byte[]> downloadReadFile(HttpServletResponse httpServletResponse) throws Exception {
byte [] body = null;
//获得输入流
InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/说明.docx");
body = new byte[in.available()];
in.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + new String("说明.docx".getBytes(), "ISO-8859-1"));
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}
/**
* 下载用户信息填写模板
* @param httpServletResponse response对象
* @return
* @throws Exception
*/
@RequestMapping("/downloadExcel")
public ResponseEntity<byte[]> downloadExcel(HttpServletResponse httpServletResponse) throws Exception {
byte [] body = null;
//获得输入流
InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/用户模板.xls");
body = new byte[in.available()];
in.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + new String("用户模板.xls".getBytes(), "ISO-8859-1"));
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}
* 下载用户信息填写模板使用说明
* @param httpServletResponse response对象
* @return
* @throws Exception
*/
@RequestMapping("/downloadReadFile")
public ResponseEntity<byte[]> downloadReadFile(HttpServletResponse httpServletResponse) throws Exception {
byte [] body = null;
//获得输入流
InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/说明.docx");
body = new byte[in.available()];
in.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + new String("说明.docx".getBytes(), "ISO-8859-1"));
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}
/**
* 下载用户信息填写模板
* @param httpServletResponse response对象
* @return
* @throws Exception
*/
@RequestMapping("/downloadExcel")
public ResponseEntity<byte[]> downloadExcel(HttpServletResponse httpServletResponse) throws Exception {
byte [] body = null;
//获得输入流
InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/用户模板.xls");
body = new byte[in.available()];
in.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + new String("用户模板.xls".getBytes(), "ISO-8859-1"));
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}
本文提供了一个简单的示例,展示如何通过Java代码实现从服务器下载指定的文件,包括文档模板和Excel模板。具体步骤包括获取输入流、读取文件内容、设置HTTP响应头,并返回包含文件数据的响应实体。
681

被折叠的 条评论
为什么被折叠?



