日期:2014-05-16  浏览次数:20323 次

jsp实现文件下载
<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.*"%>

<%
      String filepath = System.getProperty("user.dir");
        filepath = filepath
                + "/../../service/tellin/tellin.war/usr/wimax/download/download.rar";
               
        String downloadFileFailed = "Download file failed";
       
        java.io.BufferedInputStream bis = null;
        java.io.BufferedOutputStream bos = null;
       
        try
        {
           
            String filename = filepath.substring(filepath.lastIndexOf("/") + 1);
            filename = new String(filename.getBytes("iso8859-1"), "gb2312");
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-disposition", "attachment; filename="
                    + filename);
            bis = new java.io.BufferedInputStream(new java.io.FileInputStream(
                    filepath));
            bos = new java.io.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 (Exception e)
        {
              %>
               <script>alert("<%=downloadFileFailed %>");</script>
              <%
        }
        finally
        {
            if (bis != null)
                bis.close();
            if (bos != null)