-------------------------action------------------------------
public class LizNewAction extends Action {
/*
* Generated Methods
*/
private LizDao lizdao;
private ImageVo imageVo;
InputStream is;
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm lizNewForm = (DynaActionForm) form;// TODO Auto-generated method stub
String moue=(String)lizNewForm.get("moue");
FormFile file=(FormFile)lizNewForm.get("file");
String id=(String)lizNewForm.get("id");
try {
is=file.getInputStream();
System.out.println("*******************获得附件内容*******************");
imageVo.setFileid(new Integer(id));
imageVo.setFilename(file.getFileName());
imageVo.setMoue(moue);
imageVo.setFilecontext(is);
Date date=new Date();
String name=date.getDay()+date.getHours()+date.getMinutes()+"";
OutPoint op=new OutPoint();
String filepath=op.writer(imageVo.getFilecontext(), file.getFileName(), name);
System.out.println("*******************存入表对象中*******************");
if(lizdao.insert(imageVo))
{
request.setAttribute("id", imageVo.getFileid()+"");
return mapping.findForward("success");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("filed");
}
public LizDao getLizdao() {
return lizdao;
}
public void setLizdao(LizDao lizdao) {
this.lizdao = lizdao;
}
public ImageVo getImageVo() {
return imageVo;
}
public void setImageVo(ImageVo imageVo) {
this.imageVo = imageVo;
}
}
-------------------------------DAO----------------------
public class LizDao extends HibernateDaoSupport
{
PreparedStatement ps;
public boolean insert(ImageVo image) {
try {
String sql="insert into Image1(fileid,filename,moue,filecontext) value(?,?,?,?)";
Session session=this.getSession();
Transaction tran=session.beginTransaction();
ps=session.connection().prepareStatement(sql);
ps.setInt(1,image.getFileid());
ps.setString(2,image.getFilename());
ps.setString(3, image.getMoue());
ps.setBinaryStream(4, image.getFilecontext(), image.getFilecontext().available());
ps.execute();
tran.commit();
session.close();
ps.close();
System.out.println("*******************储存成功*******************");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
ssh整合后图片保存到数据库中
最新推荐文章于 2019-07-02 11:10:53 发布