首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >spring mvc 返回图片的请求

spring mvc 返回图片的请求

作者头像
似水的流年
发布于 2018-01-18 16:29:20
发布于 2018-01-18 16:29:20
1.8K0
举报
文章被收录于专栏:电光石火电光石火

功能分析:

要使用Spring MVC来处理返回一个图片的请求。这个跟servlet返回的实现是一样的。

代码如下:

代码语言:javascript
复制
@RequestMapping(value = "/image/get")
public void getImage(HttpServletRequest request,HttpServletResponse response) {
    FileInputStream fis = null;
    response.setContentType("image/gif");
    try {
        OutputStream out = response.getOutputStream();
        File file = new File("D:"+File.separator+"timg.jpg");
        fis = new FileInputStream(file);
        byte[] b = new byte[fis.available()];
        fis.read(b);
        out.write(b);
        out.flush();
    } catch (Exception e) {
         e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
               fis.close();
            } catch (IOException e) {
	        e.printStackTrace();
	    }   
          }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档