From 40234f36945ddce8b1275db2830018faf8a9836b Mon Sep 17 00:00:00 2001 From: menxipeng Date: Wed, 27 Aug 2025 21:28:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=86=E4=BA=AB=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E4=BC=98=E5=8C=96=E9=9F=B3=E4=B9=90=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/back/FileController.java | 72 ++++++++++++++++--- .../client/ShareInfoController.java | 9 +++ .../common/core/domain/entity/ShareInfo.java | 2 + .../ruoyi/system/mapper/ShareInfoMapper.java | 8 +++ .../system/service/IShareInfoService.java | 8 +++ .../service/impl/ShareInfoServiceImpl.java | 35 +++++++-- .../mapper/system/ShareInfoMapper.xml | 9 ++- 7 files changed, 125 insertions(+), 18 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/back/FileController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/back/FileController.java index a334314..7ed6479 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/back/FileController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/back/FileController.java @@ -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) { + // 处理异常时出现的异常,忽略 } } } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/client/ShareInfoController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/client/ShareInfoController.java index 3dbba45..c9a5036 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/client/ShareInfoController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/client/ShareInfoController.java @@ -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)); + } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/ShareInfo.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/ShareInfo.java index 2ef2ba0..ad868af 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/ShareInfo.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/ShareInfo.java @@ -65,4 +65,6 @@ public class ShareInfo extends BaseEntity private String musicType; + private Integer shareNum = 0; + } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShareInfoMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShareInfoMapper.java index a1872fb..d40f1fa 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShareInfoMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShareInfoMapper.java @@ -19,6 +19,14 @@ public interface ShareInfoMapper * @return 【请填写功能名称】 */ public ShareInfo selectShareInfoById(String id); + + /** + * 根据音乐ID查询分享信息 + * + * @param musicId 音乐ID + * @return 分享信息 + */ + public ShareInfo selectShareInfoByMusicId(String musicId); /** * 查询【请填写功能名称】列表 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IShareInfoService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IShareInfoService.java index 4c041dd..ca7042f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IShareInfoService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IShareInfoService.java @@ -61,4 +61,12 @@ public interface IShareInfoService public int deleteShareInfoById(String id); List findShareInfoListPage(ShareInfo shareInfo); + + /** + * 增加分享次数 + * + * @param musicId 音乐ID + * @return 结果 + */ + public int increaseShareCount(String id); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ShareInfoServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ShareInfoServiceImpl.java index 4c5fd50..12ad110 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ShareInfoServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ShareInfoServiceImpl.java @@ -28,9 +28,32 @@ public class ShareInfoServiceImpl implements IShareInfoService * @return 【请填写功能名称】 */ @Override - public ShareInfo selectShareInfoById(String id) - { - return shareInfoMapper.selectShareInfoById(id); + public List findShareInfoListPage(ShareInfo shareInfo) { + return shareInfoMapper.selectShareInfoListPage(shareInfo); + } + + /** + * 增加分享次数 + * + * @return 结果 + */ + @Override + public int increaseShareCount(String id) { + // 查询是否存在该音乐的分享记录 + ShareInfo shareInfo = shareInfoMapper.selectShareInfoById(id); + if (shareInfo != null) { + // 存在记录,更新分享次数 + shareInfo.setShareNum(shareInfo.getShareNum() + 1); + shareInfo.setUpdateTime(DateUtils.getNowDate()); + return shareInfoMapper.updateShareInfo(shareInfo); + } + return 0; + } + + + @Override + public ShareInfo selectShareInfoById(String id) { + return null; } /** @@ -95,8 +118,6 @@ public class ShareInfoServiceImpl implements IShareInfoService return shareInfoMapper.deleteShareInfoById(id); } - @Override - public List findShareInfoListPage(ShareInfo shareInfo) { - return shareInfoMapper.selectShareInfoListPage(shareInfo); - } + + } diff --git a/ruoyi-system/src/main/resources/mapper/system/ShareInfoMapper.xml b/ruoyi-system/src/main/resources/mapper/system/ShareInfoMapper.xml index 2626026..2fda8ee 100644 --- a/ruoyi-system/src/main/resources/mapper/system/ShareInfoMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/ShareInfoMapper.xml @@ -15,10 +15,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + - select id, user_id, share_decs, music_id, share_addr, share_plat, create_time, update_time, is_del, review from share_info + select id, user_id, share_decs, music_id, share_addr, share_plat, create_time, update_time, is_del, review,share_num from share_info + + insert into share_info @@ -77,6 +83,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update_time = #{updateTime}, is_del = #{isDel}, review = #{review}, + share_num=#{shareNum} where id = #{id}