jsp+tomcat5.5
页面中提供rar文件和jad文件下载,结果jad文件左键点击后会弹出下载框,rar文件点击后会在页面中打开rar文件(跟用记事本强行打开一样,一堆乱七八糟的字符),请问这是什么原因?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<center>
<p><a href="#">首页</a><a href="#">帮助</a><a href="#">关于</a>
</p>
<p>Test</p>
<p> 下载手机客户端<a href="test.jad">test.jad</a></p>
<p>下载压缩包<a href="test.rar">test.rar</a></p>
</center>
</body>
</html>
页面中提供rar文件和jad文件下载,结果jad文件左键点击后会弹出下载框,rar文件点击后会在页面中打开rar文件(跟用记事本强行打开一样,一堆乱七八糟的字符),请问这是什么原因?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<center>
<p><a href="#">首页</a><a href="#">帮助</a><a href="#">关于</a>
</p>
<p>Test</p>
<p> 下载手机客户端<a href="test.jad">test.jad</a></p>
<p>下载压缩包<a href="test.rar">test.rar</a></p>
</center>
</body>
</html>
浏览器就会直接打开,而不是下载.
至于你的rar怎么会用记事本打开.是你某一次"rar的时候,选择打开方式,你选择了记事本,并且选择了默认"
这个....... 原因不明咧. 反正html规范就是这样的. 让别人给你试一下
我试了一下,将jad文件的默认打开方式设为了记事本,重启浏览器和Netbeans,可是jad文件仍然弹出下载对话框
if (!new File(sName).exists())//sName是点击下载时传过来的参数(文件名)
{
//文件不存在
request.getRequestDispatcher("跳回原来页面").forward(request,response);
}else
{
out.clear();
out = pageContext.pushBody();
try
{
sName = new String(sName.getBytes("iso8859-1"), "gb2312");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="+ new String(name.getBytes("gb2312"),"iso8859-1"));//转码,不会出现乱码
bis = new BufferedInputStream(new FileInputStream(sName));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
response.flushBuffer();
}
catch (Exception e)
{
String simplename = e.getClass().getSimpleName();
if(!"ClientAbortException".equals(simplename)){
e.printStackTrace(); }
}
finally
{
if (bis != null)
{
bis.close();
}
if (bos != null)
{
bos.close();
}
}
}