package com.ygj.yuemum.controller.admin; import com.alibaba.fastjson.JSONObject; import com.ygj.yuemum.domain.admin.Photo; import com.ygj.yuemum.domain.admin.Resume; import com.ygj.yuemum.domain.maternitymatron.JlServiceUser; import com.ygj.yuemum.service.admin.PhotoService; import com.ygj.yuemum.service.admin.ResumeService; import com.ygj.yuemum.service.maternitymatron.JlServiceUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @RestController public class ImageUploadController { @Autowired private PhotoService photoService; @Autowired private JlServiceUserService jlServiceUserService; @Autowired private ResumeService resumeService; private ResourceLoader resourceLoader; public static final String ROOT = "D:/apache-tomcat-9.0.6/webapps/resume/img/"; public static final String ResumeFile = "D:/apache-tomcat-9.0.6/webapps/resume/"; public static final String httpRoot = "http://localhost:8080/resume/img/"; public static final String httphtmlRoot = "http://localhost:8080/resume/"; @RequestMapping(value = "/upload", method = RequestMethod.GET) public String upload() { return "/fileupload"; } @RequestMapping(value = "/uploadImg", method = RequestMethod.POST) @ResponseBody public int uploadImg(@RequestParam("file") MultipartFile file, @RequestParam(value = "id") Integer id, @RequestParam(value = "type") Integer type) { // 照片类型 1:display 2:certificate 3:comment 4:service String folder = ""; int dot = file.getOriginalFilename().lastIndexOf('.'); String newName = file.getOriginalFilename().substring(dot); String path = ROOT + getpathtype(type) + "Img"; Photo photo = new Photo(); if (file.isEmpty()) { return 0; } Date date = new Date(); DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); File dest = new File(path + "/" + getpathtype(type) + id + type + sdf.format(date) + newName); try { file.transferTo(dest); //保存文件 photo.setServant_code(id); photo.setPhoto_type(type); photo.setPhoto_name(getpathtype(type) + id + type + sdf.format(date) + newName); photo.setPhoto_path(httpRoot+ getpathtype(type) + "Img/"+dest.getName()); photoService.addPhoto(photo); return 1; } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } } @PostMapping("/deletePhoto") public int deletePhoto(@ModelAttribute Photo photo) throws ParseException { try { String path = ROOT+ getpathtype(photo.getPhoto_type()) +"Img/"+ photo.getPhoto_name(); File dest = new File(path); dest.delete(); return photoService.deletePhoto(photo); } catch (Exception ex){ ex.printStackTrace(); return 0; } } @PostMapping("/queryPhoto") public String queryPhoto(@ModelAttribute Photo photo) throws ParseException { List photos= photoService.queryPhoto(photo); String jso = JSONObject.toJSONString(photos); jso = jso.replaceAll("photo_path","url"); jso = jso.replaceAll("servant_code","name"); return jso; } @PostMapping("/createResume") public String createResume(@ModelAttribute Photo photo) { try { JlServiceUser jlServiceUser = jlServiceUserService.getResume(photo.getServant_code()); File file = ResourceUtils.getFile("classpath:resume/resume.html"); BufferedReader br = new BufferedReader(new FileReader(file)); Resume resume =new Resume(); Resume resumeold =new Resume(); StringBuilder stringHtml = new StringBuilder(); String str = ""; String photoDisplay =""; String certificate =""; String certificatehref =""; String service =""; String servicehref =""; String comment =""; if(photo.getComment() == null){ photo.setComment(" "); } List photos = photoService.queryPhoto(photo); if(photos.size() > 0 ) { int stepcertificate = 0; int stepservice = 0; for(Photo onePhoto:photos){ if(onePhoto.getPhoto_type() == 1){ if(onePhoto.getPhoto_path() !=null){ photoDisplay = ""+"\r\n"; } } if(onePhoto.getPhoto_type() == 2){ if(onePhoto.getPhoto_path() !=null){ certificate = certificate +"
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n"; if(stepcertificate == 0) { certificatehref = certificatehref + "" + "\r\n"; } else { certificatehref = certificatehref + "" + "\r\n"; } stepcertificate ++ ; } } if(onePhoto.getPhoto_type() == 4){ if(onePhoto.getPhoto_path() !=null) { service = service+"
  • "+"\r\n" + ""+"\r\n" +"
  • "+"\r\n"; if(stepservice == 0) { servicehref = servicehref + " " + "\r\n"; }else { servicehref = servicehref + " " + "\r\n"; } stepservice++; } } if(onePhoto.getPhoto_type() == 3){ comment = comment +""+"\r\n"; } } } if(photoDisplay == ""){ photoDisplay = ""+"\r\n"; } if(certificate == ""){ certificate = "
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n" +"
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n" +"
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n"; certificatehref = ""+"\r\n" +""+"\r\n" +""+"\r\n"; } if(service == ""){ service = "
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n" +"
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n" +"
  • "+"\r\n" +""+"\r\n" +"
  • "+"\r\n"; servicehref = " "+"\r\n" +""+"\r\n" +""+"\r\n"; } if(comment == ""){ comment = ""+"\r\n" +""+"\r\n" +""+"\r\n"; } while((str = br.readLine())!=null){ str = str.replace("$name",jlServiceUser.getTruename()); str = str.replace("$np",jlServiceUser.getNp()+"人"); str = str.replace("$age",jlServiceUser.getAge()); str = str.replace("$ethnicgroup",jlServiceUser.getEthnicgroup()); str = str.replace("$constellation",jlServiceUser.getConstellation()); str = str.replace("$seniority",jlServiceUser.getSeniority()); str = str.replace("$babynumber",jlServiceUser.getBabynumber()); str = str.replace("$education",jlServiceUser.getEducation()); str = str.replace("$weight",jlServiceUser.getWeight()); str = str.replace("$marriage",jlServiceUser.getMarriage()); str = str.replace("$highlight",jlServiceUser.getHighlight()); str = str.replace("$resumecomment",photo.getComment()); str = str.replace("$zodiac",jlServiceUser.getZodiac()); str = str.replace("$height",jlServiceUser.getHeight()); str = str.replace("$starts",jlServiceUser.getGender()); str = str.replace("$display",photoDisplay); str = str.replace("$credentials",certificate); str = str.replace("$cfstyle",certificatehref); str = str.replace("$service",service); str = str.replace("$scstyle",servicehref); str = str.replace("$comment",comment); stringHtml.append(str+"\r\n"); } resumeold = resumeService.getResume(photo.getServant_code()); if(resumeold != null) { resumeService.deleteResume(resumeold.getId()); } resume.setServant_code(photo.getServant_code()); resume.setResume_comment(photo.getComment()); resume.setResume_path(httphtmlRoot+photo.getServant_code() + ".html"); resumeService.addResume(resume); File newResume = new File(ResumeFile + photo.getServant_code() + ".html"); FileWriter fileWriter = new FileWriter(newResume); fileWriter.write(stringHtml.toString()); fileWriter.close(); } catch (Exception ex){ ex.printStackTrace(); return "error"; } return httphtmlRoot+photo.getServant_code() + ".html"; } public String getpathtype(int type){ switch (type) { case 1: return "display"; case 2: return "certificate"; case 3: return "comment"; case 4: return "service"; } return "error"; } }