0
点赞
收藏
分享

微信扫一扫

后端文件流传输文件内容到前端

幸甚至哉歌以咏志 2022-04-24 阅读 69
java
/**
     * 传文件到web页面,编码为UTF-8
     *
     * @param response
     * @param fileFullPath
     * @param response
     * @throws IOException
     */
    public void getFileContent(String fileFullPath, HttpServletResponse response) throws IOException {
        String[] split = fileFullPath.split("\\\\");
        if (split.length < 2) {
            split = fileFullPath.split("/");
        }
        response.setCharacterEncoding("UTF-8");
        // attachment是以附件的形式下载,inline是浏览器打开
        response.setHeader("Content-Disposition", "inline;filename=" + split[split.length - 1] + ".txt");
        response.setContentType("text/plain");
        // 把二进制流放入到响应体中
        ServletOutputStream os = response.getOutputStream();
        File file = new File(fileFullPath);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        os.write(bytes);
        os.flush();
        os.close();
    }

页面中文可能会乱码,待处理

举报

相关推荐

0 条评论