Commit 7687fc7c authored by jiaorz's avatar jiaorz

车辆统计导出bug

parent ab96c218
......@@ -9,4 +9,5 @@ public class ExcelParamDto {
List<Object[]> data;
String[] header;
String name;
String path;
}
package com.xxfc.platform.vehicle.util.excel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
/***
* 将文件内容响应到浏览器
*/
public class DownloadUtil {
// 字符编码格式
private static String charsetCode = "utf-8";
/**
* 文件的内容类型
*/
private static String getFileContentType(String name){
String result = "";
String fileType = name.toLowerCase();
if (fileType.endsWith(".png")) {
result = "image/png";
} else if (fileType.endsWith(".gif")) {
result = "image/gif";
} else if (fileType.endsWith(".jpg") || fileType.endsWith(".jpeg")) {
result = "image/jpeg";
} else if(fileType.endsWith(".svg")){
result = "image/svg+xml";
}else if (fileType.endsWith(".doc")) {
result = "application/msword";
} else if (fileType.endsWith(".xls")) {
result = "application/x-excel";
} else if (fileType.endsWith(".zip")) {
result = "application/zip";
} else if (fileType.endsWith(".pdf")) {
result = "application/pdf";
} else {
result = "application/octet-stream";
}
return result;
}
/**
* 下载文件
* @param path 文件的位置
* @param fileName 自定义下载文件的名称
* @param resp http响应
* @param req http请求
*/
public static void downloadFile(String path, String fileName, HttpServletResponse resp, HttpServletRequest req){
File file = new File(path);
try {
/**
* 中文乱码解决
*/
String type = req.getHeader("User-Agent").toLowerCase();
if(type.indexOf("firefox")>0 || type.indexOf("chrome")>0){
/**
* 谷歌或火狐
*/
fileName = new String(fileName.getBytes(charsetCode), "iso8859-1");
}else{
/**
* IE
*/
fileName = URLEncoder.encode(fileName, charsetCode);
}
// 设置响应的头部信息
resp.setHeader("content-disposition", "attachment;filename=" + fileName);
// 设置响应内容的类型
resp.setContentType(getFileContentType(fileName)+"; charset=" + charsetCode);
// 设置响应内容的长度
resp.setContentLength((int) file.length());
// 输出
outStream(new FileInputStream(file), resp.getOutputStream());
} catch (Exception e) {
System.out.println("执行downloadFile发生了异常:" + e.getMessage());
} finally {
if (file.exists()) {
file.delete();
}
}
}
/**
* 基础字节数组输出
*/
private static void outStream(InputStream is, OutputStream os) {
try {
byte[] buffer = new byte[10240];
int length = -1;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
os.flush();
}
} catch (Exception e) {
System.out.println("执行 outStream 发生了异常:" + e.getMessage());
} finally {
try {
os.close();
} catch (IOException e) {
}
try {
is.close();
} catch (IOException e) {
}
}
}
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package com.xxfc.platform.vehicle.biz;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.order.contant.enumerate.OrderTypeEnum;
import com.xxfc.platform.order.entity.OrderRentVehicleDetail;
......@@ -18,6 +17,7 @@ import com.xxfc.platform.vehicle.mapper.VehicleCountRecordMapper;
import com.xxfc.platform.vehicle.pojo.ExcelParamDto;
import com.xxfc.platform.vehicle.pojo.VehicleBookRecordVo;
import com.xxfc.platform.vehicle.util.excel.ExcelExport;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
......@@ -28,9 +28,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -40,6 +43,7 @@ import java.util.stream.Collectors;
* 统计出车,交车数量
*/
@Service
@Slf4j
public class VehicleCountRecordBiz extends BaseBiz<VehicleCountRecordMapper, VehicleCountRecord> {
public static final DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd");
......@@ -363,10 +367,10 @@ public class VehicleCountRecordBiz extends BaseBiz<VehicleCountRecordMapper, Veh
}
}
public ObjectRestResponse export(ExcelParamDto excelParamDto) {
ExcelExport ee1 = new ExcelExport();
public ObjectRestResponse export(ExcelParamDto excelParamDto, HttpServletRequest request) {
ExcelExport ee1 = new ExcelExport(excelParamDto.getName() + ".xls");
ee1.addSheetByArray(excelParamDto.getName(), excelParamDto.getData(), excelParamDto.getHeader());
String path = "/data/temp/"+ excelParamDto.getName() + ".xlsx";
String path = "/data/temp/" + excelParamDto.getName() + ".xls";
File file = new File(path);
if (!file.exists()) {
try {
......@@ -390,30 +394,57 @@ public class VehicleCountRecordBiz extends BaseBiz<VehicleCountRecordMapper, Veh
return ObjectRestResponse.succ(path);
}
public void download(String path, HttpServletResponse response){
// 下载本地文件
String fileName = "export.xlsx".toString(); // 文件的默认保存名
// 读到流中
InputStream inStream = null;// 文件的存放路径
try {
inStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
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) {
public void download(String path, HttpServletRequest req, HttpServletResponse resp) {
String filename = "export";
DataInputStream in = null;
OutputStream out = null;
try{
resp.reset();// 清空输出流
String resultFileName = filename + System.currentTimeMillis() + ".xls";
resultFileName = URLEncoder.encode(resultFileName,"UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 设定输出文件头
resp.setContentType("application/msexcel");// 定义输出类型
//输入流:本地文件路径
in = new DataInputStream(
new FileInputStream(new File(path)));
//输出流
out = resp.getOutputStream();
//输出文件
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
} catch(Exception e){
e.printStackTrace();
resp.reset();
try {
OutputStreamWriter writer = new OutputStreamWriter(resp.getOutputStream(), "UTF-8");
String data = "<script language='javascript'>alert(\"\\u64cd\\u4f5c\\u5f02\\u5e38\\uff01\");</script>";
writer.write(data);
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
......
......@@ -4,12 +4,16 @@ import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.vehicle.biz.VehicleCountRecordBiz;
import com.xxfc.platform.vehicle.entity.VehicleCountRecord;
import com.xxfc.platform.vehicle.pojo.ExcelParamDto;
import com.xxfc.platform.vehicle.util.excel.DownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
@RestController
@Controller
@RequestMapping(value = "/vehicleCount")
public class VehicleCountRecordController {
......@@ -17,24 +21,26 @@ public class VehicleCountRecordController {
VehicleCountRecordBiz vehicleCountRecordBiz;
@GetMapping("/app/unauth/test")
@ResponseBody
public ObjectRestResponse add() {
vehicleCountRecordBiz.add();
return ObjectRestResponse.succ();
}
@GetMapping("/app/unauth/get")
@ResponseBody
public ObjectRestResponse getByTypeAndDate(VehicleCountRecord vehicleCountRecord) {
return vehicleCountRecordBiz.countDepartureVehicle(vehicleCountRecord);
}
@PostMapping("/app/unauth/export")
public ObjectRestResponse export(@RequestBody ExcelParamDto excelParamDto) {
return vehicleCountRecordBiz.export(excelParamDto);
@ResponseBody
public ObjectRestResponse export(@RequestBody ExcelParamDto excelParamDto, HttpServletRequest request) {
return vehicleCountRecordBiz.export(excelParamDto, request);
}
@PostMapping("/app/unauth/download")
public ObjectRestResponse download(@RequestBody String path, HttpServletResponse response) {
vehicleCountRecordBiz.download(path, response);
return ObjectRestResponse.succ();
@GetMapping("/app/unauth/download")
public void download(ExcelParamDto excelParamDto, HttpServletRequest request,HttpServletResponse response) {
DownloadUtil.downloadFile(excelParamDto.getPath(), "export.xls", response,request);
}
}
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