1.原始的方式
@ApiOperation(value = "下载参数导入模板")
@GetMapping("/template/parameters/excel/download")
public void downloadExcelTemplate(HttpServletResponse response, HttpServletRequest request) throws Exception {
ClassPathResource resource = new ClassPathResource("templates/parameterTemplate.xls");
InputStream in =resource.getInputStream();
String fileName = "parameterTemplate.xls";
int len ;
byte[] buffer = new byte[1024];
response.setHeader("content-disposition", String.format("attachment; filename=%s", fileName));
response.setCharacterEncoding("UTF-8");
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.flush();
in.close();
}
@ApiOperation(value = "下载参数导入模板")
@GetMapping(value = "/template/parameters/excel/download123",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadExcelTemplate22(HttpServletResponse response, HttpServletRequest request) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("信息表");
// 设置要导出的文件的名字
String fileName = "users" + new Date() + ".xls";
// 新增数据行,并且设置单元格数据
int rowNum = 1;
// headers表示excel表中第一行的表头 在excel表中添加表头
String[] headers = { "id", "uid", "地址", "城市"};
HSSFRow row = sheet.createRow(0);
for(int i=0;i<headers.length;i++){
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
for (int i = 0; i <10 ; i++) {
HSSFRow row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(00);
row1.createCell(1).setCellValue(01);
row1.createCell(2).setCellValue(02);
row1.createCell(3).setCellValue(03);
rowNum++;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
workbook.write(response.getOutputStream());
}
2.新的方式
@ApiOperation(value = "下载参数导入模板")
@GetMapping(value = "/template/parameters/excel/download2",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<byte[]> downloadExcelTemplate2() throws Exception {
String resourceLocation = "classpath:templates/1.jpg";
File downloadFile = ResourceUtils.getFile(resourceLocation);
return getDownloadHttpHeaders(downloadFile);
}
private ResponseEntity getDownloadHttpHeaders(File downloadFile) throws Exception {
byte[] output = new byte[0];
try {
output = FileUtils.readFileToByteArray(downloadFile);
} catch (IOException e) {
GlobalException.throwException("下载失败!");
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.valueOf(MediaType.APPLICATION_OCTET_STREAM_VALUE));
responseHeaders.setContentLength(output.length);
responseHeaders.set("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(downloadFile.getName(), "UTF-8")+"\"");
return new ResponseEntity<>(output, responseHeaders, HttpStatus.OK);
}
3. BufferedInputStream读取的流下载
private ResponseEntity getDownloadHttpHeaders() throws Exception {
AmazonS3 s3Client=S3Utils.getS3Client("XXXX","XXXX","http://1XXX");
S3Object s3Object = s3Client.getObject(
"s3_container",
"test.tar.gz"
);
BufferedInputStream bufferedInputStream = new BufferedInputStream(
s3Object.getObjectContent()
);
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
int len;
byte []buf=new byte[2048];
while((len=bufferedInputStream.read(buf))!=-1){
byteArrayOutputStream.write(buf, 0, len);
}
byteArrayOutputStream.flush();
byte[] output=byteArrayOutputStream.toByteArray();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.valueOf(MediaType.APPLICATION_OCTET_STREAM_VALUE));
responseHeaders.setContentLength(output.length);
responseHeaders.set("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("test.tar.gz", "UTF-8")+"\"");
return new ResponseEntity<>(output, responseHeaders, HttpStatus.OK);
}