<%@page pageEncoding="UTF-8"%>
<%@page import="javax.servlet.ServletOutputStream"%>
<%@page import="java.io.*"%>
<%
//获取标签中使用的国际化资源信息
String fileNotExist="文件不存在";
String localFile=String.valueOf(request.getAttribute("downLoadPath"));
byte[] buffer = new byte[512];
int size = 0;
response.reset();
//response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment;filename=\""+ java.net.URLEncoder.encode(localFile.substring(localFile.lastIndexOf("/")+1),"UTF-8") + "\"");
ServletOutputStream os = null;
FileInputStream in = null;
try {
os = response.getOutputStream();
File downloadFile=new File(localFile);
if(downloadFile!=null&&downloadFile.exists()){
in = new FileInputStream(new File(localFile));
while ((size = in.read(buffer)) != -1) {
os.write(buffer, 0, size);
}
out.clear();
out = pageContext.pushBody();
}else{
out.print(fileNotExist); //"文件不存在!"
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(in!=null)in.close();
if(os!=null)os.close();
//File file=new File(localFile);
//if (file!=null&&file.isFile()&&file.exists()) {
// file.delete();
//}
} catch (IOException e) {
e.printStackTrace();
}
}
%>
网上都说是要加上out.clear();out = pageContext.pushBody(); 可是我的有啊(T▽T);
最后发现是文件路径不全。。。。。。