下载文件公用代码

 

    public static void download(HttpServletResponse response, String fileUrl) {
        try {
            File file=new File(fileUrl);
            String filename=file.getName();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(fileUrl));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();

            response.setContentType("application/octet-stream;charset=UTF-8");
            String fileName = new String(filename.getBytes("gb2312"), "iso8859-1");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            OutputStream ouputStream = response.getOutputStream();
            ouputStream.write(buffer);
            ouputStream.flush();
            ouputStream.close(); } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

最后修改于 2020-04-30 09:57:41
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇