【JSP开源代码栏目提醒】:网学会员为广大网友收集整理了,file_download_do.jsp,希望对大家有所帮助!
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%><%
response.reset();
//设置为下载application/x-download
response.setContentType("application/x-download");
//退到应用的根目录下
String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../downloadfiles/test.java";
//显示保存的文件名
String filenamedisplay = "test.java";
filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
//定义输出流对象
OutputStream output = null;
//定义文件流对象
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
//关闭输出流
output.close();
output = null;
}
}
%>
上一篇:
File_Chinese.jsp
下一篇:
哈弗F7 VS名爵HS,谁更懂得年轻人的心?