html界面
js界面
layui.config({
version: ‘1554901097999’ //为了更新 js 缓存,可忽略
});
layui.use([‘laydate’, ‘laypage’, ‘layer’, ‘table’, ‘carousel’, ‘upload’, ‘element’, ‘slider’], function(){
var laydate = layui.laydate //日期
,laypage = layui.laypage //分页
,table = layui.table //表格
,element = layui.element //元素操作
,slider = layui.slider //滑块
//执行一个 table 实例
table.render({
elem: ‘#demo’
,height: 420
,url: ‘DownloadRecordListByPage’ //数据接口
,title: ‘用户表’
,page: true //开启分页
,toolbar: ‘true’ //开启工具栏,此处显示默认图标,可以自定义模板,详见文档
,defaultToolbar :[‘filter’, ‘exports’]
,cols: [[ //表头
{type: ‘checkbox’, fixed: ‘left’}
,{field: ‘id’, title: ‘ID’, sort: true, fixed: ‘left’, totalRowText: ‘合计:’}
,{field: ‘documentId’, title: ‘文件ID’}
,{field: ‘documentname’, title: ‘文件名字’,width: 165, sort: true}
,{field: ‘path’, title: ‘路径’, width: 165, sort: true}
,{field: ‘inserttime’, title: ‘插入时间’, width: 180, sort: true}
,{fixed: ‘right’, align:‘center’, toolbar: ‘#barDemo’}
]]
});
//监听头工具栏事件
table.on(‘toolbar(test)’, function(obj){
/* var checkStatus = table.checkStatus(obj.config.id)
,data = checkStatus.data; //获取选中的数据
switch(obj.event){
case ‘add’:
layer.msg(‘添加’);
break;
case ‘update’:
if(data.length === 0){
layer.msg(‘请选择一行’);
} else if(data.length > 1){
layer.msg(‘只能同时编辑一个’);
} else {
layer.alert(‘编辑 [id]:’+ checkStatus.data[0].id);
}
break;
case ‘delete’:
if(data.length === 0){
layer.msg(‘请选择一行’);
} else {
layer.msg(‘删除’);
}
break;
};*/
});
//监听行工具事件
table.on(‘tool(test)’, function(obj){ //注:tool 是工具条事件名,test 是 table 原始容器的属性 lay-filter=“对应的值”
var data = obj.data //获得当前行数据
,layEvent = obj.event; //获得 lay-event 对应的值
if(layEvent === ‘del’){
layer.confirm('真的删除行么', function(index){
obj.del(); //删除对应行(tr)的DOM结构
$.ajax({
url :'deleteDocunmentById',
type : 'post',
data:{"id":data.id,
"fileName":data.documentname},//这里的data是传往后台的参数
success : function(result) {
layer.msg(result)
}
});
layer.close(index);
//向服务端发送删除指令
});
} else if(layEvent === 'edit'){//这里是下载按钮
var downloadFilePath = data.path;
downloadFilePath = downloadFilePath.replace(/\\/g,"/")
console.log(downloadFilePath);
downloadFile("/downloadFile",downloadFilePath);
}
});
//form表单下载文件方法,等待被调用
function downloadFile(url,downloadFilePath){
var form=$("<form>"); //定义一个form表单,通过form表单来发送请求
form.attr("style","display:none"); //设置表单状态为不显示
form.attr("method","post");//method属性设置请求类型为post
form.attr("action",url);//action属性设置请求路径,请求类型是post时,路径后面跟参数的方式不可用,可以通过表单中的input来传递参数
$("body").append(form);//将表单放置在web中
var input1=$("<input>"); //在表单中添加input标签来传递参数,如有多个参数可添加多个input标签
input1.attr("type","hidden");//设置为隐藏域
input1.attr("name","downloadFilePath");//设置参数名称
input1.attr("value",downloadFilePath);//设置参数值
form.append(input1);//添加到表单中
form.submit();//表单提交
}
//分页
laypage.render({
elem: 'pageDemo' //分页容器的id
,count: 100 //总页数
,skin: '#1E9FFF' //自定义选中色值
//,skip: true //开启跳页
,jump: function(obj, first){
if(!first){
layer.msg('第'+ obj.curr +'页', {offset: 'b'});
}
}
});
slider.render({
elem: ‘#sliderDemo’
,input: true //输入框
});
});
controller界面
// 文件下载相关代码
@RequestMapping(value="/downloadFile",method = RequestMethod.POST)
@LogRecordListener(event = “下载文件”)
public void downloadFile(HttpServletRequest request,HttpServletResponse response,@RequestParam(“downloadFilePath”) String downloadFilePath) throws Exception {
String str = downloadFilePath;
String[] s = str.split("/");
String fileName = s[s.length-1];// 被下载文件的名称
System.out.println(fileName);
File file = new File(downloadFilePath);
if (fileName!= null) {
if (file.exists()) {
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = new byte[1024];
//FileInputStream 读取本地文件
FileInputStream fis = null;
//BufferedInputStream
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream out = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
out.write(buffer, 0, i);
i = bis.read(buffer);
}
logger.info("文件下载成功");
} catch (Exception e) {
logger.info("文件下载失败");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
logger.info("文件不存在");
}
logger.info("文件名不存在");
}
js,html 用到layui前端框架,不是很熟悉,自己做调整去吧。2019.7.18. 主要写给自己看的。