struts2 mybatis orcale上传和显示图片

本文介绍了一种使用MyBatis实现图片上传和显示的方法。通过Java代码将文件读取为byte[]并存储到数据库中,利用MyBatis的BlobTypeHandler处理Blob类型的数据。在显示图片时,从数据库中读取Blob数据并转化为流,直接在浏览器中展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上传时是将流转为byte[]  mybatias将byte[]  转为blob

显示时将blob转为流

---------------jsp----

<input type="file" id="picpath" name="upload"  />

--显示

<img src="<%=basePath %>res_getPhotoById.action"/> 

--------java上传-------

  private File upload; 
     private String uploadContentType; 
     private String uploadFileName; 

public File getUpload() { 
        return upload; 
    } 
 
    public void setUpload(File upload) { 
        this.upload = upload; 
    } 
 
    public String getUploadContentType() { 
        return uploadContentType; 
    } 
 
    public void setUploadContentType(String uploadContentType) { 
        this.uploadContentType = uploadContentType; 
    } 
 
    public String getUploadFileName() { 
        return uploadFileName; 
    } 
 
 
    public void setUploadFileName(String uploadFileName) { 
        this.uploadFileName = uploadFileName; 
    } 
 

 

 

 

--上传---

 FileInputStream fis = new FileInputStream(upload); 
      byte[] b =new  byte[fis.available()];
      fis.read(b);
      registerInforService.updatePhotoDate(b);
         fis.close();

--------------显示-----------------

 public void getPhotoById () throws Exception{ 
  
  Map<String,Object> map=registerInforService.findpic();
  
  InputStream in = ((Blob) map.get("pic")).getBinaryStream() ;
     response.setContentType("image/jpg"); 
        response.setCharacterEncoding("UTF-8"); 
     OutputStream outputSream = response.getOutputStream(); 
      int len = 0; 
      byte[] buf = new byte[1024]; 
      while ((len = in.read(buf, 0, 1024)) != -1) { 
          outputSream.write(buf, 0, len);
      } 
      outputSream.close(); 
  } 

------mapper----

/**图片处理*/
  public void updatePhotoDate( @Param("pic") byte[] photoDate);

 public Map<String,Object> selPhotoDate()throws Exception;

---------------------xml--------------------------

 <resultMap type="map" id="photoMapper_resultMap_photoEntity">
  <id property="id" column="id" /> 
    <result property="pic" column="pic"  />
 </resultMap> 

 

<update id="updatePhotoDate" >
  UPDATE REGISTER_INFOR TAB
  <set>
   TAB.pic=#{pic, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}
  </set>
 </update>

 

 

<select id="selPhotoDate" resultMap="photoMapper_resultMap_photoEntity"> 
  SELECT ID,PIC FROM REGISTER_INFOR TAB
 </select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值