Commit 3203a55b authored by jiaorz's avatar jiaorz

Merge branch 'master-count-vehicle' into base-modify

parents bef52e5f 782a04d9
......@@ -362,38 +362,49 @@ public class VehicleCountRecordBiz extends BaseBiz<VehicleCountRecordMapper, Veh
}
}
public void export(HttpServletResponse response, ExcelParamDto excelParamDto) {
public String export(ExcelParamDto excelParamDto) {
ExcelExport ee1 = new ExcelExport();
ee1.addSheetByArray(excelParamDto.getName(), excelParamDto.getData(), excelParamDto.getHeader());
String fileName = "/data/temp/"+ excelParamDto.getName() + ".xlsx";
OutputStream fis;
try {
fis = new FileOutputStream( "/data/temp/"+ excelParamDto.getName() + ".xlsx");
fis = new FileOutputStream(fileName);
ee1.getWorkbook().write(fis);
// 下载本地文件
String fileName = excelParamDto.getName() + ".xlsx"; // 文件的默认保存名
// 读到流中
InputStream inStream = new FileInputStream("/data/temp/" + fileName);// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
fis.flush();
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
public void download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
......
......@@ -28,8 +28,13 @@ public class VehicleCountRecordController {
}
@PostMapping("/app/unauth/export")
public ObjectRestResponse export(HttpServletResponse response, @RequestBody ExcelParamDto excelParamDto) {
vehicleCountRecordBiz.export(response, excelParamDto);
public ObjectRestResponse export(@RequestBody ExcelParamDto excelParamDto) {
return ObjectRestResponse.succ(vehicleCountRecordBiz.export(excelParamDto));
}
@PostMapping("/app/unauth/download")
public ObjectRestResponse download(String path, HttpServletResponse response) {
vehicleCountRecordBiz.download(path, response);
return ObjectRestResponse.succ();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment