博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用下载文件的公共方法
阅读量:4982 次
发布时间:2019-06-12

本文共 2071 字,大约阅读时间需要 6 分钟。

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

/**

     * 读取磁盘指定路径的文件,返回字节数组(可以写入输出流用来下载文件)
     * @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;
                }
            }
        }
    }

转载于:https://www.cnblogs.com/qqzy168/p/3214754.html

你可能感兴趣的文章
楼房重建(分块优化)
查看>>
斐波那契数列(矩阵加速递推)
查看>>
HTTP笔记之一
查看>>
Gradle 学习一
查看>>
hiho #1223 不等式
查看>>
EOS多节点同步代码分析
查看>>
Synchronized关键字
查看>>
webfont 字体
查看>>
lua快速入门
查看>>
FullCalendar 官方文档翻译
查看>>
plsql 操纵表数据的2种方式
查看>>
输出日期
查看>>
hibernate中实体与数据库中属性对应的类型
查看>>
多线程池以及futures python新的线程包
查看>>
3389无法连接的5种原因分析
查看>>
C++拾遗(三)关于复合类型
查看>>
理解mvc
查看>>
WCF入门简单教程(图文) VS2010版
查看>>
jQuery EasyUI API 中文文档 - ComboBox组合框
查看>>
“ORA-12545: 因目标主机或对象不存在,连接失败”怎么办?
查看>>