跳转到主要内容

预览文件

Avatar emanjusaka
统计中…
215
3
预览文件

预览文件

@RequestMapping(value = "/preview", method = RequestMethod.GET)
    public void preview(String fileUrl,
                        String fileName,
                        HttpServletResponse response) {
        InputStream inputStream = null;
        try {
            String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
            URL url = new URL(fileUrl);
            String contentType = URLConnection.guessContentTypeFromName(url.getFile());
            inputStream = url.openStream();
            // 设置响应头信息
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + encodedFileName);
            response.setBufferSize(1024 * 1024);
            // 将文件内容写入响应输出流中
            IOUtils.copyLarge(inputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("preview file error.", e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("preview file inputStream close error.", e);
            }
        }

    }

本文由 emanjusaka 创作,发表于 www.emanjusaka.com,采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

❋

微信扫码分享

微博
点赞是美意,赞赏是鼓励。如有帮助,可以关注下微信公众号。
微信 微信
支付宝 支付宝
查看赞赏名单 →
评论
← 上一篇 安卓中使用 kotlin 代码根据进程号获取进程名 下一篇 → Redisson 工具类