zoukankan      html  css  js  c++  java
  • 常用下载文件的公共方法

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

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

  • 相关阅读:
    Java ,python面向对象的继承及其区别
    谈谈我理解的敏捷开发
    Centos7 升级python3,解决升级后不兼容问题
    Django + Uwsgi + Nginx 的生产环境部署
    理解Python协程:从yield/send到yield from再到async/await
    【转】Python 数据库连接池
    为何GET只发一次TCP连接,POST发两次TCP连接
    Python简单密码加密程序,加盐(salt)md5
    spring学习(三) ———— spring事务操作
    spring学习(二) ———— AOP之AspectJ框架的使用
  • 原文地址:https://www.cnblogs.com/qqzy168/p/3214754.html
Copyright © 2011-2022 走看看