文件下载时使用的工具类


    public static void downLoadFileByByte(HttpServletRequest request, HttpServletResponse response, byte[] b, String fileName) throws IOException {
        OutputStream outp = response.getOutputStream();
        if (b.length > 0) {
            response.setContentType("APPLICATION/OCTET-STREAM");
            String userAgent = request.getHeader("USER-AGENT");
            String finalFileName=null;
            try {
                if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){//IE 浏览器
                    finalFileName = URLEncoder.encode(fileName,"UTF8");
                }else{//火狐,google等其他浏览器
                    finalFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
                }
                response.setHeader("Content-Disposition", "attachment; filename=\"" + finalFileName + "\"");//\" 解决Firefox下载英文+中文组合的文件名的问题
            } catch (UnsupportedEncodingException e) {
            }
            outp.write(b);
        } else {
            outp.write("文件不存在!".getBytes("utf-8"));
        }

        if (outp != null) {
            outp.close();
            outp = null;
            response.flushBuffer();
        }

    }

    public static void downLoadFile(HttpServletRequest request, HttpServletResponse response, String fullPath, String fileName) throws IOException {
        OutputStream outp = response.getOutputStream();
        File file = new File(fullPath);
        if (file.exists()) {
            response.setContentType("APPLICATION/OCTET-STREAM");
            String userAgent = request.getHeader("USER-AGENT");
            String enableFileName = null;
            try {
                if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){//IE 浏览器
                    enableFileName = URLEncoder.encode(fileName,"UTF8");
                }else{//火狐,google等其他浏览器
                    enableFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
                }
                response.setHeader("Content-Disposition", "attachment; filename=\"" + enableFileName + "\"");//\" 解决Firefox下载英文+中文组合的文件名的问题
            } catch (UnsupportedEncodingException e) {
            }
            FileInputStream in = null;

            try {
                outp = response.getOutputStream();
                in = new FileInputStream(fullPath);
                byte[] b = new byte[1024];
                boolean var10 = false;

                int i;
                while((i = in.read(b)) > 0) {
                    outp.write(b, 0, i);
                }

                outp.flush();
            } catch (Exception var14) {
                var14.printStackTrace();
            } finally {
                if (in != null) {
                    in.close();
                    enableFileName = null;
                }

                if (outp != null) {
                    outp.close();
                    outp = null;
                    response.flushBuffer();
                }

            }
        } else {
            outp.write("File does not exist!".getBytes("utf-8"));
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值