jsp实现文件的下载功能

上一篇 / 下一篇  2012-05-16 10:15:59 / 个人分类:jsp

DownFile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>显示现在的文件</title>
    
    <meta. http-equiv="pragma" content="no-cache">
    <meta. http-equiv="cache-control" content="no-cache">
    <meta. http-equiv="expires" content="0">    
    <meta. http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta. http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
  <center>
    <h3>请选择要下载的文件</h3>
    <hr/>
    <%
        String thePath=request.getRealPath("/");
        File file=new File(thePath,"\\upload");
        String str[]=file.list();//将路径下的文件名存储到str数组中
        out.print("<table>");
        for(int i=0;i<str.length;i++){
            String ss=str[i];
            out.print("<tr><td>"+ss+"</td><td><a href='FileUpDown/DownFileImp.jsp?name="+ss+"'>下载<a></td></tr>");
        }
        out.print("</table>");
     %>
   </center>
  </body>
</html>

DownFileImp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>实现文件的下载</title>
    
    <meta. http-equiv="pragma" content="no-cache">
    <meta. http-equiv="cache-control" content="no-cache">
    <meta. http-equiv="expires" content="0">    
    <meta. http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta. http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
    <%
        request.setCharacterEncoding("utf-8");
        response.reset();
        try{
            String str=request.getParameter("name");//获得响应客户的输出流
            str=new String(str.getBytes("iso8859-1"),"gb2312");
            String thePath=request.getRealPath("/");
            thePath=thePath.substring(0,thePath.lastIndexOf("\\"));
            thePath=thePath+"\\upload\\";
            File fileLoad=new File(thePath,str);//下载文件的位置
            response.reset();
            OutputStream o=response.getOutputStream();//获取输出流
            BufferedOutputStream bos=new BufferedOutputStream(o);
            byte b[]=new byte[500];//输出文件用的字节数组,每次发送500字节到输出流
            response.setHeader("Content-disposition","attachment;filename="+
                new String(str.getBytes("gb2312"),"iso8859-1"));//客户使用保存文件的对话框
            response.setContentType("application/x-tar");//通知客户文件的mime类型
            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(b))!=-1){
                bos.write(b,0,n);
            }
            bos.close();
        }
        catch(Exception e){
            System.out.println(e);
            response.reset();
        }
        
     %>
  </body>
</html>



TAG:

 

评分:0

我来说两句

龙飞

龙飞

追随自己的梦想,永不言弃。

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 71797
  • 日志数: 62
  • 文件数: 1
  • 书签数: 4
  • 建立时间: 2011-01-27
  • 更新时间: 2012-06-16

RSS订阅

Open Toolbar