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