Flex 使用URLStream读取服务器端图片文件流并显示
2011-11-29 15:42
351 查看
在flex中显示服务器端的图片可以使用Image控件的source属性,但是这个条件必须是客户端可以访问到的有效的URL地址。如果图片存储在数据库中,直接使用source属性是无法显示图片的。这种情况下可以使用flex中的URLStream类来读取文件流,然后把文件显示到画面上。
第一步,创建一个URLStream对象,然后调用这个对象的load函数从服务器端读取图片文件的内容。
view plaincopy to clipboardprint?
private var stream:URLStream = new URLStream();
private function onShow():void
{
var downLoadURL:URLRequest = new URLRequest();
downLoadURL.url = "http://localhost:8080/LoadPicture/download";
stream.addEventListener(Event.COMPLETE, onLoadComplete);
stream.load(downLoadURL);
}
在取文件内容的时候,可以一个字节一个字节的取,当去到文件流的末尾时会抛出EOFError。
第三步,取到了文件内容之后就是把图片显示到画面上了,这里需要用到Loader类。使用Loader类的loadBytes函数,把图片内容放到Loader对象中,然后把Loader对象加到Image控件中就可以了。
服务器端的工作:
服务器端必须把图片文件准备还,然后在客户端请求数据的时候把文件流输出到客户端就可以了。
下面的例子是个java实现的。
view plaincopy to clipboardprint?
private void downLoad(HttpServletResponse response) throws Exception {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:/head.PNG"));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (bis != null)
bis.close();
if (bos != null)
{
bos.flush();
bos.close();
bos=null;
}
}
response.flushBuffer();
}
private void downLoad(HttpServletResponse response) throws Exception {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:/head.PNG"));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (bis != null)
bis.close();
if (bos != null)
{
bos.flush();
bos.close();
bos=null;
}
}
response.flushBuffer();
}
完整例子下载地址:
http://download.csdn.net/source/1815371
提示:使用本例的时候需要在c:/放置一个文件名为“head.PNG”的图片。
第一步,创建一个URLStream对象,然后调用这个对象的load函数从服务器端读取图片文件的内容。
view plaincopy to clipboardprint?
private var stream:URLStream = new URLStream();
private function onShow():void
{
var downLoadURL:URLRequest = new URLRequest();
downLoadURL.url = "http://localhost:8080/LoadPicture/download";
stream.addEventListener(Event.COMPLETE, onLoadComplete);
stream.load(downLoadURL);
}
view plaincopy to clipboardprint? private var stream:URLStream = new URLStream(); private function onLoadComplete(event:Event):void { if (stream.connected) { var b:int; try { do { b = stream.readByte(); bytes.writeByte(b); } while (true); } catch (e:EOFError) { Alert.show(bytes.length.toString()); } } var bmp:Loader = new Loader(); bmp.loadBytes(bytes); pic.addChild(bmp); } private var stream:URLStream = new URLStream(); private function onLoadComplete(event:Event):void { if (stream.connected) { var b:int; try { do { b = stream.readByte(); bytes.writeByte(b); } while (true); } catch (e:EOFError) { Alert.show(bytes.length.toString()); } } var bmp:Loader = new Loader(); bmp.loadBytes(bytes); pic.addChild(bmp); }
在取文件内容的时候,可以一个字节一个字节的取,当去到文件流的末尾时会抛出EOFError。
第三步,取到了文件内容之后就是把图片显示到画面上了,这里需要用到Loader类。使用Loader类的loadBytes函数,把图片内容放到Loader对象中,然后把Loader对象加到Image控件中就可以了。
服务器端的工作:
服务器端必须把图片文件准备还,然后在客户端请求数据的时候把文件流输出到客户端就可以了。
下面的例子是个java实现的。
view plaincopy to clipboardprint?
private void downLoad(HttpServletResponse response) throws Exception {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:/head.PNG"));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (bis != null)
bis.close();
if (bos != null)
{
bos.flush();
bos.close();
bos=null;
}
}
response.flushBuffer();
}
private void downLoad(HttpServletResponse response) throws Exception {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:/head.PNG"));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (bis != null)
bis.close();
if (bos != null)
{
bos.flush();
bos.close();
bos=null;
}
}
response.flushBuffer();
}
完整例子下载地址:
http://download.csdn.net/source/1815371
提示:使用本例的时候需要在c:/放置一个文件名为“head.PNG”的图片。
相关文章推荐
- Flex 使用URLStream读取服务器端图片文件流并显示
- ASP.NET把图片存入数据库和使用文件流读取显示(转)
- ASP.NET把图片存入数据库和使用文件流读取显示(转)
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- 使用OpenCV 读取图片 ,再用OpenGL显示,图像有错位
- FLEX 初学笔记之一,使用HTTPService读取Data.xml文件,显示于表示层
- Flex的Image控件中如何在读取图片文件错误时显示一个tool tip的例子
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- 安卓实训第六天:首先使用AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- 【opencv 官方教程】翻译1 基本安装使用 编译工程 读取、显示、保存图片
- flex :读取access数据库数据 转化为xml 然后在flex页面显示绑定图片
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- Android手机开发:ImageView使用和从内存读取图片显示
- 使用文件流,读取图片二进制节码!
- Qt读取本地图片使用halcon读取并显示
- Python中使用pickle Image等进行图片序列化及读取恢复显示图片
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- android问题:从list集合中循环读取图片地址,使用异步下载,利用baseAdapter怎样显示到listView中