ImageUploadController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.ygj.yuemum.controller.admin;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ygj.yuemum.domain.admin.Photo;
  4. import com.ygj.yuemum.domain.admin.Resume;
  5. import com.ygj.yuemum.domain.maternitymatron.JlServiceUser;
  6. import com.ygj.yuemum.service.admin.PhotoService;
  7. import com.ygj.yuemum.service.admin.ResumeService;
  8. import com.ygj.yuemum.service.maternitymatron.JlServiceUserService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.core.io.ResourceLoader;
  11. import org.springframework.util.ResourceUtils;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import java.io.*;
  15. import java.text.DateFormat;
  16. import java.text.ParseException;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. import java.util.List;
  20. @RestController
  21. public class ImageUploadController {
  22. @Autowired
  23. private PhotoService photoService;
  24. @Autowired
  25. private JlServiceUserService jlServiceUserService;
  26. @Autowired
  27. private ResumeService resumeService;
  28. private ResourceLoader resourceLoader;
  29. public static final String ROOT = "D:/apache-tomcat-9.0.6/webapps/resume/img/";
  30. public static final String ResumeFile = "D:/apache-tomcat-9.0.6/webapps/resume/";
  31. public static final String httpRoot = "http://localhost:8080/resume/img/";
  32. public static final String httphtmlRoot = "http://localhost:8080/resume/";
  33. @RequestMapping(value = "/upload", method = RequestMethod.GET)
  34. public String upload() {
  35. return "/fileupload";
  36. }
  37. @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
  38. @ResponseBody
  39. public int uploadImg(@RequestParam("file") MultipartFile file,
  40. @RequestParam(value = "id") Integer id,
  41. @RequestParam(value = "type") Integer type) {
  42. // 照片类型 1:display 2:certificate 3:comment 4:service
  43. String folder = "";
  44. int dot = file.getOriginalFilename().lastIndexOf('.');
  45. String newName = file.getOriginalFilename().substring(dot);
  46. String path = ROOT + getpathtype(type) + "Img";
  47. Photo photo = new Photo();
  48. if (file.isEmpty()) {
  49. return 0;
  50. }
  51. Date date = new Date();
  52. DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  53. File dest = new File(path + "/" + getpathtype(type) + id + type + sdf.format(date) + newName);
  54. try {
  55. file.transferTo(dest); //保存文件
  56. photo.setServant_code(id);
  57. photo.setPhoto_type(type);
  58. photo.setPhoto_name(getpathtype(type) + id + type + sdf.format(date) + newName);
  59. photo.setPhoto_path(httpRoot+ getpathtype(type) + "Img/"+dest.getName());
  60. photoService.addPhoto(photo);
  61. return 1;
  62. } catch (IllegalStateException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. return 0;
  66. } catch (IOException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. return 0;
  70. }
  71. }
  72. @PostMapping("/deletePhoto")
  73. public int deletePhoto(@ModelAttribute Photo photo) throws ParseException {
  74. try {
  75. String path = ROOT+ getpathtype(photo.getPhoto_type()) +"Img/"+ photo.getPhoto_name();
  76. File dest = new File(path);
  77. dest.delete();
  78. return photoService.deletePhoto(photo);
  79. } catch (Exception ex){
  80. ex.printStackTrace();
  81. return 0;
  82. }
  83. }
  84. @PostMapping("/queryPhoto")
  85. public String queryPhoto(@ModelAttribute Photo photo) throws ParseException {
  86. List<Photo> photos= photoService.queryPhoto(photo);
  87. String jso = JSONObject.toJSONString(photos);
  88. jso = jso.replaceAll("photo_path","url");
  89. jso = jso.replaceAll("servant_code","name");
  90. return jso;
  91. }
  92. @PostMapping("/createResume")
  93. public String createResume(@ModelAttribute Photo photo) {
  94. try {
  95. JlServiceUser jlServiceUser = jlServiceUserService.getResume(photo.getServant_code());
  96. File file = ResourceUtils.getFile("classpath:resume/resume.html");
  97. BufferedReader br = new BufferedReader(new FileReader(file));
  98. Resume resume =new Resume();
  99. Resume resumeold =new Resume();
  100. StringBuilder stringHtml = new StringBuilder();
  101. String str = "";
  102. String photoDisplay ="";
  103. String certificate ="";
  104. String certificatehref ="";
  105. String service ="";
  106. String servicehref ="";
  107. String comment ="";
  108. if(photo.getComment() == null){
  109. photo.setComment(" ");
  110. }
  111. List<Photo> photos = photoService.queryPhoto(photo);
  112. if(photos.size() > 0 ) {
  113. int stepcertificate = 0;
  114. int stepservice = 0;
  115. for(Photo onePhoto:photos){
  116. if(onePhoto.getPhoto_type() == 1){
  117. if(onePhoto.getPhoto_path() !=null){
  118. photoDisplay = "<image class=\"resume-header-left-image\" src='"+onePhoto.getPhoto_path()+"'></image>"+"\r\n";
  119. }
  120. }
  121. if(onePhoto.getPhoto_type() == 2){
  122. if(onePhoto.getPhoto_path() !=null){
  123. certificate = certificate +"<li class=\"poster-item zturn-item\">"+"\r\n"
  124. +"<image class=\"zturn-item-image-cer\" src='"+onePhoto.getPhoto_path()+"'></image>"+"\r\n"
  125. +"</li>"+"\r\n";
  126. if(stepcertificate == 0) {
  127. certificatehref = certificatehref + "<a href=\"javascript:void(0);\" value='"+stepcertificate+"' class=\"indicator-list-item selected\"></a>" + "\r\n";
  128. } else {
  129. certificatehref = certificatehref + "<a href=\"javascript:void(0);\" value='"+stepcertificate+"' class=\"indicator-list-item \"></a>" + "\r\n";
  130. }
  131. stepcertificate ++ ;
  132. }
  133. }
  134. if(onePhoto.getPhoto_type() == 4){
  135. if(onePhoto.getPhoto_path() !=null) {
  136. service = service+"<li class=\"poster-item zturn-item\">"+"\r\n"
  137. + "<image class=\"zturn-item-image\" src='"+onePhoto.getPhoto_path()+"'></image>"+"\r\n"
  138. +"</li>"+"\r\n";
  139. if(stepservice == 0) {
  140. servicehref = servicehref + " <a href=\"javascript:void(0);\" value='"+stepservice+"' class=\"indicator-list-item-service selected\"></a>" + "\r\n";
  141. }else {
  142. servicehref = servicehref + " <a href=\"javascript:void(0);\" value='"+stepservice+"' class=\"indicator-list-item-service\"></a>" + "\r\n";
  143. }
  144. stepservice++;
  145. }
  146. }
  147. if(onePhoto.getPhoto_type() == 3){
  148. comment = comment +"<a href=\"\"><img src='"+onePhoto.getPhoto_path()+"'/></a>"+"\r\n";
  149. }
  150. }
  151. }
  152. if(photoDisplay == ""){
  153. photoDisplay = "<image class=\"resume-header-left-image\" src=\"img/header.jpg\"></image>"+"\r\n";
  154. }
  155. if(certificate == ""){
  156. certificate = "<li class=\"poster-item zturn-item\">"+"\r\n"
  157. +"<image class=\"zturn-item-image-cer\" src=\"img/certificate01.jpg\"></image>"+"\r\n"
  158. +"</li>"+"\r\n"
  159. +"<li class=\"poster-item zturn-item\">"+"\r\n"
  160. +"<image class=\"zturn-item-image-cer\" src=\"img/certificate02.jpg\"></image>"+"\r\n"
  161. +"</li>"+"\r\n"
  162. +"<li class=\"poster-item zturn-item\">"+"\r\n"
  163. +"<image class=\"zturn-item-image-cer\" src=\"img/certificate03.jpg\"></image>"+"\r\n"
  164. +"</li>"+"\r\n";
  165. certificatehref = "<a href=\"javascript:void(0);\" value=\"0\" class=\"indicator-list-item selected\"></a>"+"\r\n"
  166. +"<a href=\"javascript:void(0);\" value=\"1\" class=\"indicator-list-item\"></a>"+"\r\n"
  167. +"<a href=\"javascript:void(0);\" value=\"2\" class=\"indicator-list-item\"></a>"+"\r\n";
  168. }
  169. if(service == ""){
  170. service = "<li class=\"poster-item zturn-item\">"+"\r\n"
  171. +"<image class=\"zturn-item-image\" src=\"img/service01.png\"></image>"+"\r\n"
  172. +"</li>"+"\r\n"
  173. +"<li class=\"poster-item zturn-item\">"+"\r\n"
  174. +"<image class=\"zturn-item-image\" src=\"img/service01.png\"></image>"+"\r\n"
  175. +"</li>"+"\r\n"
  176. +"<li class=\"poster-item zturn-item\">"+"\r\n"
  177. +"<image class=\"zturn-item-image\" src=\"img/service01.png\"></image>"+"\r\n"
  178. +"</li>"+"\r\n";
  179. servicehref = " <a href=\"javascript:void(0);\" value=\"0\" class=\"indicator-list-item-service selected\"></a>"+"\r\n"
  180. +"<a href=\"javascript:void(0);\" value=\"1\" class=\"indicator-list-item-service\"></a>"+"\r\n"
  181. +"<a href=\"javascript:void(0);\" value=\"2\" class=\"indicator-list-item-service\"></a>"+"\r\n";
  182. }
  183. if(comment == ""){
  184. comment = "<a href=\"\"><img src=\"img/comments01.jpg\"/></a>"+"\r\n"
  185. +"<a href=\"\"><img src=\"img/comments02.jpg\"/></a>"+"\r\n"
  186. +"<a href=\"\"><img src=\"img/comments03.jpg\"/></a>"+"\r\n";
  187. }
  188. while((str = br.readLine())!=null){
  189. str = str.replace("$name",jlServiceUser.getTruename());
  190. str = str.replace("$np",jlServiceUser.getNp()+"人");
  191. str = str.replace("$age",jlServiceUser.getAge());
  192. str = str.replace("$ethnicgroup",jlServiceUser.getEthnicgroup());
  193. str = str.replace("$constellation",jlServiceUser.getConstellation());
  194. str = str.replace("$seniority",jlServiceUser.getSeniority());
  195. str = str.replace("$babynumber",jlServiceUser.getBabynumber());
  196. str = str.replace("$education",jlServiceUser.getEducation());
  197. str = str.replace("$weight",jlServiceUser.getWeight());
  198. str = str.replace("$marriage",jlServiceUser.getMarriage());
  199. str = str.replace("$highlight",jlServiceUser.getHighlight());
  200. str = str.replace("$resumecomment",photo.getComment());
  201. str = str.replace("$zodiac",jlServiceUser.getZodiac());
  202. str = str.replace("$height",jlServiceUser.getHeight());
  203. str = str.replace("$starts",jlServiceUser.getGender());
  204. str = str.replace("$display",photoDisplay);
  205. str = str.replace("$credentials",certificate);
  206. str = str.replace("$cfstyle",certificatehref);
  207. str = str.replace("$service",service);
  208. str = str.replace("$scstyle",servicehref);
  209. str = str.replace("$comment",comment);
  210. stringHtml.append(str+"\r\n");
  211. }
  212. resumeold = resumeService.getResume(photo.getServant_code());
  213. if(resumeold != null) {
  214. resumeService.deleteResume(resumeold.getId());
  215. }
  216. resume.setServant_code(photo.getServant_code());
  217. resume.setResume_comment(photo.getComment());
  218. resume.setResume_path(httphtmlRoot+photo.getServant_code() + ".html");
  219. resumeService.addResume(resume);
  220. File newResume = new File(ResumeFile + photo.getServant_code() + ".html");
  221. FileWriter fileWriter = new FileWriter(newResume);
  222. fileWriter.write(stringHtml.toString());
  223. fileWriter.close();
  224. } catch (Exception ex){
  225. ex.printStackTrace();
  226. return "error";
  227. }
  228. return httphtmlRoot+photo.getServant_code() + ".html";
  229. }
  230. public String getpathtype(int type){
  231. switch (type) {
  232. case 1:
  233. return "display";
  234. case 2:
  235. return "certificate";
  236. case 3:
  237. return "comment";
  238. case 4:
  239. return "service";
  240. }
  241. return "error";
  242. }
  243. }