文章目录
1. url参数和请求体json
1.1 获取url请求参数
getRequest().getParameter("accountId");
super.getRequest().getParameter("accountId");
1.2 获取请求体中的json对象
RequestPayBodyConvertDevice convertDevice = new RequestPayBodyConvertDevice();
DeviceWrapper deviceWrapper = convertDevice.convert(super.getRequest());
DeviceInfoDto device = deviceWrapper.getDevice();
1.3 获取请求体中的formdata参数对象
String requestParms = super.getRequest().getParameter("requestParms");
RequestPayBodyConvertPortal convertPortal = new RequestPayBodyConvertPortal();
PortalWrapper portalWrapper = convertPortal.jsonToObject(requestParms);
PortalNoticeInfoDTO portal = portalWrapper.getPortal();
2. 查询请求消息体转换为查询信息对象
当前台传递的查询参数为Body请求体时,需要把请求消息体转换为查询信息对象
//RequestBodyConvertObjects.toQueryInfo: 封装好的方法
//getRequest(): 消息对象
QueryInfo queryInfo = RequestBodyConvertObjects.toQueryInfo(getRequest());
3. 验证查询条件
validateQueryInfo(queryInfo);
4. 判断集合是否为空
//CollectionUtils: 集合工具
(CollectionUtils.isNotEmpty(list))
5. 查询数据
5.1 通过queryInfo 查询数据
modelService.findByQueryInfo(A.class, queryInfo);
5.2. 通过sql 查询数据
// A.class 查询对象的Class
// Sql sql语句
// new Object[] {orgNo} 给sql语句的占位符赋值
String Sql = "....";
modelService.findBySQL(A.class, Sql, new Object[] {orgNo});
5.3 通过queryInfo和sql 查询数据
modelService.findByQueryInfo(PortalNoticeInfo.class, sql, queryInfo);
7. 将查询到的分页数据转Json
super.pageInfoToJson(数据)
8. 获取上传的文件
getMultipartFile("file")
9. 获取上传文件名字和后缀
String fileName = file.getOriginalFilename();
String fileType = FilenameUtils.getExtension(fileName)
10. 根据委托id生成委托附件根路径
ZiGuiFilePathUtils.getEntrustAnnexRootPath(entrustId) + File.separator + IDUtils.getUUID() + "." + fielType
11. 上传文件到服务器操作
11.1 文件-----根据文件路径创建目录(目录+文件)
FileSupportUtils.create(filePath)
11.2 图片-----根据路径创建目录(目录+图片)
FileSupportUtils.create(filePath);
File portalPhotoFile = new File(filePath);
file.transferTo(portalPhotoFile);
12. 执行操作
可以在Controller层执行多条数据库操作并都携带事务
modelService.doOperation(() -> {
});
13. 验证请求参数长度
校验数据长度工具类.验证请求参数长度
DataLenCheckUtil.checkAttributeValueLength(Object obj)
14 获取当前年份
Calendar.getInstance().get(Calendar.YEAR)
15 获取当前时间
DateSupportUtils.formatDateTime(new Date())
16 中文入库变成?
# 在数据库连接后加上编码
url: jdbc:mysql://localhost:3306/zigui?useUnicode=true&characterEncoding=UTF-8
17 响应一个文件
responseFile(new File(文件路径));
18 文件转base64
FileUtils.fileToBase64(filePath);
19 base64转文件
String base64String = super.getRequest().getParameter("file");//获取前端传来的base64码
String base64 = base64String;//用作截取文件后缀使用
base64String = base64String.split(",")[1];//去掉base64码的data:image/png;base64,
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(base64String);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
String fileType = base64.substring(12,15); //从base64中截取后缀
OutputStream out = new FileOutputStream(filePath);
out.write(bytes); //输出文件到磁盘
}catch (Exception e) {
}
20 判断文件是否存在
FileSupportUtils.isExist(filePath);
new File(path).exists();
21. 从服务器项目位置下载文件
模板文件跟随项目一块打包到服务器上,需要文件时点击下载到本地
//把项目模板文件复制到指定路径
InputStream inputStream = ResourcesUtils.getResourceAsStream("/template/device_template.xlsx");
Objects.requireNonNull(inputStream,"没有找到device_template.xlsx");
byte[] templateFileByteArray = IOUtils.toByteArray(inputStream);
IOUtilsE.closeQuietly(inputStream);
//把字节数组写入到指定文件 templateFileByteArray --> tempFile
//filePath: 服务器临时存储的文件路径,响应文件后删除该临时文件
FileUtils.writeByteArrayToFile( new File(filePath), templateFileByteArray);
22. 解析表格xlsx数据
XSSFWorkbook xssfWorkbook = null; //创建工作簿对象 xlsx
try{
xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath)); //创建工作簿对象 xlsx
int nums = xssfWorkbook.getNumberOfSheets(); //获取工作簿下sheet的个数
XSSFSheet sheet = null;
for(int i = 1 ; i <= nums ; i++ ){
sheet = xssfWorkbook.getSheetAt(i); //遍历得到每一个sheet页
int maxRow = sheet.getLastRowNum(); //获取每一个sheet页的表格总行数
int maxRow = sheet.getLastRowNum(); //获取表格总行数
for(int row = 3 ; row <= maxRow ; row++ ) {
//某一行某一列,得到一个单元格
XSSFCell Cell = sheet.getRow(row).getCell(2); //得到一个单元格,2是列 可以修改
Cell.setCellType(CellType.STRING); //如果单元格类型不是string,则设置为string. 还有其他数据类型
String value = Cell.getStringCellValue(); //得到单元格的值
}
}
}finally {
xssfWorkbook.close();//关流
}
23 只执行具体sql
modelService.getBaseDataBaseOperation().select(sql);