分享

Flex存储图片到数据库的例子

 firefly87 2012-10-24
版论坛全民公测! 2012年10月当选微软MVP的CSDN会员名单! JavaEE Web应用开发平台WebBuilder团队专访

Flex存储图片到数据库的例子

1352人阅读评论(1)收藏举报

As代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.ImageSnapshot;
import flash.display.BitmapData;
import mx.controls.Image;
private var image:FileReference;
//保存图片信息到数据库
private function saveData():void{
var bmp:BitmapData = ImageSnapshot.captureBitmapData(img);
var i:Bitmap = new Bitmap(bmp);
img.source = i;
var ba:ByteArray = new JPEGEncoder().encode(i.bitmapData);
save.putimg(ba);
}
//保存成功后的返回结果处理方法
private function saveResult(event:ResultEvent):void{
Alert.show("save is succeed !");
}
//初始化
private function init():void{
image = new FileReference();
image.addEventListener(Event.SELECT,onSelectImage);
image.addEventListener(Event.COMPLETE,onCompleteImage);
}
//选择图片将图片上传到服务器
private function onSelectImage(e: Event):void{
try{image.upload(new URLRequest("UploadFile"));}catch(error:Error){};
}
//上传完毕进行预览
private function onCompleteImage(e: Event):void{
img.source = encodeURI("uploadFolder/"+image.name);
}
//请求显示数据库中的二进制图片
internal function onClick(evt:MouseEvent):void{
imgserver.blobRead();
}
//将从数据库获取的二进制图片显示出来
internal function GetOneManHuaTuPian_onResult(evt:ResultEvent):void{
img.source = (evt.result as ByteArray);
}
]]>
</mx:Script>
<mx:RemoteObject id="save" destination="saveImg" result="saveResult(event)" />
<mx:RemoteObject id="imgserver" destination="ImageService" >
<mx:method name="blobRead" result="GetOneManHuaTuPian_onResult(event)"/>
</mx:RemoteObject>
<mx:Image id="img" x="25" y="10" width="116" height="122" >
<mx:source>6.jpg</mx:source>
</mx:Image>
<mx:Button x="149" y="10" label="浏览..." click="image.browse()"/>
<mx:Button x="149" y="108" label="保存" click="saveData()" width="64"/>
<mx:Button x="149" y="56" label="显示" click="onClick(event)" width="64"/>
</mx:Application>

JAVA代码:

import java.io.*;
import java.sql.*;
import com.qiansoft.util.ByteImage;
public class PutImg {
public void putimg(byte img []) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=TestImg";
Connection conn = DriverManager.getConnection(url,"sa","876010");
Statement stmt = conn.createStatement();
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
sql = "INSERT INTO imgtable (img) VALUES (?)";
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, ByteImage.getStreamFromByte(img), (int) img.length);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

ByteImage代码:

import java.io.*;
import java.sql.*;

public class ByteImage {
//用于将从数据库中读取的二进制文件流转换为byte数组
public static byte [] getByteFromStream(InputStream is){
byte[] b = new byte [1];
try {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
//创建数据读取缓存byte数组
byte[] buffer = new byte[2048];
int temp;
if(is == null)
return null;
temp = is.read(buffer);
while (temp != -1) {
bytestream.write(buffer, 0, temp);
temp = is.read(buffer);
}
//将ByteArrayOutputStream转换为二进制数组
b = bytestream.toByteArray();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
public static InputStream getStreamFromByte(byte img []){
return new ByteArrayInputStream(img);
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约