oracle数据库对blob类型数据的存和取

//图片的存入

@SuppressWarnings("serial")
public class AddPhotoToAlbumAction extends ActionSupport {
 private Photo photo;
 private File address;//图片文件,通过<input class="text" type="file" name="address" />传入
 private String addressFileName;//图片文件的后缀名,必须以命名"文件FileName"的形式命名
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  //获取该文件的后缀名
  String path = addressFileName.substring(addressFileName.indexOf(".")+1);
  photo.setContentType(path);
  //获取字节形式的文件
  byte[] rtnByContent = getPhotoContentInByte();
  //使用hibernate存入数据库
  Blob bC = Hibernate.createBlob(rtnByContent);
  photo.setContent(bC);
 }
 /**
  * 以缩略图的形式存储
  * @return
  */
 private byte[] getPhotoThumbnailInByte(){
  byte[] rtn = null;
  try{
   Image src = ImageIO.read(address);
   int oldWidth = src.getWidth(null);
   int oldHeight = src.getHeight(null);
   float divWidth = 200f;
   int newWidth = 200;
   int newHeight = 0;
   float tmp;
   if(oldWidth > newWidth){
    tmp = oldWidth / divWidth;
    newWidth = Math.round(oldWidth / tmp);
    newHeight = Math.round(oldHeight / tmp);
   }else{
    newWidth = oldWidth;
    newHeight = oldHeight;
   }
   int imageHeight = 100;
   int imageWidth = 200;
   BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
   Graphics2D graphics2D = (Graphics2D)bufferedImage.createGraphics();
   graphics2D.setBackground(Color.WHITE);
   graphics2D.clearRect(0, 0, imageWidth, imageHeight);
   bufferedImage.getGraphics().drawImage(src, 0, 0, newWidth, newHeight, null);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   BufferedOutputStream bos = new BufferedOutputStream(baos);
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
   encoder.encode(bufferedImage);
   rtn = baos.toByteArray();
   bos.close();
   baos.close();
  }catch(Exception ex){
   
  }
  return rtn;
 }
 
 /**
  * 以正常模式存储
  * @return
  * @throws IOException
  */
 private byte[] getPhotoContentInByte() throws IOException{
  FileInputStream fis = new FileInputStream(address);
  BufferedInputStream bis = new BufferedInputStream(fis);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int c = bis.read();
  while(c != -1){
   baos.write(c);
   c = bis.read();
  }
  bis.close();
  byte[] rtn = baos.toByteArray();
  baos.close();
  return rtn;
 }
}

 

 

/**

*使用servlet读取图片,网页上使用<img src="PhotoViewServlet.do?photoId=<s:property value="id"/>" />

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  String strId = request.getParameter("photoId");
  if(null == strId)
   return;
  int id = Integer.parseInt(strId);
  ServletContext sc = request.getSession().getServletContext();
  WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
  
  PhotoDao dao = (PhotoDaoImpl)wac.getBean("PhotoDao");
  Photo photo = dao.findById(id);
  response.setContentType("image/"+photo.getContentType());
  Blob b = photo.getContent();
  ServletOutputStream sos = response.getOutputStream();
  try {
   InputStream is = b.getBinaryStream();
   int bsize = (int)b.length();
   byte[] bts = new byte[bsize];
   int bread = 0;
   while((bread = is.read(bts))!=-1){
    sos.write(bts,0,bread);
   }
  } catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值