数据库中存放问价的为blob类型,在Java中是byte[]类型
/*
* 新增闹钟
* clockType:0、1、2为非自定义闹钟提醒;3、4、5为自定义闹钟提醒
* */
@Authorization
@ResponseBody
@RequestMapping(value = "/addAlarmClock" , method = RequestMethod.POST)
public JsonResult addAlarmClock(@CurrentUser User loginUser , HttpServletResponse response,
HttpServletRequest request) {
Map errorMap = new HashMap();
Integer deviceId = Integer.parseInt(request.getParameter("deviceId"));
Integer clockType = Integer.parseInt(request.getParameter("clockType"));
String clockTime = request.getParameter("clockTime");
Integer repeatMon = Integer.parseInt(request.getParameter("repeatMon"));
Integer repeatTue = Integer.parseInt(request.getParameter("repeatTue"));
Integer repeatWed = Integer.parseInt(request.getParameter("repeatWed"));
Integer repeatThu = Integer.parseInt(request.getParameter("repeatThu"));
Integer repeatFri = Integer.parseInt(request.getParameter("repeatFri"));
Integer repeatSat = Integer.parseInt(request.getParameter("repeatSat"));
Integer repeatSun = Integer.parseInt(request.getParameter("repeatSun"));
// String deviceNo = request.getParameter("deviceNo");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Collection files = multipartRequest.getFileMap().values();
//校验
if (deviceId == null) {
errorMap.put("deviceId", "设备id不能为空");
return new JsonResult(false,"设备id参数错误",errorMap);
}
if (deviceId <= 0 || deviceId > 2147483647) {
errorMap.put("deviceId", "设备id范围错误,应是1-2147483647");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (clockType == null) {
errorMap.put("clockType", "闹钟类型不能为空");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (clockType < 0 || clockType > 5) {
errorMap.put("clockType", "闹钟类型范围错误,应是0-5");
return new JsonResult(false,"设置参数错误",errorMap);
}
String pattern = "^([01]\\d|2[0123])([0-5]\\d|60)$";
//闹钟提醒时间
if (StringUtils.isNotBlank(clockTime) && !Pattern.matches(pattern, clockTime)) {
errorMap.put("clockTime", "闹钟提醒时间格式错误");
return new JsonResult(false, "设置参数错误", errorMap);
}
if (repeatMon == null || repeatMon < 0 || repeatMon > 1) {
errorMap.put("repeatMon", "周一是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (repeatTue == null || repeatTue < 0 || repeatTue > 1) {
errorMap.put("repeatTue", "周二是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (repeatWed == null || repeatWed < 0 || repeatWed > 1) {
errorMap.put("repeatWed", "周三是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (repeatThu == null || repeatThu < 0 || repeatThu > 1) {
errorMap.put("repeatThu", "周四是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (repeatFri == null || repeatFri < 0 || repeatFri > 1) {
errorMap.put("repeatFri", "周五是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (repeatSat == null || repeatSat < 0 || repeatSat > 1) {
errorMap.put("repeatSat", "周六是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
if (repeatSun == null || repeatSun < 0 || repeatSun > 1) {
errorMap.put("repeatSun", "周日是否重复范围错误");
return new JsonResult(false,"设置参数错误",errorMap);
}
/*if (StringUtils.isBlank(deviceNo) || deviceNo.length() > 64) {
errorMap.put("deviceNo", "设备编码参数范围是1-64");
return new JsonResult(false,"设置参数错误",errorMap);
}*/
if ((clockType == 3 || clockType == 4 || clockType == 5) && files.isEmpty()) {
errorMap.put("voiceFile", "语音提醒文件为空");
return new JsonResult(false,"设置参数错误",errorMap);
}
//根据Id查找设备是否存在
Device device = deviceService.deviceShow(deviceId);
if (device == null || StringUtils.isBlank(device.getDeviceno()) || device.getDevicetype() != 1 ) {
return new JsonResult(false, "根据deviceId未查询到设备", null);
}
//由于指令会出现覆盖的问题,所以在下发前需要查询出该手环之前设置的闹钟提醒
Map param = new HashMap<>();
param.put("deviceId",deviceId);
List
/*
* 根据id查询某条提醒的语音文件
* */
@ResponseBody
@RequestMapping(value = "/selectFileById")
public JsonResult selectFileById(Integer id,HttpServletRequest request,HttpServletResponse response) throws IOException {
if (id == null || id <= 0 || id > 2147483647) {
return new JsonResult(false, "参数错误", null);
}
WatchAlarmClock watchAlarmClock = watchAlarmClockService.selectByid(id);
if (watchAlarmClock == null) {
return new JsonResult(false, "没有此id", null);
}
byte[] data = watchAlarmClock.getVoiceData();
String fileName = watchAlarmClock.getId().toString() + ".amr";
// System.out.println("fileName: " + fileName);
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
return new JsonResult(true, "查询成功", null);
}