package com.fuzamei.web; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.security.MessageDigest; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.fuzamei.service.AccountOpenService; import com.fuzamei.service.PromptLetterService; import com.fuzamei.theromus.utils.HexUtils; import com.fuzamei.utils.HashCodeUtil; import com.fuzamei.utils.HashXiZhiUtil; import com.fuzamei.utils.JSONUtil; import com.fuzamei.utils.PageDTO; /** * 文件上传功能 * @param file * @return * @throws IOException */ @Controller @RequestMapping(value = "/PromptLetter") public class PromptLetterAction { @Autowired private PromptLetterService promptLetterService;//注入service /** * 风险提示函发函 上传 * @param file * @param request * @param prompt_letter_id * @return * @throws IOException */ @RequestMapping(value="/uploadFile",method=RequestMethod.POST) @ResponseBody public Map upload(@RequestParam("file") MultipartFile file,HttpServletRequest request,@RequestParam("prompt_letter_id") String prompt_letter_id) throws IOException{ try { String path = "D:\\tomcate\\apache-tomcat-8.5.24\\webapps"+request.getServletContext().getContextPath()+"\\Content"; File dir = new File(path); if(!dir.exists()){ dir.mkdirs(); } String fileName = file.getOriginalFilename(); String pathFile=path+"/"+fileName; File newfile = new File(pathFile); file.transferTo(newfile); String hash = HashXiZhiUtil.getMD5Checksum(pathFile);//上传(文件)之后产生哈希值 是对文件产生哈希值 System.out.println(hash+"上传提示函的哈希值是"); //MultipartFile自带的解析方法 Map map = new LinkedHashMap(); map.put("prompt_letter_id",prompt_letter_id);//有问题 待改动 map.put("hash", hash);//上传文件后产生的哈希值插入到提示函表 //map.put("fileName", fileName); map.put("url","/Content/"+fileName); map.put("attachmentName", fileName); promptLetterService.insertTiShiHanOrUserOrOtherTable(map);//在插入数据库 Map mapResult=JSONUtil.getJsonMap(200, true, "上传提示函成功", null); return mapResult; } catch (Exception e) { Map mapResult=JSONUtil.getJsonMap(500, true, "上传提示函失败"+e.getMessage(), null); return mapResult; } } /** * 查询提示函 管理人接收列表 * @param data * @return */ @RequestMapping(value = "/selectPromptLetter", method = RequestMethod.POST) @ResponseBody public Map selectPromptLetter(@RequestBody String data) { //System.out.println("管理人查询风险提示函信息列表"); Map mapResult = new LinkedHashMap(); Map map = JSONUtil.jsonToMap(data); PageDTO pageDto= promptLetterService.selectPromptLetter(map); mapResult = JSONUtil.getJsonMap(200, true, "操作成功", pageDto); return mapResult; } /** * 查询提示函 发送人的列表 * @param data * @return */ @RequestMapping(value = "/selectTishihan", method = RequestMethod.POST) @ResponseBody public Map selectTishihan(@RequestBody String data) { //System.out.println("查询风险提示函信息列表3 ,省分行托管中心风管岗"); Map mapResult = new LinkedHashMap(); Map map = JSONUtil.jsonToMap(data); PageDTO pageDto= promptLetterService.selectTishihan(map); mapResult = JSONUtil.getJsonMap(200, true, "操作成功", pageDto); return mapResult; } /** * 提示函下载 doc文档 * @param request * @param response * @throws Exception */ @RequestMapping(value="/downloadFile") @ResponseBody public Map downloadFile(HttpServletRequest request,HttpServletResponse response,@RequestParam("url") String url) throws Exception{ try { //模拟文件,456.doc为需要下载的文件 String fileName = request.getSession().getServletContext().getRealPath("")+url; //获取输入流 InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); //截取文件名字 String filename = url.substring(url.lastIndexOf("/")+1); //转码,免得文件名中文乱码 filename = URLEncoder.encode(filename,"UTF-8"); //设置文件下载头 response.addHeader("Content-Disposition", "attachment;filename=" + filename); //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("multipart/form-data"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); int len = 0; while((len = bis.read()) != -1){ out.write(len); out.flush(); } out.close(); //返给前端map进行前端提示 return JSONUtil.getJsonMap(200, true, "下载成功",null); } catch (Exception e) { return JSONUtil.getJsonMap(500, false, "下载失败:"+e.getMessage(),null); } } }