在下载txt文件的时候不在IE里面直接打开,而是下载

方法一:
SmartUpload su = new SmartUpload();
 // 初始化 su.initialize(pageContext);
 // 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开。
su.setContentDisposition(null);
=============================
 
方法二:
<%
// example:
// <a href="download.jsp?path=img/&name=test.gif">download image</a>
String root = getServletContext().getRealPath("/");
String path = request.getParameter("path");
String name = request.getParameter("name");
response.setContentType("unknown");
response.addHeader("Content-Disposition", "filename=\"" + name + "\"");
try
{
java.io.OutputStream os = response.getOutputStream();
java.io.FileInputStream fis = new java.io.FileInputStream(root + path + name);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 )
{
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
}
%>
==============================
 
方法三:
<script>
var n=0;
function go(url){
   
    n==0?new function()
    {
        frames("frame1").location=url,n=1
    }:null;
   
    document.all("frame1").readyState!="complete"?setTimeout(go,10):so();
   
    function so()
    {
        frames("frame1").document.execCommand("SaveAs"),n=0
    };
}
</script>
<iframe id="frame1" style="display:none"></iframe>
<a  onClick="go('java.txt')" class="style2">下载</a>
原文地址:https://www.cnblogs.com/hyd309/p/1986291.html