From 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403 Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期三, 14 五月 2025 15:50:57 +0800 Subject: [PATCH] bug:学员有状态不能修改问题 --- src/main/java/com/mindskip/xzs/controller/common/UploadController.java | 61 +++++++++++++++++++++++++----- 1 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/mindskip/xzs/controller/common/UploadController.java b/src/main/java/com/mindskip/xzs/controller/common/UploadController.java index 2c2ed72..078ef2c 100644 --- a/src/main/java/com/mindskip/xzs/controller/common/UploadController.java +++ b/src/main/java/com/mindskip/xzs/controller/common/UploadController.java @@ -3,14 +3,22 @@ import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.configuration.RuoYiConfig; import lombok.RequiredArgsConstructor; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.UUID; /** * @author锛歺p @@ -30,14 +38,14 @@ @PostMapping("/upload") public RestResponse uploadFile(MultipartFile file) throws Exception { - // 妫�鏌ユ枃浠舵槸鍚︿负绌� - if (file.isEmpty()) { - return RestResponse.fail(500, "涓婁紶鐨勬枃浠朵负绌�"); - } +// // 妫�鏌ユ枃浠舵槸鍚︿负绌� +// if (file.isEmpty()) { +// return RestResponse.fail(500, "涓婁紶鐨勬枃浠朵负绌�"); +// } try { // 鑾峰彇鏂囦欢鍚� - String fileName = StringUtils.cleanPath(file.getOriginalFilename()); - + String originalFileName = StringUtils.cleanPath(file.getOriginalFilename()); + String randomName = UUID.randomUUID().toString().replace("-", "") + originalFileName.substring(originalFileName.lastIndexOf(".")); // 鎸囧畾鏂囦欢瀛樺偍璺緞 String uploadDir = ruoYiConfig.getUrl(); // 淇敼涓烘偍甯屾湜瀛樺偍鐨勭洰褰� // 濡傛灉鐩綍涓嶅瓨鍦紝鍒欏垱寤虹洰褰� @@ -46,11 +54,14 @@ dir.mkdirs(); } // 鏋勫缓鐩爣鏂囦欢鐨勮矾寰� - String filePath = uploadDir + "/" + fileName; + String filePath = uploadDir + "/" + randomName; // 灏嗘枃浠朵繚瀛樺埌鐩爣浣嶇疆 file.transferTo(new File(filePath)); // 杩斿洖鎴愬姛鍝嶅簲 - return RestResponse.ok("鏂囦欢涓婁紶鎴愬姛"); + HashMap hashMap = new HashMap(2); + hashMap.put("name", originalFileName); + hashMap.put("url", randomName); + return RestResponse.ok(hashMap); } catch (IOException e) { e.printStackTrace(); // 杩斿洖澶辫触鍝嶅簲 @@ -58,4 +69,32 @@ } } + /** + * 涓嬭浇鏂囦欢锛堝崟涓級 + */ + @GetMapping("/download") + public void download(@RequestParam String url, @RequestParam String fileName, HttpServletResponse response) throws Exception + { + // 鎻愬彇鏂囦欢璺緞 + String filePath = ruoYiConfig.getUrl() + File.separator + url; + + File file = new File(filePath); + + // 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦� + if (!file.exists()) { + throw new RuntimeException("鏂囦欢涓嶅瓨鍦�"); + } + + // 璇诲彇鏂囦欢鍐呭 + byte[] fileContent = Files.readAllBytes(file.toPath()); + + // 璁剧疆鍝嶅簲澶� + response.setHeader("Content-Disposition", "attachment; filename=" + fileName); + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + + // 灏嗘枃浠跺唴瀹瑰啓鍏ュ搷搴旇緭鍑烘祦 + response.getOutputStream().write(fileContent); + response.getOutputStream().flush(); + } + } -- Gitblit v1.8.0