基于SpringBoot的online_music_player(上传音乐模块_3)

100 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第9天,点击查看活动详情 

MusicController完善数据库上传

package com.example.onlinemusic.controller;
​
import com.example.onlinemusic.mapper.MusicMapper;
import com.example.onlinemusic.model.Contant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.util.logging.resources.logging;
​
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
​
​
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hold on
 * Date: 2022-07-27
 * Time: 15:40
 */
@RestController
@RequestMapping("/music")
public class MusicController {
​
//    //文件上传服务器后的路径地址!
//    private String SAVE_PATH = "D:/uploadmusicfile/";
​
    //我们可以将该路径信息设置到配置文件中!
    @Value("${music.path.save}")
    private String SAVE_PATH;
​
    //上传音乐到数据库的url前缀
    @Value("${music.url}")
    private String URL_PRE;
​
    @Resource  //属性注入
    private MusicMapper musicMapper;
​
    //这里的上传需要将音乐上传到服务器,还有就是需要将音乐信息上传到数据库!
    @RequestMapping("/upload")
    public ResponseBodyMessage<Boolean> UploadMusic(@RequestParam String singer,
                                               @RequestParam MultipartFile file,
                                               HttpServletRequest request){
​
        //1.上传音乐前验证登入状态,如果未登入就不创建会话
        //如果用户已登入,则允许上传音乐!
        HttpSession session = request.getSession(false);
        if(session==null||session.getAttribute(Contant.USERINFO_SESSION_KEY)==null){
            //未登入状态!
            System.out.println("未登入");
            return new ResponseBodyMessage<>(-1,"未登录,请登录后上传!",false);
        }
        //2.登入状态,可以将歌曲上传到服务器!
​
        //获取到音乐文件名  xxx.mp4
        String musicname = file.getOriginalFilename();
        System.out.println("musicfileAndtype:"+musicname);
        //上传文件在服务器下保存的路径!
        String path = SAVE_PATH + musicname;
        //创建文件对象
        File dest = new File(path);
        //该文件目录在磁盘中不存在,就创建该目录
        if(!dest.exists()){
            dest.mkdir();
            System.out.println("mkdir"+dest);
        }
        //将音乐文件上传到该目录下!
        try {
            file.transferTo(dest);
            System.out.println(dest);
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败
            return new ResponseBodyMessage<>(-1,"服务器上传失败",false);
        }
        //服务器上传成功,我们就需要对数据库进行上传信息!
​
        //1.数据准备
        //1).title 通过文件名截取到歌名!
        String title = musicname.substring(0,musicname.lastIndexOf(".mp3"));
        //2).singer 直接获取用户上传的 singer
        //3).time
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(new Date());
        //4).url 可以通过拼接! eg: music/get?path=隆里电丝
        String url = URL_PRE+title;
        //5).user_id 通过当前session获取
        String user_id = session.getId();
        int ret = musicMapper.upload(title,singer,time,url,user_id);
        if(ret!=1){//上传失败!
            //我们数据库上传失败,那么就需要将服务器下的该音乐文件删除
            dest.delete();
            return new ResponseBodyMessage<>(-1,"数据库上传失败",false);
        }
        return new ResponseBodyMessage<>(0,"上传成功",true);
    }
}
​

验证:

image-20220729143336681

image-20220729143353645

查看数据库music

image-20220729143244779