jsp+javaBean实现文件下载

文件下载
package mybean;
import java.io.*;
public class FileNameAccept implements FilenameFilter{
 
    @Override
    public boolean accept(File dir, String filename) {
       // TODO Auto-generated method stub
       boolean boo= false ;
       if (filename.endsWith( ".doc" )||filename.endsWith( ".DOC" ))
       {
           System. out .println( " doc 文件 ..." );
           boo= true ;
       }
       return boo;
    }
}
 
// 文件下载
package mybean;
import java.io.*;
import javax.servlet.http.*;
public class downFileBean {
    HttpServletResponse response;
    String fileName;
    String dirPath;
    String fileMIME;
    public  void setFileMIME(String fileMIME)
    {
       System.out.println("setFileMIME...");
       this.fileMIME=fileMIME;
    }
    public void setResponse(HttpServletResponse response)
    {
       System.out.println("setResponse...");
       this.response=response;
    }
    public void setDirPath(String dirPath)
    {
       System.out.println("setDirPath...");
       this.dirPath=dirPath;
    }
    public void setFileName(String fileName)
    {
       System.out.println(" 设置 filename...");
       this.fileName=fileName;
       try{
           // 得到向客户端输出的输出流
           System.out.println("outputStream...");
           OutputStream outStream1=response.getOutputStream();
           // 输出文件用的字符数组
           byte bb[]=new byte[600];
           // 要下载的文件
           File fileload=new File(dirPath,this.fileName);
       // 客户端打开保存文件对话框
           response.setHeader("content-disposition","attachment;filename="+fileName);
           response.setContentType(fileMIME);
           long filelength=fileload.length();
           String length=String.valueOf(filelength);
           // 通知客户端文件长度
           response.setHeader("content_length",length);
           // 读取文件并发送给客户端
           FileInputStream in=new FileInputStream(fileload);
           int n=0;
           while((n=in.read(bb))!=-1)
           {
              outStream1.write(bb,0,n);
           }
          
       }
       catch(Exception e)
       {
           System.out.println(e.toString());
       }
    }
   
}
 
 
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="mybean.downFileBean"%>
<jsp:useBean id="downf" class="mybean.downFileBean" scope="page"/>
<!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=gb2312" />
<title>ch6_9_down.jsp</title>
</head>
 
<body>
<jsp:setProperty name="downf" property="dirPath" value="d:\\data"/>
 
<jsp:setProperty name="downf" property="response" value="<%=response%>"></jsp:setProperty>
 
<jsp:setProperty name="downf" property="fileMIME" value="application/msword"/>
 
<jsp:setProperty name="downf" property="fileName" value="<%=request.getParameter("filename")%>"/>
 
</body>
</html>
 
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="mybean.FileNameAccept"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!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=gb2312" />
<title>ch6_9.jsp</title>
</head>
 
<body>
<%
       File file1=new File("d:\\data");
       File filelist[]=file1.listFiles(new FileNameAccept());
       int count=0;
       out.print("<table border>");
       out.print("<tr><td colspan=4 align=center>");
       out.print("d:\\data 目录文件列表 </td></tr>");
       out.print("<tr><td width=40>"+" 序号 "+"</td>");
       out.print("<td width=300>"+" 文件名 "+"</td>");
       out.print("<<td width=80>"+" 文件长度 "+"</td>");
       out.print("<td width=80>"+" 下载吗 ?"+"</td>");
       out.print("</tr>");
       for(File me:filelist)
       {
              count++;
              out.print("<tr>");
              out.print("<td>"+count+"</td>");
              out.print("<td>"+me.getName()+"</td>");
              out.print("<td>"+me.length()+"</td>");
              out.print("<td><a href='ch6_9_down.jsp?filename="+me.getName()+"'> 我要下载 </a></td>");
              out.print("</tr>");
       }
%>
             
</body>
</html>3/7/2011 11:04:37 PM
 
 

你可能感兴趣的:(java,jsp,下载,休闲,javabea)