<%@page language="java" contentType="application/x-msdownload"
pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.net.URLEncoder"%>
<%@page import="javax.faces.application.Application"%>
<%
//关于文件下载时采用文件流输出的方式处理:
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
//获取此项目的物理路径
String path = application.getRealPath("");
out.print(path);
//获取要下载的文件的物理路径
String filedownload = path + "/word/嵌入式软件可信性度量指标体系.docx";
String filedisplay = "嵌入式软件可信性度量指标体系.docx";
//转码,为了中文不去出现乱码
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="
+ filedisplay);
java.io.OutputStream outp = null;
java.io.FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
//要加以下两句话,否则会报错
//java.lang.IllegalStateException: getOutputStream() has already been called for //this response
out.clear();
out = pageContext.pushBody();
} catch (Exception e) {
System.out.println("Error!");
e.printStackTrace();
} finally {
if (in != null) {
in.close();
in = null;
}
}
%>