SpringBoot 返回图片、Excel、音视频等流数据几种处理方式

方式一:直接针对响应对象(response)实现

@RestController
@Slf4j
@Api(tags = SwaggerConfig.TAG_IMAGE)
@RequestMapping(SwaggerConfig.TAG_IMAGE)
public class ImageController {
    @GetMapping(value = "/getImage")
    @ApiOperation("获取图片-以ImageIO流形式写回")
    public void getImage(HttpServletResponse response) throws IOException {
        OutputStream os = null;
        try {
            // 读取图片
            BufferedImage image = ImageIO.read(new FileInputStream(new File("F:\\谷歌下载\\未命名文件.png")));
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (IOException e) {
            log.error("获取图片异常{}",e.getMessage());
        } finally {
            if (os != null) {
                os.flush();
                os.close();
 

你可能感兴趣的:(java,项目遇到问题总结,spring,boot,后端,附件处理)