增加分享次数,优化音乐下载
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.MusicInfoMapper;
|
||||||
import com.ruoyi.system.mapper.ShopUserMapper;
|
import com.ruoyi.system.mapper.ShopUserMapper;
|
||||||
import com.ruoyi.system.mapper.SysUserPostMapper;
|
import com.ruoyi.system.mapper.SysUserPostMapper;
|
||||||
|
import com.ruoyi.system.service.IShareInfoService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@RequestMapping("/file")
|
@RequestMapping("/file")
|
||||||
@RestController
|
@RestController
|
||||||
@@ -41,6 +43,12 @@ public class FileController extends BaseController {
|
|||||||
// 获取文件字节
|
// 获取文件字节
|
||||||
if (objectName.equals("musicFile")) {
|
if (objectName.equals("musicFile")) {
|
||||||
LoginUser userInfo = SecurityUtils.getLoginUser();
|
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();
|
SysUser sysUser = userInfo.getUser();
|
||||||
if (sysUser == null) {
|
if (sysUser == null) {
|
||||||
if (StrUtil.isBlank(musicId)) {
|
if (StrUtil.isBlank(musicId)) {
|
||||||
@@ -86,19 +94,63 @@ public class FileController extends BaseController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置响应头,支持中文文件名
|
// 设置响应头
|
||||||
|
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.setContentType("application/octet-stream");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
|
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
|
||||||
|
}
|
||||||
|
try {
|
||||||
response.getOutputStream().write(data);
|
response.getOutputStream().write(data);
|
||||||
response.getOutputStream().flush();
|
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) {
|
} catch (Exception e) {
|
||||||
try {
|
try {
|
||||||
|
// 检查是否为客户端断开连接的错误
|
||||||
|
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();
|
e.printStackTrace();
|
||||||
|
// 检查响应是否已提交
|
||||||
|
if (!response.isCommitted()) {
|
||||||
|
response.reset();
|
||||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||||
response.setContentType("application/json;charset=UTF-8");
|
response.setContentType("application/json;charset=UTF-8");
|
||||||
response.getWriter().write("{\"code\":500,\"msg\":\"下载失败: " + e.getMessage() + "\"}");
|
response.getWriter().write("{\"code\":500,\"msg\":\"下载失败: " + e.getMessage() + "\"}");
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
|
// 处理异常时出现的异常,忽略
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,4 +93,13 @@ public class ShareInfoController extends BaseController
|
|||||||
{
|
{
|
||||||
return toAjax(shareInfoService.deleteShareInfoByIds(ids));
|
return toAjax(shareInfoService.deleteShareInfoByIds(ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加分享次数
|
||||||
|
*/
|
||||||
|
@GetMapping("/increaseShareCount")
|
||||||
|
public AjaxResult increaseShareCount(@RequestParam("id") String id)
|
||||||
|
{
|
||||||
|
return toAjax(shareInfoService.increaseShareCount(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,4 +65,6 @@ public class ShareInfo extends BaseEntity
|
|||||||
|
|
||||||
private String musicType;
|
private String musicType;
|
||||||
|
|
||||||
|
private Integer shareNum = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,14 @@ public interface ShareInfoMapper
|
|||||||
*/
|
*/
|
||||||
public ShareInfo selectShareInfoById(String id);
|
public ShareInfo selectShareInfoById(String id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据音乐ID查询分享信息
|
||||||
|
*
|
||||||
|
* @param musicId 音乐ID
|
||||||
|
* @return 分享信息
|
||||||
|
*/
|
||||||
|
public ShareInfo selectShareInfoByMusicId(String musicId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询【请填写功能名称】列表
|
* 查询【请填写功能名称】列表
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -61,4 +61,12 @@ public interface IShareInfoService
|
|||||||
public int deleteShareInfoById(String id);
|
public int deleteShareInfoById(String id);
|
||||||
|
|
||||||
List<ShareInfo> findShareInfoListPage(ShareInfo shareInfo);
|
List<ShareInfo> findShareInfoListPage(ShareInfo shareInfo);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加分享次数
|
||||||
|
*
|
||||||
|
* @param musicId 音乐ID
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public int increaseShareCount(String id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,9 +28,32 @@ public class ShareInfoServiceImpl implements IShareInfoService
|
|||||||
* @return 【请填写功能名称】
|
* @return 【请填写功能名称】
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ShareInfo selectShareInfoById(String id)
|
public List<ShareInfo> findShareInfoListPage(ShareInfo shareInfo) {
|
||||||
{
|
return shareInfoMapper.selectShareInfoListPage(shareInfo);
|
||||||
return shareInfoMapper.selectShareInfoById(id);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加分享次数
|
||||||
|
*
|
||||||
|
* @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);
|
return shareInfoMapper.deleteShareInfoById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ShareInfo> findShareInfoListPage(ShareInfo shareInfo) {
|
|
||||||
return shareInfoMapper.selectShareInfoListPage(shareInfo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<result property="updateTime" column="update_time" />
|
<result property="updateTime" column="update_time" />
|
||||||
<result property="isDel" column="is_del" />
|
<result property="isDel" column="is_del" />
|
||||||
<result property="review" column="review" />
|
<result property="review" column="review" />
|
||||||
|
<result property="shareNum" column="share_num" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<sql id="selectShareInfoVo">
|
<sql id="selectShareInfoVo">
|
||||||
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
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
<select id="selectShareInfoList" parameterType="ShareInfo" resultMap="ShareInfoResult">
|
<select id="selectShareInfoList" parameterType="ShareInfo" resultMap="ShareInfoResult">
|
||||||
@@ -39,6 +40,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
where id = #{id}
|
where id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="selectShareInfoByMusicId" parameterType="String" resultMap="ShareInfoResult">
|
||||||
|
<include refid="selectShareInfoVo"/>
|
||||||
|
where music_id = #{musicId} limit 1
|
||||||
|
</select>
|
||||||
|
|
||||||
<insert id="insertShareInfo" parameterType="ShareInfo" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insertShareInfo" parameterType="ShareInfo" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into share_info
|
insert into share_info
|
||||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
@@ -77,6 +83,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<if test="updateTime != null">update_time = #{updateTime},</if>
|
<if test="updateTime != null">update_time = #{updateTime},</if>
|
||||||
<if test="isDel != null">is_del = #{isDel},</if>
|
<if test="isDel != null">is_del = #{isDel},</if>
|
||||||
<if test="review != null">review = #{review},</if>
|
<if test="review != null">review = #{review},</if>
|
||||||
|
<if test="shareNum != null">share_num=#{shareNum}</if>
|
||||||
</trim>
|
</trim>
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|||||||
Reference in New Issue
Block a user