添加直播推流工具下载
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.ruoyi.cms.controller.cms;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/cms/liveSteam")
|
||||
@Api(tags = "后台:直播")
|
||||
public class CmsLiveSteamingController {
|
||||
|
||||
@ApiOperation("下载推流工具")
|
||||
@PreAuthorize("@ss.hasPermi('cms:liveSteam:downloadTools')")
|
||||
@GetMapping("/downloadTools")
|
||||
public void downloadTools(HttpServletRequest request, HttpServletResponse response)throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String name = "app.zip";
|
||||
String pathFile = "/data/downloadmodel/" + name;
|
||||
File url = new File(pathFile);
|
||||
|
||||
try {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
// 文件不存在直接返回JSON,不走二进制下载逻辑
|
||||
if (!url.exists()) {
|
||||
try {
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
mapper.writeValue(response.getWriter(), AjaxResult.error("文件未找到:" + name));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 文件名编码
|
||||
String encodeName = URLEncoder.encode(name, StandardCharsets.UTF_8.name());
|
||||
response.reset();
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + encodeName);
|
||||
response.setHeader("Pragma", "public");
|
||||
response.setHeader("Cache-Control", "max-age=0");
|
||||
|
||||
// 大文件缓冲区4k
|
||||
byte[] buffer = new byte[8192]; // 加大缓冲区,加快传输
|
||||
try (InputStream in = new FileInputStream(url)) {
|
||||
OutputStream out = response.getOutputStream();
|
||||
int len;
|
||||
try {
|
||||
while ((len = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
System.out.println("下载中断:客户端主动断开连接,文件:" + name);
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("文件读取异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user