hibernate + sql servler 的图片存储(1)

//Action 代码 action -- extends org.apache.struts.action.Action;

public class UploadImageAction
    extends Action {
  public ActionForward execute
      (ActionMapping mapping,
       ActionForm form,
       HttpServletRequest request,
       HttpServletResponse response) {
    UploadForm imgForm = (UploadForm) form;
    int id = Integer.parseInt(imgForm.getId());
    try {
      CMSColumnBean dao = new CMSColumnBean(id);
      CMSColumn vo = dao.getColumn();

      byte[] buffer = new byte[imgForm.getImg().getFileSize()];
      InputStream stream = imgForm.getImg().getInputStream();
      int length = imgForm.getImg().getFileSize();
      stream.read(buffer, 0, length);
      stream.close();
      vo.setImg(buffer);

      dao.updateTemplet(vo);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return mapping.findForward("succ");
  }
}


//配置文件代码

 <property name="img" type="binary">
   <column name="column_img" sql-type="image"/>
 </property>


// actionForm 代码   actionForm extends org.apache.struts.action.ActionForm


package capinfo.negroup.cms.controller.actinoForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadForm
    extends ActionForm {
  private String id;
  private FormFile img;

  public void setId(String value) {
    this.id = value;
  }

  public String getId() {
    return this.id;
  }

  public void setImg(FormFile value) {
    this.img = value;
  }

  public FormFile getImg() {
    return this.img;
  }

}

//model 代码

public class CMSColumn {
  private int id;
  private String name;
  private String layer;
  private String note;
  private String delFlag;
  private String order;
  private String aorder;
  private byte[] img;

  public void setId(int value) {
    this.id = value;
  }

  public int getId() {
    return this.id;
  }

  public void setName(String value) {
    this.name = value;
  }

  public String getName() {
    return this.name;
  }

  public void setLayer(String value) {
    this.layer = value;
  }

  public String getLayer() {
    return this.layer;

  }

  public void setNote(String value) {
    this.note = value;
  }

  public String getNote() {
    return this.note;
  }

  public void setDelFlag(String value) {
    this.delFlag = value;
  }

  public String getDelFlag() {
    return this.delFlag;
  }

  public void setOrder(String value) {
    this.order = value;
  }

  public String getOrder() {
    return this.order;
  }

  public void setAorder(String value) {
    this.aorder = value;
  }

  public String getAorder() {
    return this.aorder;
  }

  public void setImg(byte[] value) {
    this.img = value;
  }

  public byte[] getImg() {
    return this.img;
  }
}

posted @ 2004-11-22 18:37  黄金¤小草  阅读(316)  评论(0)    收藏  举报