第一种,写入流里返回

@RequestMapping(value = "/getVideo", method = RequestMethod.GET)
    public void getVido(HttpServletResponse response) {
        String file = "C:\\Users\\Boss\\Desktop\\123.avi";
        try {
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            String diskfilename = "final.avi";
            response.setContentType("video/avi");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + diskfilename + "\"");
            System.out.println("data.length " + data.length);
            response.setContentLength(data.length);
            response.setHeader("Content-Range", "" + Integer.valueOf(data.length - 1));
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Etag", "W/\"9767057-1323779115364\"");
            OutputStream os = response.getOutputStream();

            os.write(data);
            //先声明的流后关掉!
            os.flush();
            os.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

第二种,用 @GetMapping注解 指定返回格式

 @GetMapping(value = "getMusic/{musicid}", produces = "audio/mp3")
    public byte[] getMusic(@PathVariable String musicid) {
        log.info("请求接口 /story/Book/getBook/ 参数:{}", JSON.toJSONString(musicid));
        MusicFile musicFile = null;
        try {
            musicFile = mongodbMapper.selectMusicByName(musicid);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return musicFile.getContent().getData();
    }

produces的类型可以在下面的网址查看:
https://tool.oschina.net/commons

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐