日期:2014-05-18 浏览次数:20860 次
<form name="myForm" action="<%=path%>/platform/questionAction!fileDownload.action" method="post" enctype="multipart/form-data">
<a href="javascript: aClick();" >${Tbq.temp1}</a>
<input type="hidden" value="${Tbq.fileurl}" name="strurl"/>
</form>
<%--禁用缓存--%>
$(document).ready(function(){
$.ajaxSetup({cache:false});
});
function aClick(){
document.myForm.submit();
}
public String fileDownload(){
try{
//得到下载文件的名字
String url = request.getParameter("strurl");
if(url == null || url.equals("")){
return null;
}
//创建file对象
File file=new File(ServletActionContext.getServletContext().getRealPath("/")+url);
//设置response的编码方式
response.setContentType("application/x-msdownload");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//发送到客户端的文章
//解决中文乱码
response.setHeader("Content-Disposition","attachment;filename="+new String
(file.getName().getBytes("gbk"),"iso-8859-1"));
//读出文件到i/o流
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[buff.available()];
OutputStream myout=response.getOutputStream();
response.setContentType("application/octet-stream");
myout.write(b);
myout.flush();
myout.close();
}catch(Exception ex){
ex.printStackTrace();
}finally{
}
return null;
}