`

ExtJs+Spring+Hibernate上传文件/图片到Oracle数据库Blob字段类型

    博客分类:
  • Java
阅读更多
ExtJs:
var fatj_form = new Ext.form.FormPanel(
{
   id : 'fatj_form',
   url : 'Gzry/Fjxx/EditFjxx.action?format=json',
   method : 'post',
   frame : true,
   border : false,
   autoWidth : true,
   fileUpload : true,
   layout : 'form',
   items : [
   {
      xtype : 'textfield',
      labelWidth : 60,
      readOnly : true,
      fieldLabel : '名称',
      name : 'famc',
      width : 475
   }
   ,
   {
      xtype : 'panel',
      layout : 'column',
      items : [
      {
         xtype : 'panel',
         layout : 'form',
         columnWidth : .5,
         items : [
         {
            xtype : "datefield",
            format : "Y-m-d",
            readOnly : true,
            value : "",
            labelWidth : 60,
            fieldLabel : '开始日期',
            name : 'gzrqks',
            width : 180
         }
         ]
      }
      ,
      {
         xtype : 'panel',
         layout : 'form',
         columnWidth : .5,
         items : [
         {
            xtype : "datefield",
            format : "Y-m-d",
            readOnly : true,
            value : "",
            labelWidth : 60,
            fieldLabel : '结束日期',
            name : 'gzrqjs',
            width : 180
         }
         ]
      }
      ]
   }
   ,
   {
      xtype : 'fileuploadfield',
      layout : 'form',
      id : 'files',
      width : 475,
      allowBlank : false,
      msgTarget : 'side',
      emptyText : '请选择需要上传的图片...',
      fieldLabel : '图片附件',
      name : 'files',
      buttonText : '',
      buttonCfg :
      {
         iconCls : 'upload-icon'
      }
   }
   ]
}
);


Spring注入:
<!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />


Java:
@RequestMapping(value = "EditFjxx.action")
public void editFaxx(HttpServletRequest request,HttpServletResponse response,
		@RequestParam(value = "files", required = false)MultipartFile files) throws IOException {
	response.setContentType("text/html;charset=utf-8");
	PrintWriter out = response.getWriter();
	String fileName = files.getOriginalFilename();
	String id = request.getParameter("id");
	if (id != null && !id.equals("")) {
		try {
			if (fileName.endsWith(".jpg")) {
				InputStream in = files.getInputStream();
				Blob tp = Hibernate.createBlob(in);
				GzryFjxx fj = new GzryFjxx();
				fj.setFaid(id);
				fj.setText(fileName);
				fj.setTp(tp);
				gzryFjxxServ.add(fj, faxx);
				out.print("{success:true,message:'提交成功!!!'}");
			} else {
				out.print("{success:false,message:'文件格式必须是图片文件,扩展名(jpg)!!!'}");
			}
		} catch (Exception e) {
			out.print("{success:false,message:'提交失败!!!'}");
			logger.error(e);
			e.printStackTrace();
		}
	} else {
		out.print("{success:false,message:'出现异常!!!'}");
	}
}

//Hibernate实体类:
import java.sql.Blob;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_GZRY_FJXX")
public class GzryFjxx {

	private String id;
	private Blob tp;
	private String text;
	
	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	public String getId() {
		return this.id;
	}

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

	@Column(name = "TP")
	public Blob getTp() {
		return tp;
	}

	public void setTp(Blob tp) {
		this.tp = tp;
	}

	@Column(name = "TEXT", length = 200)
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}	

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics