流读取硬盘图片展示到web页面

来源:互联网 发布:淘宝新势力周9月 编辑:程序博客网 时间:2024/06/03 03:58


页面:

<img src="${ctx}/img/readImg?prizeid=图片名字" /> 


后台:

只需要传入图片名称即可,

//prizeid 要下载的图片名字

public void readImg(String prizeid) {

FileInputStream fis= null;
OutputStream out=null;
        try {

            out = response.getOutputStream();

//ResultStatic.path+prizeid+".jpg"  图片绝对地址

            fis = new FileInputStream(new File(ResultStatic.path+prizeid+".jpg"));
            byte[] buffer = new byte[1024*8];
            //读取文件流  
            int len = 0;  
            while ((len = fis.read(buffer)) != -1){  
                out.write(buffer,0,len);  
            }  
            out.flush();
        } catch (Exception e) {
             e.printStackTrace();
        } finally {
        if(fis != null){
        try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
            }
            if(out != null){
            try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
            }
        }
}
阅读全文
0 0
原创粉丝点击