適用於意見反饋java
public Map<String, Object> saveOpinion(HttpServletRequest request, String title, String situationLevel, String content,String userId) { Map<String, Object> map=new HashMap<String, Object>(); AppOpinionEntity opinionEntity=new AppOpinionEntity(); opinionEntity.setTitle(title); opinionEntity.setContent(content); opinionEntity.setSituationLevel(situationLevel); opinionEntity.setUserId(userId); opinionEntity.setCreateBy("1"); opinionEntity.setCreateDate(DateTimeUtils.getDateTime()); opinionEntity.setUpdateBy("1"); opinionEntity.setUpdateDate(DateTimeUtils.getDateTime()); opinionEntity.setDelTag("0"); opinionDao.save(opinionEntity); MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request; Iterator<String> ite=mr.getFileNames(); while (ite.hasNext()) { String name = ite.next(); List<MultipartFile> file = mr.getFiles(name); if (file != null || file.size() != 0) { for (MultipartFile files : file) { AppOpinionImgEntity opinionImgEntity = new AppOpinionImgEntity(); opinionImgEntity.setCreateBy("1"); opinionImgEntity.setCreateDate(DateTimeUtils.getDateTime()); opinionImgEntity.setUpdateBy("1"); opinionImgEntity.setUpdateDate(DateTimeUtils.getDateTime()); opinionImgEntity.setDelTag("0"); String sysPath = request.getSession().getServletContext().getRealPath("/"); String path = "upload/opinion/"; try { String fileName = files.getOriginalFilename(); opinionImgEntity.setImgName(fileName); opinionImgEntity.setOpinionId(opinionEntity.getId()); if (StringUtils.isNotBlank(fileName)) { String[] str = fileName.split("\\."); if (str.length == 1) { path = path + new Date().getTime() + str[0] + ".JPEG";//蘋果不帶後綴圖片處理 opinionImgEntity.setImgType("JPEG"); } else { path = path + new Date().getTime() + "." + str[str.length - 1]; opinionImgEntity.setImgType(fileName.substring(fileName.lastIndexOf(".")));//獲取圖片格式 } } // 文件保存路徑 File fl = new File(sysPath + path); if (!fl.getParentFile().exists()) { fl.getParentFile().mkdirs(); } // 轉存文件 files.transferTo(fl); } catch (Exception e) { e.printStackTrace(); return new HashMap<String, Object>(); } opinionImgEntity.setImgUrl(path); opinionImgDao.save(opinionImgEntity); } } } map.put("type", "1"); return map; }