java⽂件下载form表单形式参数传递
html界⾯
⽂件下载界⾯
js界⾯
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 实例
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’}
]]
});
//监听头⼯具栏事件
<(‘toolbar(test)’, function(obj){
/
* var checkStatus = table.checkStatus()
,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;
};*/
});
//监听⾏⼯具事件
<(‘tool(test)’, function(obj){ //注:tool 是⼯具条事件名,test 是 table 原始容器的属性 lay-filter=“对应的值”var data = obj.data //获得当前⾏数据
,layEvent = obj.event; //获得 lay-event 对应的值
if(layEvent === ‘del’){
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 = place(/\\/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();//表单提交
}
//分页
elem: 'pageDemo' //分页容器的id
,
count: 100 //总页数
,skin: '#1E9FFF' //⾃定义选中⾊值
//,skip: true //开启跳页
,jump: function(obj, first){
if(!first){
layer.msg('第'+ obj.curr +'页', {offset: 'b'});
java修改html文件}
}
});
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 (ists()) {
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+de(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 = OutputStream();
int i = ad(buffer);
while (i != -1) {
out.write(buffer, 0, i);
i = ad(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. 主要写给⾃⼰看的。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。