FLEX 读取图片

本文介绍在Flex项目中如何处理图片显示问题,由于直接使用路径可能导致安全沙箱问题,作者提出通过Java读取图片流并返回给Flex前台展示。详细介绍了Java读取文件流的代码以及Flex端加载图片的实现方法,包括调整图片大小的回调函数。
摘要由CSDN通过智能技术生成

在FLEX的项目开发中,我们经常会有上传图片的功能,一般是在数据库中建一张图片路径相关的table,讲图片上传到服务器上,路径保存在数据库中,但是flex 在前台显示图片是直接用路径显示《<mx:Image source="image/audTopLine.jpg" width="100%/>》,这种情况会出现访问不到服务器上的图片,安全沙箱的问题,想了很多都没有办法解决,想了很多办法,最终采取用流的方式来读取图片信息,可以直接将图片流存入数据库,但是这样会很浪费空间,可以还是存图片路径,利用Java来读取流返回到前台《

Java读取流的代码如下:

public class DownLoadImage extends HttpServlet {

 private static final long serialVersionUID = 3089316329737765177L;
  public  byte[] downLoad(String path) throws Exception {
  BufferedInputStream  bis = null;
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();  
  byte[] buff = new byte[2048];
  try {
            bis = new BufferedInputStream(new FileInputStream(path));         
           
         
            int bytesRead=-1;
            while((bytesRead=bis.read(buff))!=-1){
             buffer.write(buff,0,bytesRead);
            }
            buffer.flush(); 
        } catch(final IOException e) {
         e.printStackTrace();
        } catch(Exception e) {
         e.printStackTrace();
        }finally {
            if (bis != null)
                bis.close();
        }
        return buffer.toByteArray();
 
 }
 
// public static void main(String[] args) throws Exception{
//  DownLoadImage d=new DownLoadImage();
//  String path="F:/CK/DSC02180.JPG";
//  System.out.println(d.downLoad(path));
//  
// }
}》

,前台显示图片的方法如下

          var im:Image = new Image();
             var bmp:Loader = new Loader();
    bmp.loadBytes(file.BYTES);
    bmp.contentLoaderInfo.addEventListener(Event.COMPLETE,comp);
    im.addChild(bmp);
    image.source=im

 

  private function comp(e:Event):void{
     var bit:Bitmap=e.target.content;
     if(bit.height<400){
        image.height=bit.height;
        imageBox.height=bit.height;
     }else{
        bit.height=400;
        imageBox.height=400;
     }
     image.width=bit.width;
  }在回调函数comp中可以设置图片大小的相关信息!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值