Controller层:
@ResponseBody
@RequestMapping(value = "", method = RequestMethod.POST)
public JSONObject addTroubleTicket(HttpServletRequest request,
@RequestParam(value="accessToken",required=true)String accessToken,
TroubleTicket troubleTicket,
@RequestParam(value="file",required=false) MultipartFile[] images) throws Exception{
troubleTicketReportService.addTroubleTicket(troubleTicket, images, accessToken);
return returnSuccess();
}
ps:图片表单提交,所以不能用@RequestBody接收参数,可以用对象接收。

Service层:
@Service("troubleTicketReportImpl")
public class TroubleTicketReportImpl implements TroubleTicketReportService{ @Resource(name="troubleTicketDaoImpl")
private TroubleTicketDao troubleTicketDao; @Resource(name="electrombileDaoImpl")
private ElectrombileDao electrombileDao; private final String upload_img_root = PropertiesUtil.getStringValue("upload_root"); private final String upload_img_report_dir = PropertiesUtil.getStringValue("upload_img_report_dir"); private final String upload_img_report_url = PropertiesUtil.getStringValue("upload_img_report_url"); //文件最大值 kb,默认1024kb
private final Integer max_img_size =PropertiesUtil.get("trouble_max_img_size", 1024); @Override
public void addTroubleTicket(TroubleTicket ttk,MultipartFile[] images,String accessToken) {
String electrombileCode = ttk.getElectrombileCode();
ValidUtil.valid("车辆编码", electrombileCode, "{'required':true,'regex':'" + RegexConstants.NUM_10_PATTERN + "'}"); String remark = ttk.getRemark();
if(remark != null && remark != ""){
ValidUtil.valid("备注", remark, "{'required':false,'regex':'" + RegexConstants.STRING_0_300_PATTERN + "'}");
} String reportContent = ttk.getReportContent();
if(reportContent != null && reportContent != ""){
ValidUtil.valid("故障内容", reportContent, "{'required':false,'regex':'" + RegexConstants.STRING_0_300_PATTERN + "'}");
} String types = ttk.getTroubleType();
if(types.isEmpty() || types ==null){
throw new BizException("E1000022", MessageUtil.getMessage("E1000022","维修类型"));//维修类型不存在
}
if(types.contains(",")){
String[] type = types.split(",");
for(String t :type){
if(null==TroubleType.contains(Integer.valueOf(t))){
throw new BizException("E1000015", MessageUtil.getMessage("E1000015","维修类型"));
}
}
}else {
if(null==TroubleType.contains(Integer.valueOf(types))){
throw new BizException("E1000015", MessageUtil.getMessage("E1000015","维修类型"));
}
} Electrombile electrombile = new Electrombile();
electrombile.setElectrombileCode(electrombileCode);
List<Electrombile> elist = electrombileDao.queryListByParams(electrombile,1,10);
if(elist.size() ==0 || elist == null || elist.size() > 1){
throw new BizException("E1000022", MessageUtil.getMessage("E1000022","车辆信息"));//车辆信息不存在
} //查看status=0,1,2,3的数据有几条
TroubleTicket troubleTicket = new TroubleTicket();
troubleTicket.setElectrombileCode(electrombileCode);
int size = troubleTicketDao.selectCountByStatus(troubleTicket);
if(size > 0){
throw new BizException("E1000014", MessageUtil.getMessage("E1000014","报修信息"));//报修信息存在
} String reportPhoto = "";
if(images != null && images.length!=0){
reportPhoto = saveImages(images);
} AppUserDTO usrDto = KeyUtil.getAppUserByAccessToken(accessToken);
AppUser user = usrDto.getUserInfo();
Integer subscriberId = user.getId();//用户ID // ttk.setTicketCode(String.valueOf(System.currentTimeMillis()));//工单编号 ttk.setId(null);
ttk.setReportDate(new Date());
ttk.setReportPhoto(reportPhoto);
ttk.setSubscriberId(subscriberId);
ttk.setStatus(0);
troubleTicketDao.insertSelective(ttk);
} @Override
public TroubleTicket queryByElectrombileCode(String accessToken) {
AppUserDTO usrDto = KeyUtil.getAppUserByAccessToken(accessToken);
AppUser user = usrDto.getUserInfo();
String electrombileCode = user.getElectrombileCode();
TroubleTicket ttk = troubleTicketDao.selectByElectrombileCode(electrombileCode);
return ttk;
} private String saveImages(MultipartFile[] images) { //当前时间 的 年月日路径 2017/06/27
String folderpath = DateUtil.getToday();
//如果不是以/ 结尾 添加斜杠/ /smart-lease-pub/imgreport --> /smart-lease-pub/imgreport/
String uploadImgReportDir = upload_img_report_dir;
if(!upload_img_report_dir.endsWith("/")){
uploadImgReportDir +="/";
} StringBuilder urls = new StringBuilder(); //最多只能上传4张报修图片
if(images.length>4) {
throw new ValidException("E1000017", MessageUtil.getMessage("E1000017","图片张数"));
} for(MultipartFile image : images){ // 文件类型校验 image jpeg bmp.
String contentType = image.getContentType();
if(!StringUtils.isBlank(contentType)){
if (contentType.equals("image/jpeg") || contentType.equals("image/png")
|| contentType.equals("image/bmp")) {
} else {
throw new ValidException("E1000015", MessageUtil.getMessage("E1000015","图片类型"));//图片类型不合法
}
} else {
throw new ValidException("E1000013", MessageUtil.getMessage("E1000013","图片类型")); //图片类型不允许为空
} //文件大小校验
if(image.getSize() == 0){
throw new ValidException("E1000013", MessageUtil.getMessage("E1000013","图片内容"));
} //文件大小大于配置最大值
long fileSize = image.getSize();
if(fileSize > max_img_size*1024){
throw new ValidException("E1000031", MessageUtil.getMessage("E1000031", new Object[]{"图片大小",max_img_size*1024}));
} //图片保存路径和访问URL不能为空
if(StringUtils.isBlank(upload_img_report_dir) || StringUtils.isBlank(upload_img_report_url)){
throw new ValidException("E1000013", MessageUtil.getMessage("E1000013","图片保存路径和访问URL"));
} String fileName = System.currentTimeMillis() + RandomUtil.getRandomNumber(4) + ".png"; //时间戳 + 四位随机数
//文件完整路径示例 : /service/media/smart-lease-pub/imgreport/2018/12/12/xxxxxx.png
String path = upload_img_root + uploadImgReportDir + folderpath + "/" ;
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
}
path += fileName;
File newFile=new File(path);
try {
image.transferTo(newFile);
}catch(Exception e) {
throw new BizException("E1000038", MessageUtil.getMessage("E1000038","保存图片"));
}
String url = upload_img_report_url + uploadImgReportDir + folderpath + "/" + fileName; urls.append(url);
urls.append(Constants.URL_SPLIT);//多个用|分割
}
return urls.substring(0,urls.length()-1);//去掉最后一个分隔符
} }

最新文章

  1. Mysql查询语句使用select.. for update导致的数据库死锁分析
  2. 【BZOJ】3670: [Noi2014]动物园
  3. JAVA多线程下载网络文件
  4. Sublime Text 3 常用快捷键总结
  5. 关于app transfer之后的开发
  6. Bzoj 1878: [SDOI2009]HH的项链 莫队
  7. Find your present (2) (位异或)
  8. 共享器 TS ERROR WINDOWS-FAILED 错误解决方法
  9. linux下一个php未找到php型材php.ini解决方案
  10. 1041: [HAOI2008]圆上的整点
  11. Linq 实例
  12. 机器学习之支持向量机(三):核函数和KKT条件的理解
  13. 网管到CEO的10年逆袭之路
  14. [Python设计模式] 第13章 造小人——建造者模式
  15. Knockout.Js官网学习(options绑定)
  16. odoo开发笔记 -- 前端开发相关
  17. ubuntu 安装redis
  18. Java 注解概要
  19. Vue 组件设计
  20. Spring-BeanFactory容器

热门文章

  1. 33、shuffle性能优化
  2. laravel 多控制器路由
  3. MySQL:服务无法启动(1067)问题
  4. c#调用phantomjs 将 网页 存为 PDF
  5. 【CSP模拟赛】奇怪的队列(树状数组 &amp;二分&amp;贪心)
  6. Ubuntu安装邮件服务器
  7. PHP是单线程还是多线程?
  8. Java IO系统--RandomAccessFile
  9. Web Application Framework
  10. github上fork分支后再合入原master分支的改动