PromptLetterAction.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.fuzamei.web;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URLEncoder;
  9. import java.security.MessageDigest;
  10. import java.util.LinkedHashMap;
  11. import java.util.Map;
  12. import javax.servlet.ServletOutputStream;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.util.DigestUtils;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.ResponseBody;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import com.fuzamei.service.AccountOpenService;
  25. import com.fuzamei.service.PromptLetterService;
  26. import com.fuzamei.theromus.utils.HexUtils;
  27. import com.fuzamei.utils.HashCodeUtil;
  28. import com.fuzamei.utils.HashXiZhiUtil;
  29. import com.fuzamei.utils.JSONUtil;
  30. import com.fuzamei.utils.PageDTO;
  31. /**
  32. * 文件上传功能
  33. * @param file
  34. * @return
  35. * @throws IOException
  36. */
  37. @Controller
  38. @RequestMapping(value = "/PromptLetter")
  39. public class PromptLetterAction {
  40. @Autowired
  41. private PromptLetterService promptLetterService;//注入service
  42. /**
  43. * 风险提示函发函 上传
  44. * @param file
  45. * @param request
  46. * @param prompt_letter_id
  47. * @return
  48. * @throws IOException
  49. */
  50. @RequestMapping(value="/uploadFile",method=RequestMethod.POST)
  51. @ResponseBody
  52. public Map<String,Object> upload(@RequestParam("file") MultipartFile file,HttpServletRequest request,@RequestParam("prompt_letter_id") String prompt_letter_id) throws IOException{
  53. try {
  54. String path = "D:\\tomcate\\apache-tomcat-8.5.24\\webapps"+request.getServletContext().getContextPath()+"\\Content";
  55. File dir = new File(path);
  56. if(!dir.exists()){
  57. dir.mkdirs();
  58. }
  59. String fileName = file.getOriginalFilename();
  60. String pathFile=path+"/"+fileName;
  61. File newfile = new File(pathFile);
  62. file.transferTo(newfile);
  63. String hash = HashXiZhiUtil.getMD5Checksum(pathFile);//上传(文件)之后产生哈希值 是对文件产生哈希值
  64. System.out.println(hash+"上传提示函的哈希值是");
  65. //MultipartFile自带的解析方法
  66. Map<String, Object> map = new LinkedHashMap<String, Object>();
  67. map.put("prompt_letter_id",prompt_letter_id);//有问题 待改动
  68. map.put("hash", hash);//上传文件后产生的哈希值插入到提示函表
  69. //map.put("fileName", fileName);
  70. map.put("url","/Content/"+fileName);
  71. map.put("attachmentName", fileName);
  72. promptLetterService.insertTiShiHanOrUserOrOtherTable(map);//在插入数据库
  73. Map<String, Object> mapResult=JSONUtil.getJsonMap(200, true, "上传提示函成功", null);
  74. return mapResult;
  75. }
  76. catch (Exception e) {
  77. Map<String, Object> mapResult=JSONUtil.getJsonMap(500, true, "上传提示函失败"+e.getMessage(), null);
  78. return mapResult;
  79. }
  80. }
  81. /**
  82. * 查询提示函 管理人接收列表
  83. * @param data
  84. * @return
  85. */
  86. @RequestMapping(value = "/selectPromptLetter", method = RequestMethod.POST)
  87. @ResponseBody
  88. public Map<String, Object> selectPromptLetter(@RequestBody String data) {
  89. //System.out.println("管理人查询风险提示函信息列表");
  90. Map<String, Object> mapResult = new LinkedHashMap<String, Object>();
  91. Map<String, Object> map = JSONUtil.jsonToMap(data);
  92. PageDTO pageDto= promptLetterService.selectPromptLetter(map);
  93. mapResult = JSONUtil.getJsonMap(200, true, "操作成功", pageDto);
  94. return mapResult;
  95. }
  96. /**
  97. * 查询提示函 发送人的列表
  98. * @param data
  99. * @return
  100. */
  101. @RequestMapping(value = "/selectTishihan", method = RequestMethod.POST)
  102. @ResponseBody
  103. public Map<String, Object> selectTishihan(@RequestBody String data) {
  104. //System.out.println("查询风险提示函信息列表3 ,省分行托管中心风管岗");
  105. Map<String, Object> mapResult = new LinkedHashMap<String, Object>();
  106. Map<String, Object> map = JSONUtil.jsonToMap(data);
  107. PageDTO pageDto= promptLetterService.selectTishihan(map);
  108. mapResult = JSONUtil.getJsonMap(200, true, "操作成功", pageDto);
  109. return mapResult;
  110. }
  111. /**
  112. * 提示函下载 doc文档
  113. * @param request
  114. * @param response
  115. * @throws Exception
  116. */
  117. @RequestMapping(value="/downloadFile")
  118. @ResponseBody
  119. public Map<String,Object> downloadFile(HttpServletRequest request,HttpServletResponse response,@RequestParam("url") String url) throws Exception{
  120. try {
  121. //模拟文件,456.doc为需要下载的文件
  122. String fileName = request.getSession().getServletContext().getRealPath("")+url;
  123. //获取输入流
  124. InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
  125. //截取文件名字
  126. String filename = url.substring(url.lastIndexOf("/")+1);
  127. //转码,免得文件名中文乱码
  128. filename = URLEncoder.encode(filename,"UTF-8");
  129. //设置文件下载头
  130. response.addHeader("Content-Disposition", "attachment;filename=" + filename);
  131. //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
  132. response.setContentType("multipart/form-data");
  133. BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
  134. int len = 0;
  135. while((len = bis.read()) != -1){
  136. out.write(len);
  137. out.flush();
  138. }
  139. out.close();
  140. //返给前端map进行前端提示
  141. return JSONUtil.getJsonMap(200, true, "下载成功",null);
  142. } catch (Exception e) {
  143. return JSONUtil.getJsonMap(500, false, "下载失败:"+e.getMessage(),null);
  144. }
  145. }
  146. }