加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:04:20 所属栏目:百科 来源:网络整理
导读:? ?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.cont
?

?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,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); ?} }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读