|
|
@@ -1,6 +1,5 @@
|
|
|
package com.uas.report.controller;
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.util.HashMap;
|
|
|
@@ -9,12 +8,12 @@ import java.util.Map;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import org.apache.commons.lang.ArrayUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import com.uas.report.service.PrintService;
|
|
|
|
|
|
@@ -51,19 +50,20 @@ public class PrintController {
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping
|
|
|
- @ResponseBody
|
|
|
public String print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
|
|
|
- String exportFileType, HttpServletResponse response) {
|
|
|
+ String exportFileType, HttpServletRequest request, HttpServletResponse response) {
|
|
|
String message = "";
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
message = "未传入当前账套用户名!";
|
|
|
logger.error(message);
|
|
|
- return message;
|
|
|
+ request.setAttribute("message", message);
|
|
|
+ return "error.jsp";
|
|
|
}
|
|
|
if (StringUtils.isEmpty(reportName)) {
|
|
|
message = "报表名称无效!";
|
|
|
logger.error(message);
|
|
|
- return message;
|
|
|
+ request.setAttribute("message", message);
|
|
|
+ return "error.jsp";
|
|
|
}
|
|
|
|
|
|
logger.info("开始打印报表: " + reportName);
|
|
|
@@ -71,10 +71,18 @@ public class PrintController {
|
|
|
exportFileType = "pdf";
|
|
|
}
|
|
|
|
|
|
+ byte[] data = printService.print(userName, reportName, whereCondition, otherParameters, exportFileType);
|
|
|
+ if (ArrayUtils.isEmpty(data)) {
|
|
|
+ message = "报表 " + reportName + " 打印失败!";
|
|
|
+ logger.error(message);
|
|
|
+ request.setAttribute("message", message);
|
|
|
+ return "error.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + reportName + "." + exportFileType);
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
|
- printService.print(userName, reportName, whereCondition, otherParameters, exportFileType, outputStream);
|
|
|
+ outputStream.write(data);
|
|
|
outputStream.flush();
|
|
|
outputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
@@ -83,7 +91,7 @@ public class PrintController {
|
|
|
|
|
|
message = "报表 " + reportName + " 打印完成!";
|
|
|
logger.info(message);
|
|
|
- return message;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -93,8 +101,7 @@ public class PrintController {
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping("/test")
|
|
|
- @ResponseBody
|
|
|
- public String testPrint(HttpServletResponse response) {
|
|
|
+ public String testPrint(HttpServletRequest request, HttpServletResponse response) {
|
|
|
String reportName = "Purchase";
|
|
|
// String userName = "UAS";
|
|
|
// String whereCondition = "where pu_code = 'MP160800017' and pd_qty >
|
|
|
@@ -105,7 +112,7 @@ public class PrintController {
|
|
|
Map<String, Object> otherParameters = new HashMap<>();
|
|
|
otherParameters.put("OTHER_PARAMETER_TEST", "天气真好!");
|
|
|
String exportFileType = "xls";
|
|
|
- return print(userName, reportName, whereCondition, otherParameters, exportFileType, response);
|
|
|
+ return print(userName, reportName, whereCondition, otherParameters, exportFileType, request, response);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -130,22 +137,29 @@ public class PrintController {
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
message = "未传入当前账套用户名!";
|
|
|
logger.error(message);
|
|
|
- return message;
|
|
|
+ request.setAttribute("message", message);
|
|
|
+ return "error.jsp";
|
|
|
}
|
|
|
if (StringUtils.isEmpty(reportName)) {
|
|
|
message = "报表名称无效!";
|
|
|
logger.error(message);
|
|
|
- return message;
|
|
|
+ request.setAttribute("message", message);
|
|
|
+ return "error.jsp";
|
|
|
}
|
|
|
|
|
|
logger.info("开始打印报表: " + reportName);
|
|
|
String exportFileType = "pdf";
|
|
|
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
- printService.print(userName, reportName, whereCondition, otherParameters, exportFileType, outputStream);
|
|
|
- request.setAttribute("data", outputStream.toByteArray());
|
|
|
+ byte[] data = printService.print(userName, reportName, whereCondition, otherParameters, exportFileType);
|
|
|
+ if (ArrayUtils.isEmpty(data)) {
|
|
|
+ message = "报表 " + reportName + " 预览失败!";
|
|
|
+ logger.error(message);
|
|
|
+ request.setAttribute("message", message);
|
|
|
+ return "error.jsp";
|
|
|
+ }
|
|
|
+ request.setAttribute("data", data);
|
|
|
request.setAttribute("message", "test");
|
|
|
- message = "报表 " + reportName + " 打印完成!";
|
|
|
+ message = "报表 " + reportName + " 预览完成!";
|
|
|
logger.info(message);
|
|
|
- return "preview";
|
|
|
+ return "preview.jsp";
|
|
|
}
|
|
|
}
|