From f9acca94bd913c3e155dc86245df372910f8d76e Mon Sep 17 00:00:00 2001 From: zxl <763096477@qq.com> Date: 星期二, 09 九月 2025 14:46:39 +0800 Subject: [PATCH] 视频下载问题以及文档 --- src/main/java/com/mindskip/xzs/controller/common/UploadController.java | 75 +++++++++++++++++++++++++++++++++---- 1 files changed, 66 insertions(+), 9 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 4c1f474..406c700 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,19 @@ 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 java.io.File; -import java.io.IOException; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.UUID; @@ -32,10 +37,10 @@ @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 originalFileName = StringUtils.cleanPath(file.getOriginalFilename()); @@ -63,4 +68,56 @@ } } + /** + * 涓嬭浇鏂囦欢锛堝崟涓級 + */ + @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("鏂囦欢涓嶅瓨鍦�"); + } + + String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); + response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName); + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + response.setContentLengthLong(file.length()); // 璁剧疆鏂囦欢澶у皬锛屼究浜庢祻瑙堝櫒鏄剧ず杩涘害 + + + // 娴佸紡浼犺緭锛氫娇鐢ㄧ紦鍐叉祦鍒嗗潡璇诲啓锛岄伩鍏嶄竴娆℃�у姞杞芥暣涓枃浠跺埌鍐呭瓨 + try (InputStream in = new BufferedInputStream(new FileInputStream(file)); + OutputStream out = new BufferedOutputStream(response.getOutputStream())) { + + byte[] buffer = new byte[8192]; // 8KB缂撳啿鍖� + int len; + // 寰幆璇诲彇鏂囦欢鍐呭锛屾瘡娆¤8KB锛屽啓鍏ュ搷搴旇緭鍑烘祦 + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + out.flush(); // 纭繚鏈�鍚庝竴閮ㄥ垎鏁版嵁琚啓鍑� + } catch (IOException e) { + throw new RuntimeException("鏂囦欢涓嬭浇澶辫触", e); + } + + +// // 璇诲彇鏂囦欢鍐呭 +// 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