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

SSH+SQLServer2005图片文件上传及页面显示

发布时间:2020-12-12 14:44:54 所属栏目:MsSql教程 来源:网络整理
导读:作为一个常用的功能,自己做个笔记,免得到时候又找百度。 1、图片文件的上传,这个和普通文件的上传是一样的,从页面选择图片文件到数据传至Action的操作,之前的日记中已经说明过,不再赘述,主要说怎样将图片文件存入数据库以及从数据库中读出来显示在页

作为一个常用的功能,自己做个笔记,免得到时候又找百度。

1、图片文件的上传,这个和普通文件的上传是一样的,从页面选择图片文件到数据传至Action的操作,之前的日记中已经说明过,不再赘述,主要说怎样将图片文件存入数据库以及从数据库中读出来显示在页面的问题。

2、图片文件存入数据库,SQLServer2005数据库中字段定义为:image,oracle中可以使用blob类型,mysql中可以使用blob或者longblob等类型。

? 2.1、图片实体类定义:?

??? @Entity
??? @Table(name = "OBJ_PHOTO")
??? public class Photo extends DomainObject {
??????? @Column(name = "photoName",nullable = false)
??????? private StringphotoName;

??????? @Column(name = "photoAddr",nullable = false)
??????? private byte[]photoAddr;? // 图片文件定义为byte[]

??????? // get和set方法。。。。
??? }

?2.2、将图片存入数据库:

??? Photophoto = newPhoto();
??? InputStream in = null;
??? if (StringUtils.isNotEmpty(formBean.getPhotoAddrFileName())) {
??????? try {
??????????? byte buffer[] = new byte[(int)formBean.getPhotoAddr().length()];
??????????? in = new FileInputStream(formBean.getPhotoAddr());
??????????? in.read(buffer);
??????????? photo.setPhotoAddr(buffer);
??????? } catch (FileNotFoundException e) {
??????????? e.printStackTrace();
??????? } catch (IOException e) {
??????????? e.printStackTrace();
??????? }
??? }
??? photo.setPhotoName(formBean.getPhotoName());
??? photoDao.save(photo);

??3、图片存入数据库之后如何显示在页面上:

??3.1、首先在Action中定义一个InputStream对象:

???private InputStream imagStream;

???// get和set方法。。。

??3.2、Action中定义一个方法:

???public String getPhoto(){
???????String id = request.getParameter("id");
???????Photo?Photo= mapService.getMapById(id);?
???????return "photoImag";

???}

??? 3.3、struts.xml文件配置:

??? <result name="photoImag" type="stream">
??????? <param name="contentType">image/jpeg,image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
??????? <param name="inputName">imagStream</param>
??? </result>

??? 3.4、jsp页面如何显示图片:

???<img id="preview" src="photoAction!getPhoto.action?id=<s:property value='id'/>" />?// 此处传入该图片文件的ID,调用Action中的方法getPhoto即可在页面显示图片

(编辑:李大同)

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

    推荐文章
      热点阅读