从工程外读取图片,并显示

来源:互联网 发布:英文网络用语缩写 编辑:程序博客网 时间:2024/05/18 20:08

前台:

<img id="editImg_'+(i+1)+'" alt=""  src="xxxController_loadImage?imageid='+url+'" style="width:100px;height: 100px">


后台:

@RequestMapping({"/xxxController_loadImage"})
public void out(){
FileInputStream fileInputStream=null;
OutputStream outputStream=null;
   
String fileid = request.getParameter("imageid");    // 获取文件的文件名
File file =new File(ResourceBundle.getBundle("config").getString("deadbeat_dir")+fileid);
if(file.exists()){
try {
fileInputStream = new FileInputStream(file); // 
   int i=fileInputStream.available(); //得到文件大小
   byte data[]=new byte[i];


fileInputStream.read(data);

   response.setContentType("image/*"); //设置文件类型
   outputStream=response.getOutputStream(); //输出文件
   outputStream.write(data);  //输出数据
       
   } catch (IOException e) {
    this.logger.error(e.toString(), e);
} finally{
try {//关闭
outputStream.flush();
outputStream.close();   
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
this.logger.error(e.toString(), e);
}   
  
}
}
}

阅读全文
0 0
原创粉丝点击