您的位置:首页 > 数据库

Flex上传图片,将图片保存以二进制流的方式保存到数据库。

2011-11-16 10:00 309 查看
Java代码:

DTO中定义:

private byte[] employePhoto = null;

public byte[] getEmployePhoto() {

return employePhoto;

}

public void setEmployePhoto(byte[] employePhoto) {

if (null != employePhoto) {

this.employePhoto = employePhoto;

}

}

Impl中:

修改:将文件找到,再读取,并存入

方法一:使用DTO的字段直接修改,但注意如果不修改照片时,要确保DTO中的照片的值存在,否则将会将照片更新为空。

public boolean isHasFile(String photoName) throws Exception {

boolean ishasFile = false;

if (!photoName.equals("")) {

String filePath = FlexContext.getHttpRequest().getRealPath("/")

+ FlexContext.getServletContext().getInitParameter(

"uploadpath") + java.io.File.separator + photoName;

File file = new File(filePath);

int i = 0;

while (!file.exists() && i < 10) {

Thread.currentThread().sleep(1000);

i++;

}

if (!file.exists()) {

ishasFile = false;

} else {

ishasFile = true;

}

}

return ishasFile;

}

if (!photoName.equals("")) {

if (isHasFile(photoName)) {

String filePath = FlexContext.getHttpRequest().getRealPath("/")

+ FlexContext.getServletContext().getInitParameter(

"uploadpath") + java.io.File.separator

+ photoName;

File file = new File(filePath);

InputStream aInputStream = null;

try {

aInputStream = new FileInputStream(file);

try {

long length = file.length();

byte[] photobody = new byte[new Long(length).intValue()];

aInputStream.read(photobody, 0, photobody.length);

aDTO.setEmployePhoto(photobody);

} catch (Exception e) {

e.printStackTrace();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

try {

aInputStream.close();

file.delete();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

getSqlMapClientTemplate().update("empInfoItem.updata", aDTO);

<update id="updata" parameterClass="gds.ywda.common.dto.EmployeeDTO">

UPDATE FW_EMPLOYEE

SET WORKDEPID = #workDepID#,

EMPLOYEPHOTO =#employePhoto#,

WHERE EMPLOYEEID = #employeeID#

</update>

方法一:使用Map

if (!photoName.equals("")) {

AppSettingFactory appSettingFactory = AppSettingFactory.getInstance();

Map aMap = new HashMap();

aMap.put("aEmployeeID", aEmployeeID);

String filePath = FlexContext.getHttpRequest().getRealPath("/")

+ FlexContext.getServletContext().getInitParameter(

"uploadpath") + java.io.File.separator + photoName;

InputStream aInputStream = null;

File file = new File(filePath);

if (file.exists()) {

try {

aInputStream = new FileInputStream(file);

try {

long length = file.length();

byte[] photobody = new byte[new Long(length).intValue()];

aInputStream.read(photobody, 0, photobody.length);

aMap.put("aEmpPhoto", photobody);

getSqlMapClientTemplate().update("empInfoItem.modifyEmployeePhoto", aMap);

} catch (Exception e) {

e.printStackTrace();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

try {

aInputStream.close();

file.delete();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

<update id="modifyEmployeePhoto" parameterClass="java.util.HashMap">

UPDATE FW_EMPLOYEE SET EMPLOYEPHOTO=#aEmpPhoto# where employeeID=#aEmployeeID#

</update>

在查询的时候,照片要单独查出。

<resultMap class="java.util.HashMap" id="employeePhoto">

<result column="EMPLOYEPHOTO" property="EMPLOYEPHOTO" jdbcType="BLOB" />

</resultMap>

<!-- 得到员工照片 -->

<select id="findEmployeePhoto" parameterClass="java.util.HashMap" resultMap="employeePhoto">

SELECT EMPLOYEPHOTO FROM $shareDBName$.FW_EMPLOYEE WHERE EMPLOYEEID=#aEmployeeID#

</select>

@SuppressWarnings("unchecked")

public byte[] findEmployePhotoByID(String aEmployeeID) throws Exception {

AppSettingFactory appSettingFactory = AppSettingFactory.getInstance();

Map<String, String> aMap = new HashMap<String, String>();

aMap.put("aEmployeeID", aEmployeeID);

aMap.put("shareDBName", appSettingFactory.getAppSetting("shareDBName"));

java.util.HashMap employeePhoto = (java.util.HashMap) getSqlMapClientTemplate()

.queryForObject("empInfoItem.findEmployeePhoto", aMap);

if (employeePhoto.get("EMPLOYEPHOTO") != null

&& !"".equals(employeePhoto.get("EMPLOYEPHOTO"))) {

java.sql.Blob blob = (java.sql.Blob) employeePhoto

.get("EMPLOYEPHOTO");

return blob.getBytes(1, new Long(blob.length()).intValue());

} else {

return null;

}

}

Flex 端使用Jsp上传图片。

上传:

public var aFileReference:FileReference=new FileReference();

private var loader:Loader=new Loader();

private var byteArray:ByteArray;

private var bitmapData:BitmapData;

public var imageName:String="";

private function uploadPicLB_clickHandler(event:MouseEvent):void

{

try

{

var arr:Array=new Array();

arr.push(new FileFilter("*.jpg;*.gif;*.png", "*.jpg;*.gif;*.png"));

aFileReference.addEventListener(Event.SELECT, onSelect);

aFileReference.addEventListener(Event.COMPLETE, onComplete);

aFileReference.browse(arr);

}

catch (error:Error)

{

Alert.show(error.message)

}

}

private function onSelect(event:Event):void

{

aFileReference.load();

var date:Date=new Date();

imageName=date.fullYear.toString() + (date.month + 1).toString() + date.date.toString() + date.hours.toString() + date.minutes.toString() + date.seconds.toString() +
date.milliseconds.toString();

}

private function onComplete(event:Event):void

{

byteArray=aFileReference.data;

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);

loader.loadBytes(byteArray);

aFileReference.removeEventListener(Event.SELECT, onSelect);

aFileReference.removeEventListener(Event.COMPLETE, onComplete);

}

private function loaderCompleteHandler(event:Event):void

{

var bitmap:Bitmap=Bitmap(loader.content);

bitmapData=bitmap.bitmapData;

picture.source=bitmap;

isUpload=true;

}

保存到服务器:

var uploadURLImage:URLRequest=new URLRequest();

uploadURLImage.url="jsp/uploadEmpPhoto.jsp?picName=" + basicInfoHG.imageName;

photoName=basicInfoHG.imageName + basicInfoHG.aFileReference.type;

basicInfoHG.aFileReference.upload(uploadURLImage, basicInfoHG.aFileReference.name);

basicInfoHG.isUpload=false;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: