package com.highdatas.srs.web; import com.alibaba.fastjson.JSONObject; import com.highdatas.srs.entity.ProjectDeal; import com.highdatas.srs.entity.Scheme; import com.highdatas.srs.entity.SchemeDetail; import com.highdatas.srs.entity.Topic; import com.highdatas.srs.pojo.CodeMsg; import com.highdatas.srs.pojo.Result; import com.highdatas.srs.service.ISchemeDetailService; import com.highdatas.srs.service.ISchemeService; import com.highdatas.srs.service.ITopicService; import com.highdatas.srs.util.DbUtils; import com.highdatas.srs.util.FileUtils; import lombok.extern.java.Log; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author kimi * @description * @date 2020-01-17 12:05 */ @RestController @Log @RequestMapping("file") public class FileController { @Autowired ISchemeDetailService schemeDetailService; @Autowired ISchemeService schemeService; @Autowired ITopicService topicService; @Value("${base.file.path}") String basePath; @Value("${base.topic.path}") String topicPath; @RequestMapping(value = "child", method = RequestMethod.GET) public Result children(@RequestParam String path) throws UnsupportedEncodingException { path = URLDecoder.decode(path, "UTF-8"); ArrayList childrenFiles = FileUtils.getChildrenFiles(path); return Result.success(childrenFiles); } @RequestMapping(value = "downloadPath", method = RequestMethod.GET) public Result downloadPath(@RequestParam String path, HttpServletResponse response) throws UnsupportedEncodingException { path = URLDecoder.decode(path, "UTF-8"); File file = new File(path); if(file.exists()){ //判断文件父目录是否存在 response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); // response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(file.getName(),"UTF-8")); byte[] buffer = new byte[1024]; FileInputStream fis = null; //文件输入流 BufferedInputStream bis = null; OutputStream os = null; //输出流 try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int i = bis.read(buffer); while(i != -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("----------file download---" + file.getName()); try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }} return null; } @RequestMapping("{detailId}/download/{fileName}") public Result downLoad(@PathVariable String detailId, @PathVariable String fileName, HttpServletResponse response) throws UnsupportedEncodingException { SchemeDetail detail = schemeDetailService.selectById(detailId); if (detail == null) { return Result.error(CodeMsg.SELECT_ERROR); } String parentId = detail.getParentId(); Scheme scheme = schemeService.selectById(parentId); if (scheme == null) { return Result.error(CodeMsg.SELECT_ERROR); } String path = FileUtils.createFile(basePath, scheme.getName(), detail.getType().covert()); File file = new File(path); if(file.exists()){ //判断文件父目录是否存在 response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); // response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8")); byte[] buffer = new byte[1024]; FileInputStream fis = null; //文件输入流 BufferedInputStream bis = null; OutputStream os = null; //输出流 try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int i = bis.read(buffer); while(i != -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("----------file download---" + fileName); try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } @RequestMapping("/download/{topicId}") public Result downLoadTopic(@PathVariable String topicId, HttpServletResponse response) throws UnsupportedEncodingException { Topic topic = topicService.selectById(topicId); String path = topic.getAttachment(); File file = new File(path); if(file.exists()){ //判断文件父目录是否存在 response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); // response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(file.getName(),"UTF-8")); byte[] buffer = new byte[1024]; FileInputStream fis = null; //文件输入流 BufferedInputStream bis = null; OutputStream os = null; //输出流 try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int i = bis.read(buffer); while(i != -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("----------file download---" + file.getName()); try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } @RequestMapping("/downloadPath/{path}") public Result downLoadPath(@PathVariable String path, HttpServletResponse response) throws UnsupportedEncodingException { path = URLDecoder.decode(path, "UTF-8"); File file = new File(path); if(file.exists()){ //判断文件父目录是否存在 response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); // response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(file.getName(),"UTF-8")); byte[] buffer = new byte[1024]; FileInputStream fis = null; //文件输入流 BufferedInputStream bis = null; OutputStream os = null; //输出流 try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int i = bis.read(buffer); while(i != -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("----------file download---" + file.getName()); try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } /** * 实现文件上传 * */ @RequestMapping("fileUpload/{detailId}") @ResponseBody public Result fileUpload(@PathVariable String detailId, @RequestParam("file") MultipartFile file, HttpServletRequest request){ String desp = request.getParameter("desp"); SchemeDetail detail = schemeDetailService.selectById(detailId); if (detail == null) { return Result.error(CodeMsg.SELECT_ERROR); } if (!detail.getMustAttach()) { ProjectDeal projectDeal = new ProjectDeal(); projectDeal.setCreateTime(new Date()).setSchemeId(detail.getParentId()) .setSchemeDetailId(detailId).setDesp(desp) .setId(DbUtils.getUUID()).insert(); detail.setFinish(true); detail.setUpdateTime(new Date()); detail.setFinishTime(new Date()); detail.updateById(); return Result.success(CodeMsg.SUCCESS); } if(file.isEmpty()){ return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); } String parentId = detail.getParentId(); Scheme scheme = schemeService.selectById(parentId); if (scheme == null) { return Result.error(CodeMsg.SELECT_ERROR); } String fileName = file.getOriginalFilename(); int size = (int) file.getSize(); log.info(fileName + "-->" + size); String path = FileUtils.createFile(basePath, scheme.getName(), detail.getType().covert()); File dest = new File(path + "/" + fileName); if(!dest.getParentFile().exists()){ //判断文件父目录是否存在 dest.getParentFile().mkdirs(); } dest.deleteOnExit(); try { dest.createNewFile(); file.transferTo(dest); //保存文件 ProjectDeal projectDeal = new ProjectDeal(); projectDeal.setCreateTime(new Date()).setSchemeId(detail.getParentId()) .setAttachment(path + "/" + fileName) .setSchemeDetailId(detailId).setDesp(desp) .setId(DbUtils.getUUID()).insert(); ProjectDeal projectDeal2 = new ProjectDeal(); projectDeal2.setCreateTime(new Date()).setSchemeId(detail.getParentId()) .setSchemeDetailId(detailId).setDesp("处理任务:" + detail.getType().covert()) .setId(DbUtils.getUUID()).insert(); detail.setFinish(true); detail.setAttachment(path + "/" + fileName); detail.setUpdateTime(new Date()); detail.setFinishTime(new Date()); detail.updateById(); return Result.success(CodeMsg.SUCCESS); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return Result.error(CodeMsg.OPERATR_ERROR); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return Result.error(CodeMsg.OPERATR_ERROR); } } @RequestMapping("topicUpload") @ResponseBody public Result topicUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request){ String id = request.getParameter("id"); String title = request.getParameter("title"); String desp = request.getParameter("desp"); String userId = request.getParameter("userId"); Topic topic; if (StringUtils.isEmpty(id)) { topic = new Topic().setId(DbUtils.getUUID()).setTitle(title).setDesp(desp).setUserId(userId).setCreateTime(new Date()); } else { topic = topicService.selectById(id).setTitle(title).setDesp(desp); } String fileName = file.getOriginalFilename(); int size = (int) file.getSize(); log.info(fileName + "-->" + size); String path = topicPath + "/"+ topic.getId() + fileName; File dest = new File(path); if(!dest.getParentFile().exists()){ //判断文件父目录是否存在 dest.getParentFile().mkdirs(); } dest.deleteOnExit(); try { dest.createNewFile(); file.transferTo(dest); //保存文件 topic.setAttachment(path); topic.insertOrUpdate(); return Result.success(CodeMsg.SUCCESS); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return Result.error(CodeMsg.OPERATR_ERROR); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return Result.error(CodeMsg.OPERATR_ERROR); } } /** * 实现多文件上传 * */ @RequestMapping(value="multifileUpload",method= RequestMethod.POST) public @ResponseBody String multifileUpload(HttpServletRequest request){ List files = ((MultipartHttpServletRequest)request).getFiles("fileName"); if(files.isEmpty()){ return "false"; } String path = "F:/test" ; for(MultipartFile file:files){ String fileName = file.getOriginalFilename(); int size = (int) file.getSize(); log.info(fileName + "-->" + size); if(file.isEmpty()){ return "false"; }else{ File dest = new File(path + "/" + fileName); if(!dest.getParentFile().exists()){ //判断文件父目录是否存在 dest.getParentFile().mkdir(); } try { file.transferTo(dest); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "false"; } } } return "true"; } }