常用下载文件的公共方法

这是本人在项目中用到的,感觉还不错,在此分享一下: 

/**
     * 读取磁盘指定路径的文件,返回字节数组(可以写入输出流用来下载文件)
     * @param file 文件全路径
     * @throws IOException 读写文件异常
     * @return 下载文件的字节数组
    */
    public static byte[] readFileBytes(String file) throws IOException
    {
        File f = new File(file);
        if (!f.exists())
        {
            DebugLogFactory.error(Tools.class, "error:file is not exsit ");
            return null;
        }
        BufferedInputStream bis = null;
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        byte[] fileBytes = null;
       
        try
        {
            fis = new FileInputStream(f);
            bis = new BufferedInputStream(fis);
            int dataSize = 1024;
            byte[] bt = new byte[dataSize];
            bos = new ByteArrayOutputStream();
            int readLen = 0;
            while ((readLen = bis.read(bt)) > 0)
            {
                bos.write(bt, 0, readLen);
            }
           
            fileBytes = bos.toByteArray();
        }
        finally
        {
            if (null != bis)
            {
                try
                {
                    bis.close();
                }
                catch (Exception e)
                {
                    DebugLogFactory.error(Tools.class,
                            "close BufferedInputStream bis failed ",
                            e);
                }
               
                bis = null;
            }
           
            if (null != fis)
            {
                try
                {
                    fis.close();
                }
                catch (Exception e)
                {
                    DebugLogFactory.error(Tools.class,
                            "close FileInputStream fis failed ",
                            e);
                }
               
                fis = null;
            }
           
            if (null != bos)
            {
                try
                {
                    bos.close();
                }
                catch (Exception e)
                {
                    DebugLogFactory.error(Tools.class,
                            "close ByteArrayOutputStream bos failed ",
                            e);
                }
               
                bos = null;
            }
        }
       
        return fileBytes;
    }
   
    /**
     * 将字节数组写入输出流下载文件
     * @param downloadFileBytes 待写入的字节数组
     * @param resp HttpServletResponse
     * @param out PrintWriter
     * @param resFileName 下载文件的命名
     * @throws IOException 读写文件异常
    */
    public static void writeFileBytes(HttpServletResponse resp,
            PrintWriter out, byte[] downloadFileBytes, String resFileName)
        throws IOException
    {
        if (null != downloadFileBytes)
        {
            OutputStream opts = null;
            try
            {
                resp.reset();
                resp.setContentType("application/x-msdownload");
                resp.setHeader("Content-Disposition", "attachment;filename="
                        + resFileName);
                opts = resp.getOutputStream();
                opts.write(downloadFileBytes);
            }
            catch (Exception e)
            {
                DebugLogFactory.error(Tools.class, "writeFileBytes()  error", e);
            }
            finally
            {
                if (null != opts)
                {
                    opts.close();
                    opts = null;
                }
               
                if (null != out)
                {
                    out.close();
                    out = null;
                }
            }
        }
    }

posted @ 2013-08-03 12:12  嗨,你的益达~~~  阅读(400)  评论(0编辑  收藏  举报