在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中可以设置图片大小的相关信息!!!
本文介绍在Flex项目中如何处理图片显示问题,由于直接使用路径可能导致安全沙箱问题,作者提出通过Java读取图片流并返回给Flex前台展示。详细介绍了Java读取文件流的代码以及Flex端加载图片的实现方法,包括调整图片大小的回调函数。

5556

被折叠的 条评论
为什么被折叠?



