BrCodeController.java
4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//: com.huaheng.pc.manager.brcode.controller/BrCodeController.java
package com.huaheng.pc.manager.brcode.controller;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.pc.manager.brcode.domain.BrCode;
import com.huaheng.pc.manager.brcode.service.BrCodeService;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.*;
/** A class comment */
@Controller
@RequestMapping("/manager/BrCode")
public class BrCodeController extends BaseController {
/** A field comment */
private String prefix = "/manager/BrCode";
@Resource
private BrCodeService brCodeService;
@Resource
private DataSource dataSource;
@Resource // 启动失败是注入类错误
private HttpServletResponse response;
@GetMapping()
public String BrCodeScanner () {
return prefix + "/brcode";
}
/** A method comment */
@PostMapping("/printBrCode")
public void printBrCode (Integer page) {
Connection connection = null;
Collection<BrCode> brCodes = null;
if(page != 0)
// 查询
brCodes = brCodeService.query(page);
try {
/*加载模板-begin*/
// 获取文件流
final ClassPathResource resource = new ClassPathResource("jaspers/BrCodePF.jasper"); // 要打印的文件的ClassPathResource √
InputStream in = resource.getInputStream(); // 获取输入流
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(in); // 利用输出和输入流以及JasperReport对象提供的方法来返回一个pdf文件 JasperReport对象传入文件
/*加载模板-end*/
// 打印PDF对象
JRAbstractExporter exporter = new JRPdfExporter();
connection = dataSource.getConnection();
List<JasperPrint> list = new LinkedList<>();
// 格式化模板参数出入
Map<String, Object> param = new HashMap<>();// param key1:left;key2:right
// Useless
// param.keySet().toArray()[0]=new String[]{"left"};
// param.keySet().toArray()[1]= new String[]{"right"};
for (int i = 0; i < brCodes.size(); i++) {
Object right, left;
param.put("left", ((BrCode) brCodes.toArray()[i]).getCode());// 右边总是比左边大
// 判断是否为双数追后执行
i++;
if( i< brCodes.size())
right= ((BrCode) brCodes.toArray()[i]).getCode();
else
right=null;
param.put("right", right);// 自增1并放入
// 模板传入参数后
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, connection); // 填充jasperReport编译报表 报告参数paramters{(map) mysql连接
list.add(jasperPrint);
}
// 下面是固定输出数据源
ServletOutputStream output = response.getOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, list);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output); // 序列化jasper对象
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
// the end
exporter.exportReport();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@GetMapping("create")
public void create () {
BrCode brCode = new BrCode();
brCode.setCreatedBy(ShiroUtils.getUser().getLoginName());
brCode.setLastUpdatedBy(ShiroUtils.getUser().getLoginName());
for (int i = 0; i < 1000000; i++) {
brCode.setCode("E"+String.format("%06d", i)+"^");
brCodeService.save(brCode);
}
}
} ///:~