添加依赖:
<dependency>
<groupId>org</groupId>
<artifactId>jaudiotagger</artifactId>
<version>2.0.3</version>
</dependency>
/**
* @desc : 上传音乐
* @author : 毛会懂
* @create: 2020/3/10 16:37:00
**/
@RequestMapping("/upload")
public ApiResult<?> uploadMusic(@RequestParam("file") MultipartFile file){
ApiResult<?> ret = null;
File localFile = null;
try {
String fileName = file.getOriginalFilename();
localFile = File.createTempFile(UUID.randomUUID().toString(), ".mp3");
file.transferTo(localFile);
//解析音乐,取长度和大小,存储路径
Map<String,Object> map = new HashMap<>();
String fileUrl = fileUploadBusiness.uploadPicFile(fileName, localFile);
map.put("size",String.format("%.1f", file.getSize() / 1024.0));
map.put("path",fileUrl);
localFile.setReadable(Boolean.TRUE);
try {
MP3File f = (MP3File) AudioFileIO.read(localFile);
MP3AudioHeader audioHeader = (MP3AudioHeader)f.getAudioHeader();
map.put("timeLength",audioHeader.getTrackLength());;
} catch(Exception e) {
log.error("解析mp3错误",e);
}
ret = ApiResult.success(map);
} catch (Exception e) {
log.error("", e);
ret = ApiResult.fail("文件上传失败!");
} finally {
FileUtils.deleteQuietly(localFile);
}
return ret;
}