package com.edu.servlet;
import com.edu.bean.SongBean;
import com.edu.bean.SongtypeBean;
import com.edu.bean.UserBean;
import com.edu.bean.VipBean;
import com.edu.service.*;
import com.edu.service.impl.*;
import com.edu.util.R;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.DecimalFormat;
import java.util.List;
/**
* @Author: 王仁洪
* @Date: 2019/1/16 11:07
*/
public class SongUtilServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private SongService songService = new SongServiceImpl();
private SongTypeService songTypeService = new SongTypeServiceImpl();
private ClicksService clicksService = new ClicksServiceIpml();
private UserService userService = new UserServiceImpl();
private DownloadService downloadService = new DownloadServiceImpl();
private VipService vipService = new VipServiceImpl();
private Gson gson = new Gson();
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String state = request.getParameter("state");
System.out.println(state);
if ("querySongType".equals(state)){
this.querySongType(request,response);
}else if ("querySongs".equals(state)){
this.querySongs(request,response);
}else if ("querySongByTypeId".equals(state)){
this.querySongByTypeId(request,response);
}else if("hotSearch".equals(state)){
this.hotSearch(request,response);
}else if ("hotDownload".equals(state)){
this.hotDownload(request,response);
}else if ("click".equals(state)){
this.click(request,response);
}else if ("query".equals(state)){
this.query(request,response);
}else if ("listen".equals(state)){
this.listen(request,response);
}else if ("download".equals(state)){
this.download(request,response);
}else if ("purchase".equals(state)){
this.purchase(request,response);
}else if ("selectVip".equals(state)){
this.selectVip(request,response);
}else if ("recharge".equals(state)){
this.recharge(request,response);
}
}
private void recharge(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
PrintWriter printWriter = response.getWriter();
Integer user_id = Integer.parseInt(request.getParameter("user_id"));
Integer vip_id = Integer.parseInt(request.getParameter("vip_id"));
Boolean flag = userService.recharge(user_id, vip_id);
R r = R.modify(flag);
String json = gson.toJson(r);
printWriter.print(json);
}
private void selectVip(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer userId = Integer.parseInt(request.getParameter("userId"));
List<UserBean> userBeans = userService.selectById(userId);
Integer vip_id = userBeans.get(0).getVip_id();
List<VipBean> vipBeans = vipService.selectUpVip(vip_id);
Double rate = 0.8;
for (int i=0;i<vipBeans.size();i++){
VipBean vipBean = vipBeans.get(i);
//计算月数
Integer monthNum = (i+1)*6;
//得到vip类型
String vip = vipBean.getVip();
//计算金额
Double money = monthNum * 8 * rate;
//将金额格式化为小数点后保留两位数字
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String formatMoney = decimalFormat.format(money);
vipBean.setVip(vip + monthNum + "个月共计" + formatMoney);
// vipBeans.get(i).setVip(vipBeans.get(i).getVip() + ((i+1)*6) + "个月共计"
// + ((i+1)*6)*8*rate);
}
request.setAttribute("userId",userId);
request.setAttribute("vipBeans",vipBeans);
request.getRequestDispatcher("/page/user/recharge_jsp").forward(request,response);
}
private void downLoad(List<SongBean> songBeans,HttpServletResponse response) throws UnsupportedEncodingException {
//获取所要下载的文件名称
String song_name = songBeans.get(0).getSong_name();
String song_format = songBeans.get(0).getSong_format();
//下载文件所在目录
String song_url = songBeans.get(0).getSong_url();
String fileName = new String((song_name + "." + song_format).getBytes("utf-8"),"ISO8859-1");
// System.out.println(fileName);
try {
// path是指欲下载的文件的路径。
File file = new File(song_url);
// 以流的形式下载文件。
InputStream inputStream = new BufferedInputStream(new FileInputStream(song_url));
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void purchase(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
Integer user_id = Integer.parseInt(request.getParameter("user_id"));
Integer song_id = Integer.parseInt(request.getParameter("song_id"));
downloadService.download(user_id,song_id);
List<SongBean> songBeans = songService.selectById(song_id);
this.downLoad(songBeans,response);
}
private void download(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setCharacterEncoding("utf-8");
//设置ContentType字段值
response.setContentType("text/html;charset=utf-8");
Integer song_id = Integer.parseInt(request.getParameter("song_id"));
List<SongBean> songBeans = songService.selectById(song_id);
Integer songVipId = songBeans.get(0).getVip_id();
HttpSession session = request.getSession();
Object loginId = session.getAttribute("userLoginId");
if (null==loginId){
request.getRequestDispatcher("/page/user/user_tips_jsp").forward(request,response);
}else {
List<UserBean> userBeans = userService.selectById((Integer) loginId);
Integer userVipId = userBeans.get(0).getVip_id();
if (userVipId >= songVipId){
//在下载(download)表插入一条下载记录,
//将歌曲(song)表中与song_id对应的记录的下载次数加一
downloadService.download((Integer)loginId,song_id);
this.downLoad(songBeans,response);
}else {
String purchaseInfo = "您需支付¥" + (songVipId - 1)*2 + "元以购买该歌曲!";
request.setAttribute("user_id",(Integer)loginId);
request.setAttribute("song_id",song_id);
request.setAttribute("purchaseInfo",purchaseInfo);
request.getRequestDispatcher("/page/user/purchase_jsp").forward(request,response);
}
}
}
pr
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
数据库课程设计mysql 【作品名称】:基于 Java+Mysql 实现的音乐库管理系统【数据库课程设计】 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 开发环境:idea + Tomcat9 + MySQL + Git 软件架构:JAVA + jsp + Maven + Bootstrap table 实现功能:充值、购买歌曲、poi数据导入导出、歌曲上传下载、歌曲播放、用户注册登录注销
资源推荐
资源详情
资源评论
收起资源包目录
基于 Java+Mysql 实现的音乐库管理系统【数据库课程设计】 (381个子文件)
style.css 133KB
bootstrap.min.css 118KB
animate.css 64KB
datepicker3.css 33KB
font-awesome.css 32KB
bootstrap-rtl.css 31KB
font-awesome.min.css 26KB
ambiance.css 25KB
bootstrap-datepicker.css 17KB
layer.css 11KB
layim.css 11KB
codemirror.css 7KB
laydate.css 6KB
bootstrap-table.min.css 4KB
style.css 4KB
laydate.css 3KB
layer.ext.css 3KB
login.css 2KB
component.css 2KB
normalize.css 2KB
Huploadify.css 1KB
demo.css 553B
tiki.css 441B
tiddlywiki.css 220B
fontawesome-webfont.eot 67KB
glyphicons-halflings-regular.eot 20KB
loading-0.gif 6KB
xubox_loading0.gif 6KB
xubox_loading3.gif 2KB
loading-2.gif 2KB
xubox_loading2.gif 2KB
xubox_loading1.gif 701B
loading-1.gif 701B
loading.gif 166B
.gitignore 133B
scala.html 28KB
index.html 22KB
index.html 17KB
index.html 13KB
index.html 13KB
index.html 10KB
index.html 10KB
index.html 9KB
index.html 8KB
index.html 8KB
index.html 7KB
index.html 7KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 5KB
index.html 5KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
less.html 4KB
index.html 4KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
scss.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
json-ld.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
共 381 条
- 1
- 2
- 3
- 4
资源评论
MarcoPage
- 粉丝: 4303
- 资源: 8839
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于Spring Boot框架的报表管理系统.zip
- (源码)基于树莓派和TensorFlow Lite的智能厨具环境监测系统.zip
- (源码)基于OpenCV和Arduino的面部追踪系统.zip
- (源码)基于C++和ZeroMQ的分布式系统中间件.zip
- (源码)基于SSM框架的学生信息管理系统.zip
- (源码)基于PyTorch框架的智能视频分析系统.zip
- (源码)基于STM32F1的Sybertooth电机驱动系统.zip
- (源码)基于PxMATRIX库的嵌入式系统显示与配置管理.zip
- (源码)基于虚幻引擎的舞蹈艺术节目包装系统.zip
- (源码)基于Dubbo和Redis的用户中台系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功