在SpringBoot中使用@ResponseBody实现返回图片功能-创新互联

本篇文章为大家展示了在SpringBoot中使用@ResponseBody实现返回图片功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

站在用户的角度思考问题,与客户深入沟通,找到盱眙网站设计与盱眙网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站建设、网站设计、企业官网、英文网站、手机端网站、网站推广、域名申请、网页空间、企业邮箱。业务覆盖盱眙地区。

首先写一个Controller类,包括一个方法,如下:


package com.example.demo.common;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;

@RestController
@RequestMapping(value="/api/v1")
public class ImageTest {

  @GetMapping(value = "/image",produces = MediaType.IMAGE_JPEG_VALUE)
  @ResponseBody
  public byte[] test() throws Exception {

    File file = new File("E:\\ce\\1.jpg");
    FileInputStream inputStream = new FileInputStream(file);
    byte[] bytes = new byte[inputStream.available()];
    inputStream.read(bytes, 0, inputStream.available());
    return bytes;

  }

}

分享题目:在SpringBoot中使用@ResponseBody实现返回图片功能-创新互联
本文地址:http://myzitong.com/article/dppeoo.html