项目目前已经上线
音乐播放器登录页面铁子们可以后台私信获取管理员用户和密码
在上一篇文章中我们干掉了上传音乐方法的大头——判断mp3文件
本篇文章将完成,上传音乐文件和保存音乐文件信息,以及播放音乐模块
完整代码参考上一篇文章
@Value("${music.local.path}")
private String SAVE_PATH ;//使用反斜杠,用英文路径,部署在Linux上,避免不能识别
String fileNameAndType = file.getOriginalFilename();//获取文件的完整名称,包括文件名称和文件拓展名,xxx.mp3
System.out.println("fileNameAndType----->" + fileNameAndType);
String path = SAVE_PATH + "/" + fileNameAndType;//文件的存储路径
@Autowired
private MusicService musicService;
//3:music上传到服务器
Boolean result2 = musicService.uploadServer(file, path);
if(!result2){
return new ResponseBodyMessage<>(-1,"服务器上传失败",false);
}
Service业务逻辑层处理具体逻辑
public Boolean uploadServer(MultipartFile file, String path) {
File dest = new File(path);//用这个存储路径new 一个file , dest现在就相当于 前端传进来的file可以这么理解
if(!dest.exists()){//假设这个存储路径下有这个文件,去找它
dest.mkdirs();//找不到就考虑创建目录,尝试创建目录部分,也就是 D:\music 目录,而不会直接创建文件 song.mp3。如果文件不存在,它不会自动创建文件,mkdirs() 仅保证目录的创建。
}
try {
file.transferTo(dest);//上传文件
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
这里的重定向页面跳转,可以理解为页面刷新,极少数触发不跳转的情况(反正我遇到过)
//4:进行数据库的上传
// (1)准备数据 (2)调用insert
int ret = musicService.uploadDatebase(fileNameAndType,httpSession,singer);
if(ret == 1){
response.sendRedirect("/list.html");//前端实现完毕,上传成功进行跳转页面(重定向)
} else if (ret == -1) {//已经存在该音乐了
return new ResponseBodyMessage<>(-1,"数据库中已经存在该音乐",false);
} else {
dest.delete();//上传数据库失败
return new ResponseBodyMessage<>(-1,"数据库上传失败",false);
}
return new ResponseBodyMessage<>(0,"数据库上传成功",true);
这里的逻辑也很简单
第一步:截取.mp3文件前的名字作为title
第二步:查数据库是否有相同标题&&歌手的数据(注:歌手是前端传过来的,之所以这样判断,是因为可能一首歌有多种歌手嘛——就是我们常说的翻唱!)
第三步:根据session会话获取,是哪个用户上传的音乐
第四步:记录一下时间
第五步:调Mapper层的insertMusic方法(Mybatis出动)
/**
* @param fileNameAndType 文件全名称
* @param httpSession http会话
* @param singer
*/
public Integer uploadDatebase(String fileNameAndType, HttpSession httpSession, String singer) {
//上传前线判断数据库中是否已经存在相同的音乐了
int index = fileNameAndType.lastIndexOf(".");
String title = fileNameAndType.substring(0, index);
Music music = musicMapper.selectMusicByTitle(title,singer);
if(music != null){//查询到了一条相同名字音乐sql数据
return -1;//若存在返回-1
}
User userinfo = (User) httpSession.getAttribute(Constants.USERINFO_SESSION_KEY);
int userid = userinfo.getId();
//前端请求播放音乐的时候,请求的路径
String url = "/music/getMusic?path="+title;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//格式化日期类
String time = sf.format(new Date());
return musicMapper.insertMusic(title, singer,time,url,userid);
}
/**
* 新增音乐
* @param title
* @param singer
* @param time
* @param url
* @param userid
* @return
*/
int insertMusic(String title , String singer , String time , String url , int userid);
<insert id="insertMusic" >
insert into music (title,singer,time,url,userid)
values (#{title} , #{singer} , #{time} , #{url} , #{userid});
</insert>
验证的话使用postman进行模拟前端请求即可,这里不做过多展开
它继承于我们的HttpEntity类,这里面我们可以直接返回一些状态码,这些状态码都是常量值设置好了的
这里面我们重点介绍ok方法,他有好几个重载方法,这里我们使用一个简单的传参响应体内容的就行。ok本身的状态码就是200。我们在把需要返回的数据传参,这个方法自然会帮我们封装到响应体当中。
不多bb,上图理解会更清楚
下面实战
前端发送播放音乐的请求,我们拿到路径,去硬盘中读取音乐文件,返回给前端二进制文件,再由前端来解析文件并进行播放
/**
* 播放音乐的时候 :/music/get?path=xxx.mp3
* @param path
* @return
*/
@RequestMapping("/getMusic")
public ResponseEntity<byte[]> getMusic(String path){
File file = new File(SAVE_PATH + "/" + path);
byte[] a = null;
try {
a = Files.readAllBytes(file.toPath());
if(a == null){
return ResponseEntity.badRequest().build();
}
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok(a);
}