动态生成img的链接

来源:互联网 发布:淘宝话费充值利润 编辑:程序博客网 时间:2024/06/12 06:58

前言

要实现的就是这样的效果:

<img src="/img/182034" />  

可以看到,这里链接不再是指定某一个图片的具体位置。这种情况在图片不是保存在文件系统时很有用,比如,保存在nosql数据库里。

实现

图片控制器:

@Controller@RequestMapping("/")public class WebController {    @RequestMapping(method=RequestMethod.GET,path="/img/{imgNo}")    public void getImg(HttpServletResponse response,int index) throws IOException{          //读取图片文件到一个byte[]        File file = new File("C:\\Users\\luchu\\Desktop\\QQ截图20160410192529.jpg");        OutputStream stream = response.getOutputStream();        FileInputStream reader = new FileInputStream(file);        byte[] data= new byte[ (int) file.length()];        reader.read(data);        reader.close();        //记得设置ContentType        response.setContentType("image/png");        stream.write(data);        stream.flush();    }}

这里只是模拟,就从本地拿数据。

0 0