增加分享次数,优化音乐下载
This commit is contained in:
@@ -13,10 +13,12 @@ import com.ruoyi.system.config.AliConfig;
|
||||
import com.ruoyi.system.mapper.MusicInfoMapper;
|
||||
import com.ruoyi.system.mapper.ShopUserMapper;
|
||||
import com.ruoyi.system.mapper.SysUserPostMapper;
|
||||
import com.ruoyi.system.service.IShareInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@RequestMapping("/file")
|
||||
@RestController
|
||||
@@ -41,6 +43,12 @@ public class FileController extends BaseController {
|
||||
// 获取文件字节
|
||||
if (objectName.equals("musicFile")) {
|
||||
LoginUser userInfo = SecurityUtils.getLoginUser();
|
||||
if (userInfo == null){
|
||||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.getWriter().write("{\"code\":401,\"msg\":\"用户未登录\"}");
|
||||
return;
|
||||
}
|
||||
SysUser sysUser = userInfo.getUser();
|
||||
if (sysUser == null) {
|
||||
if (StrUtil.isBlank(musicId)) {
|
||||
@@ -86,19 +94,63 @@ public class FileController extends BaseController {
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置响应头,支持中文文件名
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
|
||||
response.getOutputStream().write(data);
|
||||
response.getOutputStream().flush();
|
||||
|
||||
// 设置响应头
|
||||
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
||||
if (fileExtension.equals("mp3")) {
|
||||
// 对于MP3文件,设置为音频流,支持直接播放
|
||||
response.setContentType("audio/mpeg");
|
||||
// 设置内容长度
|
||||
response.setContentLength(data.length);
|
||||
// 允许范围请求,支持断点续传
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
// 不设置Content-Disposition,这样浏览器会直接播放而不是下载
|
||||
} else {
|
||||
// 其他文件类型,保持下载行为
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
|
||||
}
|
||||
try {
|
||||
response.getOutputStream().write(data);
|
||||
response.getOutputStream().flush();
|
||||
} catch (IOException e) {
|
||||
// 检查是否为客户端断开连接的错误(Broken pipe)
|
||||
if (e.getMessage() != null &&
|
||||
(e.getMessage().contains("Broken pipe") ||
|
||||
e.getMessage().contains("Connection reset by peer") ||
|
||||
e.getMessage().contains("连接被对方重置") ||
|
||||
e.getMessage().contains("你的主机中的软件中止了一个已建立的连接") ||
|
||||
e.getMessage().contains("Software caused connection abort"))) {
|
||||
// 客户端已断开连接,记录日志但不作为错误处理
|
||||
logger.info("客户端断开连接,文件传输中断: {}", e.getMessage());
|
||||
} else {
|
||||
// 其他IO错误,重新抛出
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
e.printStackTrace();
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.getWriter().write("{\"code\":500,\"msg\":\"下载失败: " + e.getMessage() + "\"}");
|
||||
// 检查是否为客户端断开连接的错误
|
||||
if (e instanceof IOException && e.getMessage() != null &&
|
||||
(e.getMessage().contains("Broken pipe") ||
|
||||
e.getMessage().contains("Connection reset by peer") ||
|
||||
e.getMessage().contains("连接被对方重置") ||
|
||||
e.getMessage().contains("你的主机中的软件中止了一个已建立的连接") ||
|
||||
e.getMessage().contains("Software caused connection abort"))) {
|
||||
// 客户端已断开连接,记录日志但不作为错误处理
|
||||
logger.info("客户端断开连接,文件传输中断: {}", e.getMessage());
|
||||
} else {
|
||||
// 其他错误,返回错误响应
|
||||
e.printStackTrace();
|
||||
// 检查响应是否已提交
|
||||
if (!response.isCommitted()) {
|
||||
response.reset();
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.getWriter().write("{\"code\":500,\"msg\":\"下载失败: " + e.getMessage() + "\"}");
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// 处理异常时出现的异常,忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,4 +93,13 @@ public class ShareInfoController extends BaseController
|
||||
{
|
||||
return toAjax(shareInfoService.deleteShareInfoByIds(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加分享次数
|
||||
*/
|
||||
@GetMapping("/increaseShareCount")
|
||||
public AjaxResult increaseShareCount(@RequestParam("id") String id)
|
||||
{
|
||||
return toAjax(shareInfoService.increaseShareCount(id));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user