zoukankan      html  css  js  c++  java
  • 使用ssh向sqlserver2005数据库中保存image类型的二进制图片

    1.首先设计数据库表,其中photo、photo2字段均为image类型的。

    2.建立实体bean对象,设置两个字段为byte[]如:private byte[] photo; private byte[] photo2;

    3.建立hibernate与数据库的映射文件hbm.xml,其中photo、photo2字段的映射如下:

      <property generated="never" lazy="false" name="photo" type="binary">
       <column name="photo"/>
      </property>
       <property generated="never" lazy="false" name="photo2" type="binary">
       <column name="photo2"/>
      </property>

    4.建立前台上传选择文件的页面:

        <form id="inputForm" action="./saveStudent.action" method="post" enctype="multipart/form-data">

                 <tr>  
                    <th class="tdtitle">上传图片:</th>
                    <td class="tdcell"><input type="file" id="file" name="file" value="浏览"/></td>  
                </tr>
                <tr>  
                    <th class="tdtitle">上传图片:</th>
                    <td class="tdcell"><input type="file" id="file2" name="file2" value="浏览"/></td>  
                </tr>

      </form>

    5.在后台action中创建File对象file,并写set、get方法。在save方法中作如下处理(将输入流转换为byte数组):

    InputStream inputStream=null;
    inputStream = new FileInputStream(file);
    byte bytes[]=new byte[inputStream.available()];
    inputStream.read(bytes);
    inputStream.close();
    inputStream=new FileInputStream(file2);
    byte bytes2[]=new byte[inputStream.available()];
    inputStream.read(bytes2);
    inputStream.close();
    price.setPhoto(bytes);
    price.setPhoto2(bytes2);

    在显示的函数中做如下处理:(从数据库中取得二进制数据,转换为输出流)

    byte [] buf=null;
    if(pro.equals("1")){
    buf=list.get(0).getPhoto();
    }else{
    buf=list.get(0).getPhoto2();
    }
    response.setContentType("image/jpg");
    OutputStream outputStream=response.getOutputStream();
    outputStream.write(buf);
    outputStream.flush();

    在显示的前台页面作如下处理:

    <tr><td>图片1:</td><td><img src="<%=basePath %>getImageStudent.action?no=<s:property value="student.no"/>&&kind=1"/></td></tr>
     <tr><td>图片2:</td><td><img src="<%=basePath %>getImageStudent.action?no=<s:property value="student.no"/>&&kind=2"/></td></tr>

  • 相关阅读:
    JavaScript打造很酷的图片放大效果实例代码
    【荐】CSS实现的鼠标点击小图无刷新放大图片代码
    JavaScript+CSS实现的文字幻灯切换代码
    【荐】很棒的图片友情链接带控制按钮的横向滚动代码
    jquery制作一个漂亮带渐隐效果的跑动区域
    JS打造的一款响应鼠标变化很炫的图片特效代码
    JS+CSS控制鼠标移上图片滑出文字提示代码
    Jquery+CSS实现的大气漂亮图片切换效果代码
    【荐】JS+CSS防FLASH效果带倒影的图片切换效果代码
    JavaScript限制对图片右键代码
  • 原文地址:https://www.cnblogs.com/pangblog/p/3398158.html
Copyright © 2011-2022 走看看