下载txt,如何弹出新窗口?

codeyell-com 2011-01-12 12:25:16
我用链接:
<a href="<%=path%>/doc/t.txt"><u>下载</u></a>
实现下载,但是浏览器会把文本内容显示出来,而不是弹出下载框

请问如何才能弹出框?
谢谢
...全文
113 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuzhuaihuzi 2011-01-12
  • 打赏
  • 举报
回复
另存为 就可以啊
codeyell-com 2011-01-12
  • 打赏
  • 举报
回复
修改IE只能对自己有效,看来我只得修改服务器实现了

[Quote=引用 3 楼 whut_lcy 的回复:]
一个是修改服务器代码,就犹如楼上的说的
还有修改IE设置,不叫他自己打开,而是弹出下载对话框
[/Quote]
partys 2011-01-12
  • 打赏
  • 举报
回复
怎么修改ie设置?
whut_lcy 2011-01-12
  • 打赏
  • 举报
回复
一个是修改服务器代码,就犹如楼上的说的
还有修改IE设置,不叫他自己打开,而是弹出下载对话框
focusforce 2011-01-12
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="application/x-msdownload"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.net.URLEncoder"%>
<%@page import="tkl.download.Constants"%>
<%@page import="tkl.download.action.ActionConstants"%>
<%@page import="java.io.FileInputStream"%>
<%
session.setAttribute(ActionConstants.CUSTOMER_ACTION,
"/cdownload.jsp");
Object loginUser = session
.getAttribute(Constants.SEESION_LOGINED_USER);
if (loginUser == null)
{
pageContext.forward("/clogin.jsp");
return;
}

response.reset();
response.setContentType("application/x-download");
String filedownload = application
.getRealPath("/download/tklfreesetup.exe");
String filedisplay = URLEncoder.encode("tklfreesetup.exe", "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();
out.clear();
out = pageContext.pushBody();
}
catch (Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if (in != null)
{
in.close();
in = null;
}
}
%>
focusforce 2011-01-12
  • 打赏
  • 举报
回复
File file = File.createTempFile("license", ".txt");
file.deleteOnExit();
FileWriter fw = new FileWriter(file);
String text = selectedLicense.getFile();
fw.write(text);
fw.close();

String path = file.getAbsolutePath();
// 读取文件名:用于设置客户端保存时指定默认文件名
int index = path.lastIndexOf("\\");
String fileName = path.substring(index + 1);

// 写流文件到前端浏览器
ServletOutputStream out = getResponse().getOutputStream();
getResponse().setHeader("Content-disposition",
"attachment;filename=" + fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧