1、采用RequestDispatcher的方式进行
jsp页面中添加如下代码:
复制代码
<%
("application/x-download");//设置为下载application/x-download
String filedownload = "/要下载的文件名";//即将下载的文件的相对路径
String filedisplay = "最终要显示给用户的保存文件名";//下载文件时显示的文件保存名称
filenamedisplay = (filedisplay,"UTF-8");
("Content-Disposition","attachment;filename=" + filedisplay);
try
...{
RequestDispatcher dis = (filedownload);
if(dis!= null)
...{
(request,response);
}
();
}
catch(Exception e)
...{
();
}
finally
...{
}
%>
2、采用文件流输出的方式下载
复制代码
<%***@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%>
<%
//关于文件下载时采用文件流输出的方式处理:
//(),并且所有的%>后面不要换行,包括最后一个;
();//可以加也可以不加
("application/x-download");
String filedownload = "想办法找到要提供下载的文件的物理路径+文件名";
String filedisplay = "给用户提供的下载文件名";
filedisplay = (filedisplay,"UTF-8");
("Content-Disposition","attachment;filename=" + filedisplay);
OutputStream outp = null;
Jsp页面下载的两种方式 来自淘豆网www.taodocs.com转载请标明出处.