上传时是将流转为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>