链接导出文件的时候,用流会存在一些环境上的错误,解决方法是采用Dispatcher方式。
1、首先设置你要定义你下载的链接,我以一个xls文件为例。
<input οnclick="javascript:window.location.href='importuser.jsp?fileName=funtion1.xls&filePath=(file_name)/function1.xls'" type="button" value="you download name" align="absmiddle"/>
2、然后在jsp上插入Dispatcher实现:
<%
//下载演示包 lzl
String filePath = "/" + request.getParameter("filePath");
String fileName = request.getParameter("fileName");
if(fileName!=null&&filePath!=null){
response.setContentType("application/x-download");
response.addHeader("Content-Disposition","attachment;filename=" + java.net.URLEncoder.encode(fileName,"UTF-8"));
try{
out.clear();
out=pageContext.pushBody();
}catch(Throwable e){
e.printStackTrace();
}
try{
RequestDispatcher dis = application.getRequestDispatcher(filePath);
dis.forward(request,response);
}catch(Throwable e){
e.printStackTrace();
}finally{
response.flushBuffer();
}
}
%>