springBoot优雅返回图片/网页到浏览器

您所在的位置:网站首页 springboot返回图片数据 springBoot优雅返回图片/网页到浏览器

springBoot优雅返回图片/网页到浏览器

2023-05-28 14:39| 来源: 网络整理| 查看: 265

springBoot优雅返回图片/网页到浏览器

一、普通spring mvc返回图片或网页到浏览器

@Controller @RequestMapping(value = "/image") public class ImageController { @RequestMapping(value = "/get") @ResponseBody public void getImage(HttpServletResponse response) throws IOException { File file = new File("D:/test.jpg"); FileInputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[inputStream.available()]; response.setContentType("image/jpeg"); OutputStream out = response.getOutputStream(); out.write(result); out.flush(); //关闭响应输出流 out.close(); } }

 

 

 

二、spring boot

import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @Controller @RequestMapping(value = "/image") public class ImageController { @RequestMapping(value = "/get",produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public byte[] getImage() throws IOException { File file = new File("D:/test.jpg"); FileInputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes, 0, inputStream.available()); return bytes; } }

如果是网页的话,

produces = MediaType.TEXT_HTML

posted on 2019-05-17 15:57  myf008  阅读(11825)  评论(0)  编辑  收藏  举报



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3