来啦来啦,一个星期中最终要的时刻来了,基于Tomcat服务器上的网易云项目来了。小编这都几天没更新,是因为小编是一个在校的学生,目前在武汉一家Java培训机构培训,想来的可以找小编免费介绍,听说最近几天报名有优惠,python&Java都可以找小编了解,好了广告打完了我们直接进入主题吧!!!!!!!!!!!!!!!!
小编还是一样给出项目的结构图,大家可以瞅一眼
小编也是一个学生,所以呢前端页面是偷我们培训机构的,感觉挺好的,其次呢?小编使用的技术是Java做后端,html+css做的前端,脚本则是JS,数据库当然是mysql,其他没有了,小编写的代码有点多,可以给后台代码给各位看看,可能比较菜,里面也有点功能没写完,欢迎各位大佬指导.
登录后台LongServlet.java
package com.wxsc.controller;
import com.wxsc.dao.*;
import com.wxsc.pojo.US;
import com.wxsc.pojo.User;
import com.wxsc.pojo.song;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
/**
* @author 张宗臣
* @version 1.0
* @date 2021/12/6 0006 10:07
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
// 获取请求参数
String username = req.getParameter("username");
String pwd = req.getParameter("password");
// dao层
userDao userDao = new UserDaoImpl();// 用户
User u = userDao.Login(username,pwd);
USDao usDao = new USDaoLmpl();//用户--歌曲
SongDao songDao = new SongDaoLmpl();// 歌曲
// 如果登陆成功,用户的信息绑定到会话
// 获得当前用户的歌曲信息
if (u!=null){
HttpSession session = req.getSession();
session.setAttribute("user",u);
// 查询用户的歌曲
// 先获得当前用户的歌曲id集合
List<Integer> ids = usDao.findByUid(u.getId());
// 再根据id集合获得歌曲集合
List<song> songs = songDao.findBySidList(ids);
System.out.println(songs);
session.setAttribute("songs",songs);
req.getRequestDispatcher("main.jsp").forward(req,resp);
}else {
resp.sendRedirect("login.html");
}
}
}
添加歌曲的后台 AddSongServlet.java
package com.wxsc.controller;
import com.wxsc.dao.SongDao;
import com.wxsc.dao.SongDaoLmpl;
import com.wxsc.pojo.song;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;
/**
* @author 张宗臣
* @version 1.0
* @date 2021/12/5 0005 17:05
* 上传歌曲的Servlet
*/
//识别上下文件的注解
@MultipartConfig
@WebServlet("/addSong")
public class AddSongServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
// 上传文件,获得文件上传的信息[fileName-value location-value upLocadTime-value,size-value] 返回一个不完整的song
song s = uploadFile(req);
// 获得歌手 专辑,风格
String singer = req.getParameter("singer");
s.setSinger(singer);
String album = req.getParameter("album");
s.setAlbum(album);
String style = req.getParameter("style");
s.setStyle(style);
System.out.println(singer+album+style);
// 访问数据库,添加到数据库中
SongDao songDao = new SongDaoLmpl();
songDao.addSong(s);
// 添加成功页面
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
private song uploadFile(HttpServletRequest req) throws IOException, ServletException {
// 获得请求头的封装的Part对象----->封装请求文件的信息
// Content-Disposition :form-data;name = file ;filename="程辰 - 好像在爱你.mp3"
Part part = req.getPart("file");
// part本质自己不能获取文件名,但可以的到文件头
//可以通过 请求头的信息,进而获取上传文件的信息,进而截取
String message = part.getHeader("Content-Disposition");
// subString(最后一个等号加上7 刚好是文件名,到文件的末尾的位置) 函数是取前不取后
String fileName = message.substring(message.lastIndexOf("=")+7,
message.length()-1);
System.out.println(fileName);
// 获得输入流
InputStream in = part.getInputStream();
// 构建输出流
String location = "D:\\Wyy_Music"+ File.separator+fileName;
FileOutputStream fos = new FileOutputStream(location);
//IO操作
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read())!=-1){
fos.write(b,0,len);
}
//关闭
in.close();
fos.close();
// 上传时间
Timestamp uploadTime = new Timestamp(System.currentTimeMillis());
File file = new File(location);
long size = file.length();
song s = new song();
s.setSize(size);
s.setUploadTime(uploadTime);
s.setLocation(location);
s.setName(fileName.substring(0,fileName.lastIndexOf(".")));
return s;
}
}
删除的后台代码 DeleteServlet.java
package com.wxsc.controller;
import com.alibaba.fastjson.JSON;
import com.wxsc.dao.SongDao;
import com.wxsc.dao.SongDaoLmpl;
import com.wxsc.dao.USDao;
import com.wxsc.dao.USDaoLmpl;
import com.wxsc.pojo.US;
import com.wxsc.pojo.User;
import com.wxsc.pojo.song;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
/**
* @author 张宗臣
* @version 1.0
* @date 2021/12/7 0007 19:22
*/
@WebServlet("/deleteSong")
public class DeleteServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
// 获得请求参数
int id = Integer.parseInt(req.getParameter("sid"));
// dao
SongDao songDao = new SongDaoLmpl();
USDao usDao = new USDaoLmpl();
boolean flag = songDao.deleteById(id);
if (flag){
HttpSession session = req.getSession();
User u = (User) session.getAttribute("user");
// 重新绑定会话里面的歌曲信息,因为歌曲发生了更新
List<Integer> ids = usDao.findByUid(u.getId());
// 根据歌曲id集合获得歌曲对象集合
List<song> songs = songDao.findBySidList(ids);
session.setAttribute("songs",songs);
// 转发
resp.getWriter().println(JSON.toJSONString(true));
}else {
resp.getWriter().println(JSON.toJSONString(false));
}
}
}
还有的代码小编就不意译为ctrl c v了想要的可以联系小编qq:3265857641或者微信Q3265857641小编第一时间给你解答,当然小编也为你们准备好了网盘链接,
链接:https://pan.baidu.com/s/1F7GdCAYANheYp5IoU9G9Pg 提取码:ns4q
但是小编感觉可以加一下小编,不会还可以第一时间为你解答,或者你对Java十分热爱亦或者你对自己的控制力不相信,可以询问小编,小编可以给你安排免费的VIP试听课,不满意不报名