From 188601ec04cee3beadabfd2a2e957b76c7ce5e62 Mon Sep 17 00:00:00 2001 From: whycrzg <ruanzhigang@whycst.com> Date: 星期二, 30 三月 2021 11:42:15 +0800 Subject: [PATCH] update 项目管理/文件上传 --- src/main/java/com/whyc/controller/ProjectManageController.java | 76 ++++++++++++++++++++++++++++++++++--- 1 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/whyc/controller/ProjectManageController.java b/src/main/java/com/whyc/controller/ProjectManageController.java index c60e151..5937621 100644 --- a/src/main/java/com/whyc/controller/ProjectManageController.java +++ b/src/main/java/com/whyc/controller/ProjectManageController.java @@ -13,8 +13,10 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.io.File; +import javax.servlet.http.HttpServletResponse; +import java.io.*; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -84,38 +86,98 @@ /** * 鏂囦欢瀛樺偍鍚庡湴鍧�鏇存柊鍒板搴旈」鐩紪鍙风殑鍦板潃锛屼竴涓」鐩搴斿涓枃浠� db_experiment.tb_project_archive_manage * 鏄惁闇�瑕佸皢word杞琾df/棰勮鏂囦欢pdf + * * @param file * @return */ @PostMapping("upload") - @ApiOperation(notes = "杩斿洖鏂囦欢鍦板潃锛岀‘璁ゆ椂鎻愪氦淇濆瓨鍒板瓧娈�",value = "鏂囨。鏉愭枡娣诲姞(閫夋嫨鏂囦欢)") + @ApiOperation(notes = "杩斿洖鏂囦欢鍦板潃锛岀‘璁ゆ椂鎻愪氦淇濆瓨鍒板瓧娈�", value = "鏂囨。鏉愭枡娣诲姞(閫夋嫨鏂囦欢)") public Response upload(@RequestParam("file") MultipartFile file) { // 鑾峰彇鍘熷鍚嶅瓧 - String fileName = file.getOriginalFilename(); + String originalFilename = file.getOriginalFilename(); // 鑾峰彇鍚庣紑鍚� // String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 鏂囦欢淇濆瓨璺緞銆佸悗鏈熻�冭檻鎸夋棩鏈熸垨鑰呮湀浠藉缓dir String filePath = "c:/upload/"; // 鏂囦欢閲嶅懡鍚嶏紝闃叉閲嶅,棰勮鏃堕渶瑕佹樉绀哄師鏂囦欢鍚� _ 鍒囧壊 - fileName = filePath + UUID.randomUUID() + "_" + fileName; + String fileName = filePath + UUID.randomUUID() + "_" + originalFilename; // 鏂囦欢瀵硅薄 File dest = new File(fileName); // 鍒ゆ柇璺緞鏄惁瀛樺湪锛屽鏋滀笉瀛樺湪鍒欏垱寤� if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } + Response<Object> response = new Response<>(); + HashMap<Object, Object> map = new HashMap<>(); try { // 淇濆瓨鍒版湇鍔″櫒涓� file.transferTo(dest); + map.put("鏂囦欢绫诲瀷锛�", file.getContentType()); + map.put("鍘熸枃浠跺悕锛�", originalFilename); + map.put("鏄惁涓虹┖锛�", file.isEmpty()); + Double size = file.getSize() * 0.01 / 1024 / 1024 * 100; + String douStr = String.format("%.2f", size); + map.put("鏂囦欢澶у皬锛�", douStr+" M"); + map.put("鏂囦欢鍦板潃锛�",fileName); } catch (Exception e) { e.printStackTrace(); - return new Response().setCode(0); + return response.setMsg(0, "涓婁紶澶辫触"); } - Response<Object> response = new Response<>(); - response.setMsg(1, fileName); + response.setCode(1); + response.setData(map); return response; } + @GetMapping("/download") + @ApiOperation(notes = "闇�瑕佸湪鍦板潃鏍忔祴璇�",value = "鏂囨。鏉愭枡涓嬭浇") + public Object download(HttpServletResponse response, @RequestParam String filePath,@RequestParam String fileName) { + Response<Object> result = new Response<>(); + File file = new File( filePath); + if (file.exists()) { + try { + fileName= new String(fileName.getBytes("gbk"), "ISO8859-1"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + response.setContentType("application/force-download"); + // 璁剧疆寮哄埗涓嬭浇涓嶆墦寮� + response.addHeader("Content-Disposition", "attachment;fileName=" + fileName); + byte[] buffer = new byte[1024]; + FileInputStream fis = null; + BufferedInputStream bis = null; + try { + fis = new FileInputStream(file); + bis = new BufferedInputStream(fis); + OutputStream outputStream = response.getOutputStream(); + int i = bis.read(buffer); + while (i != -1) { + outputStream.write(buffer, 0, i); + i = bis.read(buffer); + } + return result.setMsg(1,"涓嬭浇鎴愬姛"); + } catch (Exception e) { + e.printStackTrace(); + return result.setMsg(0,"涓嬭浇澶辫触"); + } finally { + if (bis != null) { + try { + bis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + return result.setMsg(0,"鏂囦欢涓嶅瓨鍦�"); + } + @GetMapping("managesState") @ApiOperation(notes = "", value = "椤圭洰绠$悊-宸辩‘璁�/鏈‘璁ら樁娈垫煡璇�") -- Gitblit v1.9.1