`
wawa129
  • 浏览: 321420 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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)
                bos.close();
        }
        out.clear();
        out = pageContext.pushBody();
   
%>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics