81,114
社区成员
发帖
与我相关
我的任务
分享
//文件下载 2016.10.20
$('#downloadfile').click(function() {
var searchType = $('#searchType').combobox('getValue');
var searchStr = $('#searchStr').textbox('getText').trim();
var userTaskState = $('#userTaskState').combobox('getValue');
var form = $("<form>");
form.attr('style', 'display:none');
form.attr('target', '');
form.attr('method', 'post');
form.attr('action', "${pageContext.request.contextPath}/m_tasktm_usertask/downloadFile");
var input1 = $('<input>');
input1.attr('type', 'hidden');
input1.attr('name', 'searchType');
input1.attr('value', searchType);
var input2 = $('<input>');
input2.attr('type', 'hidden');
input2.attr('name', 'searchStr1');
input2.attr('value', searchStr);
var input3 = $('<input>');
input3.attr('type', 'hidden');
input3.attr('name', 'userTaskState');
input3.attr('value', userTaskState);
$('body').append(form);
form.append(input1);
form.append(input2);
form.append(input3);
form.submit();
});
String fileName = "";
fileName = tanmiUserTaskService.fileNameOfUserTaskExcel(searchStr,searchType,userTaskState);
String path =Command.SYSTEM_DOWNLOAD_EXCEL+fileName+"."+Command.SYSTEM_DOWNLOAD_FILE_EXTS;
File file=new File(path);
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b= new byte[1024];
int len = 0;
try{
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType("application/force-download");
String filename = file.getName();
response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentLength( (int) file.length( ) );
while((len = inputStream.read(b)) != -1){
outputStream.write(b, 0, len);
}
inputStream.close();
outputStream.close();
}catch(Exception e){
System.out.println("文件下载异常:downloadFile方法");
e.printStackTrace();
}finally{
try {
inputStream.close();
} catch (IOException e) {
System.out.println("流文件读取异常");
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
System.out.println("流文件输出异常");
e.printStackTrace();
}
}
前台JS方法和后台方法。用js封装一个表单。然后发送给后台。这样实现的效果是,当用户点击按钮的时候就能实现直接下载。另存为的那种框框,而不用新弹出页面或者别的之类的。