jsp开发下载文件模块,谁有经验

ivaict 2003-03-30 12:20:53
要求下载文件到客户端
...全文
30 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ivaict 2003-03-31
  • 打赏
  • 举报
回复
多谢多谢
我来实施
mengsky 2003-03-31
  • 打赏
  • 举报
回复
import java.io.*;
public class FileDownLoad {

public FileDownLoad() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;
try {
File ff=new File(aFileName);
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));
//设置类型和头信息
response.setContentType( "application/octet-stream" ); // MIME type for pdf doc
int pos=aFileName.lastIndexOf("/");
aFileName=aFileName.substring(pos+1);
response.setHeader("Content-disposition", "attachment; filename="+aFileName);
//传送数据
dout = new BufferedOutputStream( response.getOutputStream());
int once = 0;
int total = 0;
while ((total<length) && (once>=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
}
--------------------
<%@ page import="java.io.*,SqlData,FileDownLoad,Utility,java.util.*"%><%@ page language="java" contentType="text/html;charset=gb2312"%><jsp:useBean id="fload" scope="page" class="FileDownLoad" /><%String username="";if (session.getValue("username")==null){%><script>window.open("/mail/webmail/login.htm","_top");</script><%}else{username=session.getValue("username").toString();}String cd=request.getRequestURI();cd=cd.substring(cd.lastIndexOf('/')+1);String path=application.getRealPath(cd);int lastSlash=path.lastIndexOf('\\');path=path.substring(0,lastSlash);String filename=path+"/mail/affix/"+username+"/"+request.getParameter("affixname");if(filename!=null) fload.fileDown(filename,response);%>
------------------
<a href="+java.net.URLEncoder.encode(RS.getString("filename"))+">RS.getString("filename")


download.jsp文件,请不要用硬回车,要一行写下来,修改相应的路经传递给FILEDOWNLOAD类就可以了

ivaict 2003-03-30
  • 打赏
  • 举报
回复
请教各位
以上是什么问题
我直接写<a href="test.doc"></a> 点击这个连接处上边的错
ivaict 2003-03-30
  • 打赏
  • 举报
回复
出错提示
您不能关闭Microsoft word 因为有一个对话框仍处于活动状态
请先切换到Microsoft word 关闭该对话框
我察看进程确实有一个word进程,但word没有打开
ivaict 2003-03-30
  • 打赏
  • 举报
回复
不行
比如我下载一个word老是报错
zhxx 2003-03-30
  • 打赏
  • 举报
回复
可以用jspSmartUpload
你搜索一下
zhwquan 2003-03-30
  • 打赏
  • 举报
回复
<a href=''></a>
ivaict 2003-03-30
  • 打赏
  • 举报
回复
急急急
诸位大虾赶紧助小弟

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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