java 触发http浏览器下载 具体实现

方式一:

@RequestMapping("mydownload")

    public ResponseEntity download(HttpServletResponse response) throws IOException {

        String path = "D:\\111.ts";

        File file = new File(path);

        long size = file.length();

        //为了解决中文名称乱码问题 这里是设置文件下载后的名称        String fileName = new String("000.ts".getBytes("UTF-8"), "iso-8859-1");

        response.reset();

        response.setHeader("Accept-Ranges", "bytes");

        //设置文件下载是以附件的形式下载        response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));

        response.addHeader("Content-Length", String.valueOf(size));

        ServletOutputStream sos = response.getOutputStream();

        FileInputStream in = new FileInputStream(file);

        BufferedOutputStream outputStream = new BufferedOutputStream(sos);

        byte[] b = new byte[1024];

        int i = 0;

        while ((i = in.read(b)) > 0) {

            outputStream.write(b, 0, i);

        }

        outputStream.flush();

        sos.close();

        outputStream.close();

        in.close();

        return new ResponseEntity(HttpStatus.OK);

    }


方式二:

//文件下载相关代码 与上面的方法类似 @RequestMapping("/download")

    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {

        String fileName = "111.mov";

        if (fileName != null) {

            //当前是从该工程的WEB-INF//File//下获取文件(该目录可以在下面一行代码配置)            String realPath = request.getServletContext().getRealPath(

                    "WEB-INF/File/");

            File file = new File(realPath, fileName);

            if (file.exists()) {

                // 设置强制下载不打开                response.setContentType("application/force-download");

                // 设置文件名                response.addHeader("Content-Disposition",

                        "attachment;fileName=" + fileName);

                byte[] buffer = new byte[1024];

                FileInputStream fis = null;

                BufferedInputStream bis = null;

                try {

                    fis = new FileInputStream(file);

                    bis = new BufferedInputStream(fis);

                    OutputStream os = response.getOutputStream();

                    int i = bis.read(buffer);

                    while (i != -1) {

                        os.write(buffer, 0, i);

                        i = bis.read(buffer);

                    }

                    System.out.println("success");

                } catch (Exception e) {

                    e.printStackTrace();

                } finally {

                    if (bis != null) {

                        try {

                            bis.close();

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    }

                    if (fis != null) {

                        try {

                            fis.close();

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    }

                }

            }

        }

        return null;

    }


主要原理触发浏览器下载,设置response 响应头为response.setHeader("Content-disposition","attachment; filename=testsprint2.xlsx");

然后通过response.getOutputStream 去写入文件的相应字节

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容