用JSP实现文件(excel和text)的下载的代码?详细的......

解决方案 »

  1.   


    //-------下载Excel------------  
    File file=null;
            InputStream ins=null;
            String fileName=null;
            int fileSize=0;         
    file=new File(path+"\\Excel\\"+excelName);
            ins=new FileInputStream(file);
            fileName=file.getName();
            fileName=new String(fileName.getBytes(),"iso-8859-1");
            fileSize=(int)file.length();
            
            response.setContentType("application/x-msdownload");
            String str = "attachment; filename="+fileName;   //文件名
            response.setHeader("Content-Disposition", str);        
            response.setContentLength(fileSize);//大小
            
            ServletOutputStream sos=response.getOutputStream();
            byte[] data = new byte[2048];
            int len = 0;
            while((len=ins.read(data)) >0)
            {
                sos.write(data,0,len); 
            }        
            ins.close(); 
            sos.close();
            file.delete();        放到servlet中 ..下载excel
      

  2.   

    try
            {
                //这里你可以做些其他的事情
              response.setContentType("application/vnd.ms-excel");           //  Word  对应的就是 application/msword         
              response.setHeader("Content-disposition","attachment; filename=abc.xls");
                 //这里的abc是保存时的默认文件名
              BufferedInputStream bis = null;
              BufferedOutputStream bos = null;
              try {
                    // bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
                  bis = new BufferedInputStream(new FileInputStream("D:\\23.xls"));
                  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);
                     }             } catch(final IOException e) {
                     System.out.println ( "出现IOException." + e );
                 } finally {
                     if (bis != null)
                         bis.close();
                     if (bos != null)
                         bos.close();
                 }
                 return;        }
            catch (Exception e)
            {
                System.out.println(e);
            }
      

  3.   

    1 下载txt文件 
    代码如下:
    OutputStream o=response.getOutputStream();
      byte b[]=new byte[500];
      File fileLoad=new File("e:/test.txt");
       response.setContentType("application/octet-stream");
      response.setHeader("content-disposition","attachment; filename=text.txt");
      long fileLength=fileLoad.length();
      String length1=String.valueOf(fileLength);
      response.setHeader("Content_Length",length1);
      FileInputStream in=new FileInputStream(fileLoad);
      int n;
      while((n=in.read(b))!=-1){
       o.write(b,0,n);
      }  
      in.close();
      out.clear();
      out = pageContext.pushBody();
    2  下载excel文件 
    <%@page language="java" import="java.sql.*,java.io.*,java.net.URLEncoder,java.util.*" pageEncoding="gb2312"%>
    <% 
      OutputStream o=response.getOutputStream();
      byte b[]=new byte[1024];
      File fileLoad=new File("e:/dzl.xls");
      response.reset();
      response.setContentType("application/vnd.ms-excel");
      response.setHeader("content-disposition","attachment; filename=text.xls");
      long fileLength=fileLoad.length();
      String length1=String.valueOf(fileLength);
      response.setHeader("Content_Length",length1);
      FileInputStream in=new FileInputStream(fileLoad);
      int n;
      while((n=in.read(b))!=-1){
       o.write(b,0,n);
      }  
      in.close();
      out.clear();
      out = pageContext.pushBody(); 
    %>
    在web.xml下配置:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    //配置excel下载    
       <mime-mapping>   
      <extension>xls</extension>   
      <mime-type>application/vnd.ms-excel</mime-type>   
      </mime-mapping>   
      //配置word下载
       <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/vnd.ms-word</mime-type>
       </mime-mapping>
    </web-app>