码迷,mamicode.com
首页 > 其他好文 > 详细

server-controller

时间:2020-03-30 19:34:59      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:view   for   arraylist   rom   puts   生成   var   fhe   finally   

@Slf4j
@Controller
public class ExportPPTController {

private static final String REQ_FILES = "files";
private static final String REQ_DATA = "data";
private static final String REQ_TEMPLATE_ID = "templateId";
private static final String REQ_DOWNLOAD_FILE_NAME = "fileName";

@Autowired
private PdfHelper pdf2Helper;

@Autowired
private PPTConf pptConf;

@Autowired
private MessageSender messageSender;

@Autowired
private ViewOptionConvertReportService viewOptionConvertReportService;

@PostMapping("/ppt/handler")
public void handler(HttpServletRequest request, HttpServletResponse httpResponse) {
String downloadFileName = "" + System.currentTimeMillis();
try (XMLSlideShow ppt = genPPT(request)) {

downloadFileName = ((MultipartHttpServletRequest) request).getParameter(REQ_DOWNLOAD_FILE_NAME);
downloadFileName =
SafeString.filterFileName(StringUtil.isNotBlank(downloadFileName) ? downloadFileName : "" + System.currentTimeMillis());

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

ppt.write(outputStream);
httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH, "" + outputStream.size());
httpResponse.setContentType("application/force-download");// 设置强制下载不打开
httpResponse.addHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + new String((downloadFileName + ".pptx").getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
ppt.write(httpResponse.getOutputStream());

} catch (Exception e) {
log.error("生成PPT[" + downloadFileName + "]时出错 " + e.getMessage(), e);
}
}


@PostMapping(value = "/pdf/handler", produces = MediaType.APPLICATION_JSON_VALUE)
public void pdfHandlerWithOffice(HttpServletRequest request, HttpServletResponse httpResponse) {
String downloadFileName = "" + System.currentTimeMillis();
try (XMLSlideShow ppt = genPPT(request)) {

downloadFileName = ((MultipartHttpServletRequest) request).getParameter(REQ_DOWNLOAD_FILE_NAME);
downloadFileName =
SafeString.filterFileName(StringUtil.isNotBlank(downloadFileName) ? downloadFileName : "" + System.currentTimeMillis());


pdf2Helper.ppt2PdfWithJacob(ppt, httpResponse.getOutputStream(), pptConf.getTemplateFolder());

// httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH, "" + outputStream2.size());
httpResponse.setContentType("application/force-download");// 设置强制下载不打开
httpResponse.addHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + new String((downloadFileName + ".pdf").getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
} catch (Exception e) {
log.error("生成PPT[" + downloadFileName + "]时出错 " + e.getMessage(), e);
}
}

@PostMapping("/autoReport/export")
public void autoReport(HttpServletRequest request, HttpServletResponse httpResponse) {

var data = request.getParameter(REQ_DATA);
log.info("自动报告导出request===>{}", data);
ReportExportRequest reportExportRequest = JSONObject.parseObject(data, ReportExportRequest.class);
if (null == reportExportRequest) {
return;
}
StopWatch stopWatch = new StopWatch();
stopWatch.start("export ppt");
String reportNum = reportExportRequest.getReportNum();
RiflemanConfig riflemanConfig = reportExportRequest.getRiflemanConfig();
String fileType = reportExportRequest.getFileType();
String fileName = reportExportRequest.getFileName();
GlobalContext globalContext = reportExportRequest.getGlobalContext();

ExportPPTMessage exportPPTMessage = new ExportPPTMessage();
exportPPTMessage.setReportNum(reportNum);

try {
Map<String, Object> context = ObjectUtils.convert2map(globalContext, true);
fileName = SafeString.filterFileName(StringUtils.isNotEmpty(fileName) ? fileName : System.currentTimeMillis() + "");
PPTReport pptReport = viewOptionConvertReportService.convert(context, reportNum, riflemanConfig);
XMLSlideShow ppt = AutoReportGenerator.generatorPPT(pptReport, pptConf.getTemplateFolder(), pptConf.getTempFileFolder());

FileOutputStream fileO = new FileOutputStream(pptConf.getTempFileFolder() + "自动报告.pptx");
ppt.write(fileO);
fileO.close();
httpResponse.setContentType("application/force-download");// 设置强制下载不打开
if ("pptx".equals(fileType)) {//ppt
// ByteArrayOutputStream outputStream = null;
// try {
// outputStream = new ByteArrayOutputStream();
// ppt.write(outputStream);
// outputStream.flush();
// } finally {
// outputStream.close();
// ppt.close();
// }
// outputStream.writeTo(httpResponse.getOutputStream());

fileName += ".pptx";
ppt.write(httpResponse.getOutputStream());
httpResponse.getOutputStream().flush();
} else if ("pdf".equals(fileType)) {//pdf
fileName += ".pdf";
pdf2Helper.ppt2PdfWithJacob(ppt, httpResponse.getOutputStream(), pptConf.getTemplateFolder());
}
exportPPTMessage.setStatus("T");
httpResponse.addHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + new String((fileName).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));

stopWatch.stop();
long time = stopWatch.getTotalTimeMillis();
log.info("export ppt success! use {} ms.", time);
} catch (Exception e) {
log.error("生成[" + fileName + "]时出错" + e.getMessage(), e);
exportPPTMessage.setStatus("F");
}

log.info("ppt生成完成,发送mq消息;reportNum={}", reportNum);
messageSender.send(exportPPTMessage);
}

private XMLSlideShow genPPT(HttpServletRequest request) throws IOException {
val multiReq = (MultipartHttpServletRequest) request;
val files = multiReq.getFiles(REQ_FILES);
val data = multiReq.getParameter(REQ_DATA);
var templateId = multiReq.getParameter(REQ_TEMPLATE_ID);

log.info(data);
MultipartFile file = null;
Map<String, byte[]> fileMap = Maps.newHashMap();
for (var i = 0; i < files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
fileMap.put(file.getOriginalFilename(), file.getBytes());
} else {
// does nothing
}
}

List<SlideData> slideDataList = JSON.parseArray(data, SlideData.class);

slideDataList.forEach(slide -> {
if (CollectionUtils.isNotEmpty(slide.getImageDataList())) {
slide.getImageDataList().forEach(img -> {
if (fileMap.containsKey(img.getPicId())) {
img.setPicBytes(fileMap.get(img.getPicId()));
}
});
}
});

return PowerPointGenerator.generatorPowerPoint(pptConf.getTemplateFolder() + SafeString.filterFileName(templateId) + ".pptx", slideDataList);
}

f
@Controller
@Slf4j
public class ExportWordController {
@Autowired
private PPTConf pptConf;

@Autowired
private ReportGenerateService reportGenerateService;

@PostMapping(value = "/word/export", produces = MediaType.APPLICATION_JSON_VALUE)

public void exportWord(HttpServletRequest request, HttpServletResponse httpResponse) {
var data = request.getParameter("data");
List<WordExportRequest> wordExportRequests = JSON.parseArray(data, WordExportRequest.class);
try {
reportGenerateService.htmlToWord(httpResponse.getOutputStream(), wordExportRequests);
httpResponse.setContentType("application/force-download");// 设置强制下载不打开
httpResponse.addHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + new String((System.currentTimeMillis() + ".doc").getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
} catch (IOException e) {
log.error("htmlToWord失败,e==>{}", e.getMessage(), e);
}
}
}


@Slf4j
@RestController
public class RiflemanController {
@Autowired
private PPTConf pptConf;

@Autowired
private ScreenService screenService;

@PostMapping("/rifleman/shotChart")
public List<ScreenResultDTO> shotChart(HttpServletRequest request, HttpServletResponse httpResponse) {
List<ScreenResultDTO> screenResultDTOS = Lists.newArrayList();
try {
String data = request.getParameter("data");
RiflemanConfig riflemanConfig = JSON.parseObject(data, RiflemanConfig.class);
List<ScreenConfig> screenConfigs = riflemanConfig.getScreenConfigs();
// ScreenUtils.screen(riflemanConfig.getJsName(), riflemanConfig.getUrl(), riflemanConfig.getPageViewSize()
// , screenConfigs, riflemanConfig.getParam());
ScreenUtils.chromeDriverScreen(riflemanConfig, pptConf.getTempFileFolder(), pptConf.getChromeDriverPath());

List<String> tempFiles = Lists.newArrayList();
for (int i = 0; i < screenConfigs.size(); i++) {
ScreenResultDTO screenResultDto = new ScreenResultDTO();
String picName = screenConfigs.get(i).getPicName();
tempFiles.add(picName);
byte[] picBytes = ScreenUtils.getScreenPicBytes(picName, pptConf.getTempFileFolder());
screenResultDto.setFileName(picName);
screenResultDto.setFileBytes(picBytes);
screenResultDTOS.add(screenResultDto);
}

FileUtils.removeTempFile(pptConf.getTempFileFolder(), tempFiles);
} catch (Exception e) {
log.error("截屏时出错 " + e.getMessage(), e);
}
return screenResultDTOS;
}

@PostMapping("/rifleman/scrollScreenShot")
public List<ScreenResultDTO> scrollScreenShot(HttpServletRequest request, HttpServletResponse httpResponse) {
List<ScreenResultDTO> screenResultDTOS = Lists.newArrayList();
try {
String data = request.getParameter("data");
RiflemanConfig riflemanConfig = JSON.parseObject(data, RiflemanConfig.class);
List<ScreenConfig> screenConfigs = riflemanConfig.getScreenConfigs();
screenService.scrollScreenShot(riflemanConfig);

List<String> tempFiles = Lists.newArrayList();
for (int i = 0; i < screenConfigs.size(); i++) {
ScreenResultDTO screenResultDto = new ScreenResultDTO();
String picName = screenConfigs.get(i).getPicName();
tempFiles.add(picName);
byte[] picBytes = ScreenUtils.getScreenPicBytes(picName, pptConf.getTempFileFolder());
screenResultDto.setFileBytes(picBytes);
screenResultDto.setFileName(picName);
screenResultDTOS.add(screenResultDto);
}

FileUtils.removeTempFile(pptConf.getTempFileFolder(), tempFiles);
} catch (Exception e) {
log.error("截屏时出错 " + e.getMessage(), e);
}
return screenResultDTOS;
}
}




}

server-controller

标签:view   for   arraylist   rom   puts   生成   var   fhe   finally   

原文地址:https://www.cnblogs.com/shuaiqing/p/12600341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!