通过接口返回文件流

java接口返回文件流:预览,下载

   
   @GetMapping("/download/{filename}")
    public void test(@PathVariable("filename") String filename, HttpServletResponse response) {
        //文件路径
        String filepath = "";
        try {
            File file = new File(filepath + filename);
            FileInputStream inputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream();

            //响应文件格式
            response.setContentType(this.getContentType(this.getSuffix(filename)));
            response.setContentLengthLong(file.length());

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                //读取输出流
                outputStream.write(bytes, 0, len);
            }
            outputStream.flush(); //刷新
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

部分文件后缀对应的content-type

    public String getContentType(String suffix) {
        String contentType = "";
        //转小写
        switch (suffix.toLowerCase()) {
            case "txt":
                contentType = "text/plain";
                break;
            case "html":
                contentType = "text/html";
                break;
            case "css":
                contentType = "text/css";
                break;
            case "js":
                contentType = "text/javascript";
                break;
            case "json":
                contentType = "application/json";
                break;
            case "xml":
                contentType = "application/xml";
                break;
            case "jpeg":
            case "jpg":
                contentType = "image/jpeg";
                break;
            case "png":
                contentType = "image/png";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "mp3":
                contentType = "audio/mpeg";
                break;
            case "wav":
                contentType = "audio/wav";
                break;
            case "ogg":
                contentType = "audio/ogg";
                break;
            case "mp4":
                contentType = "video/mp4";
                break;
            case "webm":
                contentType = "video/webm";
                break;
            case "pdf":
                contentType = "application/pdf";
                break;
           case "tiff":
                contentType = "image/tiff";
                break;
            default:
                contentType = "application/octet-stream";
                break;
        }
        return contentType;
    }

        //获取文件后缀
    public String getSuffix(String filename) {
        int dotIndex = filename.lastIndexOf(".");
        return filename.substring(dotIndex + 1);
    }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值