各位大虾帮帮忙,如何在下载txt文件的时候不在IE里面直接打开,而是下载,在jsp里面怎么设置??急
解决方案 »
- 怎样为自己的网站做接口
- hibernate中的序列化问题
- javascript 判断对象为空
- 用axis开发webservice,call.invoke(requestSoapMsg)返回的status是“20002”
- JSP刷新问题
- 求一脚本,本人不会jsp脚本书写
- 问个初级问题,在win2000的平台上能运行java开发的程序吗?
- 高分求在线电影播放系统程序300分
- 请问做JSP开发时选用了JSP的WEB服务器,还需要IIS干什么?
- 一条sql怎么写
- 问各位英雄,JSF可以开发成熟应用吗?
- 我的一个网站刚刚做成,程序修改还有很大的工作量,因为有3个人一起在修改程序,我想问一下,使用何种版本控制软件好一点
// 初始化
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 )
{
}
%>
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>
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
//输出流的时候是需要下面的ServletOutputStream 定义的才能向客户端输出
ServletOutputStream out = res.getOutputStream();
HttpSession session = req.getSession(true);
String realfile = WebConfig.getInstance().getProperty("WEB_ROOT");
/*
* String realfile = req.getRealPath("/");这个方法已经不被赞成使用了最好用ServletContext.getRealPath()
* 上面我使用的因为在ConfigIniServlet这个servlet中已经定义过了
*/
String filename = req.getParameter("file_name"); /*try {
filename = DbOperator.executeQueryString(
"select file_name from down_table");
}
catch (Exception ex1) {
}*/
String fullPath = realfile + "WEB-INF" + "/" + "down" + "/" + filename;
File file2get = new File(fullPath);
// System.out.println(fullPath);
// System.out.println(file2get.exists());
if (file2get.exists()) {
try {
boolean logFlag = false; //是否对下载进行计数
int readLen = 1024;
long fileLen = file2get.length();
byte[] rtnByte = new byte[readLen];
FileInputStream in = new FileInputStream(fullPath);
String httpRange = req.getHeader("Range");
// System.out.println("before if..."+httpRange);//
if (httpRange != null) {
// System.out.println(" after if..."+httpRange);//
// out.println("Only support downloading directly.");
//下面的代码就是支持多线程的下载支持工具下载
long brokenStart = Integer.parseInt(httpRange.substring(6,
httpRange.length() - 1));
long contentLen = fileLen - brokenStart;
//System.out.println(" brokenStart..."+brokenStart);//
StringBuffer contentRange = new StringBuffer();
contentRange.append("bytes ").append(brokenStart).append("-")
.append(fileLen - 1).append("/").append(fileLen);
//System.out.println("contentRange" + contentRange);//
res.setStatus(206);
res.setHeader("Content-type", "application/DRM");
res.setHeader("Content-Disposition",
"attachment; filename=" + filename);
res.setHeader("Content-Description", "My3ia DRM Data");
res.setHeader("Content-Length", Long.toString(contentLen));
res.setHeader("Content-Range", contentRange.toString());
long position = brokenStart;
in.skip(brokenStart);
int rtnSize = in.read(rtnByte, 0, readLen);
while (rtnSize > 0) {
out.write(rtnByte, 0, rtnSize);
position += rtnSize;
if ( position >= fileLen - 1 ) {
logFlag = true;
}
rtnSize = in.read(rtnByte, 0, readLen);
}
}
else { //单线程下载
//----------------------- Download Directly --------------------------
//System.out.println("after eles"+httpRange);
res.setStatus(200);
res.setHeader("Content-type", "application/DRM");
res.setHeader("Content-Disposition",
"attachment; filename=" + filename);
res.setHeader("Content-Description", "My3ia DRM Data");
res.setHeader("Content-length", Long.toString(fileLen));
//我数据读到一个字节数组然后然后分别指定位置和可以读取的最大字节数
int rtnSize = in.read(rtnByte, 0, readLen);
while (rtnSize > 0) {
out.write(rtnByte, 0, rtnSize);
rtnSize = in.read(rtnByte, 0, readLen);
}
//统计还不是很精确主要是,只能是对点击次数的下载,如果文件大话就统计的正确,文件小就会直接统计
logFlag = true;
}
in.close();
/* //removed by lnr 2005.12.23
if (DbOperator.executeQueryInt(
"select count(*) from down_table where file_name='" + filename +
"' ") <= 0) {
DbOperator.executeUpdate("insert into down_table values(1,'" +
filename + "')");
}
else {
DbOperator.executeUpdate(
"update down_table set down_count=down_count+1 where file_name='" +
filename + "'");
}
*/
if ( logFlag ) {
this.downloadLogger(filename);
logFlag = false;
} }
catch (Exception ex) {
out.println("<script language = \"javascript\">");
out.println("<!--");
out.println("window.alert('Downloading failure.');");
out.println("window.history.back();");
out.println("-->");
out.println("</script>");
}
try {
out.flush();
out.close();
}
catch (Exception ef) {
}
}
else {
out.println("<script language = \"javascript\">");
out.println("<!--");
out.println("window.alert('file is not exist.');");
out.println("window.history.back();");
out.println("-->");
out.println("</script>"); }
}