效果图
导入 jar包
<dependency>
<groupId>org</groupId>
<artifactId>jaudiotagger</artifactId>
<version>2.0.3</version>
</dependency>
后台代码
//获取大小
long l = sing.getSize();
String size = l / 1024 / 1024+"MB";
//获取时长
AudioFile read = AudioFileIO.read(new File(realPath, newFileName));
AudioHeader audioHeader = read.getAudioHeader();
//音频的秒数
int trackLength = audioHeader.getTrackLength();
文件上传
jqGrid做文件上传时,上传后然后直接修改存放文件的路径
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
if (oper.equals("add")){
String add = chapterService.add(albumId, chapter);
return add;
}
{
closeAfterAdd:true,
afterSubmit:function (response) {
var chapterId=response.responseText;
$.ajaxFileUpload({
url:"${pageContext.request.contextPath}/chapter/upload",
//文件名
fileElementId:"sing",
//当前对象id
data:{chapterId:chapterId},
success:function (data) {
//表格刷新
$("#"+tableId).trigger("reloadGrid");
}
});
return response;
}
},
String realPath = session.getServletContext().getRealPath("/mp3/");
File file = new File(realPath);
if(!file.exists()){
file.mkdirs();
}
String filename = sing.getOriginalFilename();
String newFileName = new Date().getTime() + "_" + filename;
try {
sing.transferTo(new File(file,newFileName));
} catch (IOException e) {
e.printStackTrace();
}
//调用修改数据库的方法
文件下载
String realPath = session.getServletContext().getRealPath("/mp3/");
//定义文件
File file = new File(realPath, audio);
//切割文件名字
String s = audio.split("_")[1];
//改编码格式
String encode = URLEncoder.encode(s, "UTF-8");
//设置头文件
response.setHeader("content-disposition","attachment;fileName="+encode);
ServletOutputStream outputStream = null;
try {
//获取输出流
outputStream = response.getOutputStream();
//下载
FileUtils.copyFile(file,outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭输出流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
二级表格
subGrid:true,
subGridRowExpanded:function (subGridId, albumId) {
var tableId = subGridId+"table";
var pagerId = subGridId+"pager";
$("#"+subGridId).html(
"<table style=\"text-align: center\" id="+tableId+"></table>\n" +
"<div id="+pagerId+"></div>"
);
$("#"+tableId).jqGrid({
url:"${pageContext.request.contextPath}/chapter/findByPage?albumId="+albumId,
editurl:"${pageContext.request.contextPath}/chapter/edit?albumId="+albumId,
datatype:"json",
//参数
//postData:{albumId:albumId},
colNames:["标题","大小","时长","上传时间","音频","操作"],
colModel:[
{name:"title",editable:true},
{name:"size",width:"40px"},
{name:"time",width:"70px"},
{name:"date",width:"70px"},
{name:"sing",editable:true,edittype:"file"},
{name:"",width:"250px",
formatter:function (cellvalue, options, rowObject) {
//添加播放按钮,自己添加文件下载按钮
return"<audio controls src=\"mp3/"+rowObject.sing+"\"></audio>"+
" <a><span οnclick=\"downloads('"+rowObject.sing+"')\" class='glyphicon glyphicon-download-alt'></span></a>";
}
}
],
styleUI:"Bootstrap",//改变样式
autowidth:true,//自适应宽度
pager:"#"+pagerId,
rowNum:2,
page:1,
rowList:[2,3,4],
rownumbers: true,//显示行号
multiselect:true,//多选框
viewrecords:true,
height:"60%"
}).jqGrid("navGrid","#"+pagerId,{search:false},
{//控制编辑按钮,在之前或者之后进行额外操作
closeAfterEdit:true,
beforeShowForm:function (obj) {
//不修改的属性
obj.find("#size").prop("disabled",true);
}
},
{
//控制添加按钮,在之前或者之后进行额外操作
closeAfterAdd:true
},
{
//控制删除按钮,在之前或者之后进行额外操作
})
}