package com.keydak.common.util;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.function.Consumer;
import static com.keydak.base.utils.CloseUtil.close;
@Slf4j
public class DownloadUtil {
public static void download(HttpServletResponse response, String srcFilePath, String targetName) {
FileInputStream in = null;
BufferedInputStream bis = null;
try {
in = new FileInputStream(srcFilePath);
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment;fileName=" + targetName + ";filename*=utf-8''" + URLEncoder.encode(targetName, "UTF-8"));
int available = in.available();
response.setHeader("Content-Length", String.valueOf(available));
// 发送给客户端的数据
OutputStream outputStream = response.getOutputStream();
byte[] buff = new byte[1024];
// 读取filename
bis = new BufferedInputStream(in);
int i = bis.read(buff);
while (i != -1) {
outputStream.write(buff, 0, buff.length);
outputStream.flush();
i = bis.read(buff);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(in);
}
}
/**
* 浏览器下载
*
* @param response 响应流
* @param fileName 文件名
* @param consumer 流的处理
*/
public static void download(HttpServletResponse response, String fileName, Consumer<OutputStream> consumer) {
try {
String targetName = fileName;
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(targetName, "UTF-8") + ";filename*=utf-8''" + URLEncoder.encode(targetName, "UTF-8"));
// 发送给客户端的数据
OutputStream outputStream = response.getOutputStream();
if (consumer != null) {
consumer.accept(outputStream);
}
} catch (Exception e) {
log.error("下载失败:" + e.getMessage());
}
}
}
@GetMapping("/excel")
@ApiOperation(value = "导出邮件发送记录Excel")
@OpLog(
outputExpression = "{#code}"
)
public void exportMailRecord(HttpServletResponse response, MailQueryParam queryParam) {
String targetName = "邮件发送记录.xlsx";
queryParam.setPageSize(999999);
IPage<MailLogEntity> mailLogEntityIPage = mailManagerService.find(queryParam);
List<MailExportExcelParam> exportList = mailLogEntityIPage.getRecords().stream()
.map(this::convertToExportParam)
.collect(Collectors.toList());
OpLogAspect.setProcessDescription("导出文件名:" + targetName);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + targetName + "\"");
try {
DownloadUtil.download(response, targetName, stream -> {
EasyExcel.write(stream, MailExportExcelParam.class).sheet("邮件发送记录").doWrite(exportList);
});
} catch (Exception e) {
// 处理异常
e.printStackTrace();
}
}
/**
* 邮件发送记录
*
* @param mailLogEntity
* @return {@link SmtExportExcelParam}
*/
private MailExportExcelParam convertToExportParam(MailLogEntity mailLogEntity) {
return MailExportExcelParam.builder()
.address(mailLogEntity.getAddress())
.subject(mailLogEntity.getSubject())
.source(mailLogEntity.getSource())
.createDate(mailLogEntity.getCreateDate() + "")
.valid(mailLogEntity.getValid() ? "成功" : "失败")
.build();
}
package com.keydak.message.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author xjl
* @date 2024/06/20
*/
@ApiModel(value = "短信导出参数")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MailExportExcelParam implements Serializable {
@ApiModelProperty(value = "接收人邮箱")
@ExcelProperty("接收人邮箱")
private String address;
@ApiModelProperty(value = "邮件主题")
@ExcelProperty("邮件主题")
private String subject;
@ApiModelProperty(value = "来源")
@ExcelProperty("来源")
private String source;
@ApiModelProperty(value = "时间")
@ExcelProperty("时间")
private String createDate;
@ApiModelProperty(value = "状态")
@ExcelProperty("状态")
private String valid;
}