JAVA获取文件byte数组并输出进行展示和文件下载


 

 

 /**
     * 文件下载
     */
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response) {
        //文件路径
        try {
            //文件字节流 JAVA将文件转换成byte数组(byte[]) 参考:https://www.cnblogs.com/pxblog/p/14919037.html
            byte[] file = new byte[1024];
            response.setContentType("application/vnd.ms-word;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=\"" + new String("文件名包含文件后缀".getBytes("gb2312"), "ISO8859-1"));
            OutputStream out = response.getOutputStream();
            out.write(file);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

 

如果只是要请求url进行图片展示,只需要在上面去掉

 response.setContentType("application/vnd.ms-word;charset=utf-8");
                response.setHeader("Content-Disposition",
                        "attachment;filename=\"" + new String(versionPO.getVersionFileName().getBytes("gb2312"), "ISO8859-1"));
                

这部分代码即可


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM