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

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

发布时间:2020-12-15 01:04:22 所属栏目:百科 来源:网络整理
导读:? Java代码: DTO中定义: ? private byte[] employePhoto = null; ? ? public byte[] getEmployePhoto() { ? return employePhoto; ? } ? ? public void setEmployePhoto(byte[] employePhoto) { ? if (null != employePhoto) { ? ? this.employePhoto = em
?

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,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,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)
??????????????? {
????????????

(编辑:李大同)

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

    推荐文章
      热点阅读