Spring Boot音乐服务器项目-上传音乐模块
项目结构图
相较于上次新增集中在这些地方:
🚀 上传音乐的核心流程
-
前端投递:用户填写歌手名 + 选择MP3文件
-
后端接收:
/music/upload
接口化身音乐快递员 -
安全验证:先查用户是否“持证上岗”(登录态)
-
仓库选址:把音乐存到服务器的“音乐保险柜”(暂时就是存在自己的电脑)
-
数据库登记:给音乐办张“身份证”(标题、歌手、存储路径等)
⭐接口预览设计:
请求: { post, /music/upload {singer,MultipartFilefile}, }
响应: { \"status\":0, \"message\":\"上传成功!\", \"data\":true }
🔍 技术揭秘:关键代码与骚操作
⚙️ 1. 接口设计:音乐快递签收单
实体类:新建Music类
import lombok.Data;@Datapublic class Music { private int id; private String title; private String singer; private String url; private String time; private int userid;}
Controller层:MusicController
@RestController@RequestMapping(\"/music\")public class MusicController { @Value(\"${music.local.path}\") private String SAVE_PATH; @Resource private MusicMapper musicMapper; @RequestMapping(value = \"/upload\") public ResponseBodyMessage insertMusic(@RequestParam String singer, @RequestParam(\"filename\") MultipartFile file, HttpServletRequest req, HttpServletResponse resp) throws IOException { //无session就不创建 HttpSession httpSession = req.getSession(false); if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) { System.out.println(\"没有登录!\"); return new ResponseBodyMessage(-1, \"没有登录!\", false); } String filenameAndType = file.getOriginalFilename(); System.out.println(\"filenameAndType---->>>>>>>>>>\" + filenameAndType); String path = SAVE_PATH + \"\\\\ \" + filenameAndType; File dest = new File(path); System.out.println(\"dest:=>\" + dest.getPath()); if (!dest.exists()) { dest.mkdirs(); } try { file.transferTo(dest); } catch (IOException e) { e.printStackTrace(); return new ResponseBodyMessage(-1, \"上传失败\", false); } //数据库当中存储的歌曲不包括.mp3.所以需要进行截取 String title = filenameAndType.substring(0, filenameAndType.lastIndexOf(\".\")); //SimpleDateFormat来格式化当前的系统时间 SimpleDateFormat sf = new SimpleDateFormat(\"yyyy-MM-dd\"); String time = sf.format(new Date()); //这里会被用到 播放音乐的模块 String url = \"/CloudMusic/get?path=\"+title; User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY); int userId = user.getId(); int ret = musicMapper.insert(title,singer,time,url,userId); if(ret == 1){ }else{ dest.delete(); return new ResponseBodyMessage(-1,\"数据库上传失败,删除上传的⾳乐!\",false); } return new ResponseBodyMessage(0,\"上传成功!\",true); }}
String url = \"/CloudMusic/get?path=\"+title;
注意这个地方的 路径/http请求方法
注解介绍:
1. 使⽤@Value(\"${music.local.path}\"),获取到配置⽂件当中的值。不建议中⽂路径。
#⾳乐上传后的路径 music.local.path=D:/work/local/music
2. MultipartFile类,在org.springframework.web.multipart包当中,是Spring框架中处理⽂件上传 的主要类。
接口层:MusicMapper 现在还未将数据插⼊到数据库当中,接下来我们实现数据库中 数据的写⼊。
@Mapperpublic interface MusicMapper { int insert(String title,String singer,String time,String url,int userid);}
定义MusicMapper.xml
insert into music(title,singer,time,url,userid) values(#{title},#{singer},#{time},#{url},#{userid})
注意自己的文件名
基于此这个插入一个.MP3形式文件到数据库的接口就已经完成了
我们其实可以看到在我们的service中是写下了这样一段逻辑的
HttpServletResponse resp) throws IOException { //没有session不创建 HttpSession httpSession = req.getSession(false); if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) { System.out.println(\"没有登录!\"); return new ResponseBodyMessage(-1,\"没有登录!\",false); }
同时还有一些配置要记得配置,我的这个配置文件如下
application.propertied
#配置数据库spring.datasource.url=jdbc:mysql://127.0.0.1:3306/musicserver?characterEncoding=utf8&serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=1234spring.datasource.driver-class-name=com.mysql.jdbc.Driver#配置xmlmybatis.mapper-locations=classpath:mybatis/**Mapper.xml#配置springboot上传文件的大小,默认每个文件的配置最大为15Mb,单次请求的文件的总数不能大于100Mbspring.servlet.multipart.max-file-size = 15MBspring.servlet.multipart.max-request-size=100MB#音乐上传后的路径music.local.path=D:/CloudMusic#music.local.path=/home/gb/music# 配置springboot日志调试模式是否开启debug=true# 设置打印日志的级别,及打印sql语句#日志级别:trace,debug,info,warn,error#基本日志logging.level.root=INFOlogging.level.com.example.onlinemusic.mapper=debug#扫描的包:druid.sql.Statement类和frank包logging.level.druid.sql.Statement=DEBUGlogging.level.com.example=DEBUG
于是我们看到当我们在postman中测试的时候是无法直接调用这个insertMusic接口的
于是我们可以知道
💡 一些总结
-
事务性操作:文件存盘 + 数据库登记 = 原子操作(要么全成功,要么回滚)
-
路径解耦:
@Value
动态读取存储路径,搬家不用改代码! -
安全防线:Session验证 + 文件类型检测(虽然检测逻辑要自己补全😉)
-
扩展空间:
-
加文件去重?👉
SELECT * FROM music WHERE title=?
-
加音乐元数据解析?👉 用 JAudiotagger 库读ID3标签!
-
相对于上次的提交,提交文件如下:
代码仓库:插入音乐接口开发 7.22 /音乐服务器 - Gitee.com