框架移植后出现上传失败的问题
最近我从原先的webwork+hibernate框架中转到现在的webwork+hibernate+spring框架,当处理上传的时候我采用了原先的方法的来处理上传,
vm文件
<form action="souupload.action" method="post" enctype="multipart/form-data" name="uploadsource"> 文件名:<input name="fileName" type="text" name="id" value="$!req.getSession().getAttribute("customer").id"> <input name="EDITFILE" type="file" name="Submit" value="上传文件" value=" 退 出 " onclick="javaScript:window.location.href='index.action';" ></form>
private int fileId=-1; private int id; private String fileName;private EricFileAware ericfileAware;public String execute()throws Exception{try {HttpServletRequest req = ServletActionContext.getRequest();MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) req;File doc = null;try {File[] docs = wrapper.getFiles("EDITFILE");System.out.println(docs.length);if (docs != null && docs.length > 0) {doc = docs[0];FileInputStream in = new FileInputStream(doc);Blob blob = Hibernate.createBlob(in);int fileSize = (int) doc.length();cn.kyvin.shop.webshop.object.EricFile ericFile=new cn.kyvin.shop.webshop.object.EricFile();if(this.fileId>0){}else{ericFile.setFileName(this.fileName);ericFile.setFileSize(new Integer(fileSize));ericFile.setFileData(blob);this.ericfileAware.insertFile(ericFile);}}} finally {doc.delete();}this.addActionMessage("^o^EricZone资源上传成功^o^");return SUCCESS;} catch (Exception e) {return INPUT;}}
...public EricFile insertFile(EricFile file) throws ServicesException;
public EricFile insertFile(EricFile file) throws ServicesException {try {Session session =this.getSession();Transaction tx = null;byte[] buffer = new byte[1];buffer[0] = 1;try {tx = session.beginTransaction();Blob blob = file.getFileData();file.setFileData(Hibernate.createBlob(buffer));session.save(file);session.flush();session.refresh(file, LockMode.UPGRADE);BLOB blobTemp = (BLOB) file.getFileData();OutputStream out = blobTemp.getBinaryOutputStream();InputStream in = blob.getBinaryStream();byte[] data = new byte[(int) in.available()];in.read(data);out.write(data);out.flush();in.close();out.close();session.flush();tx.commit();} catch (Exception he) {if (tx != null)tx.rollback();} finally {session.close();}} catch (HibernateException he) {}return file;}
09:15:30,156 ERROR [http-8562-Processor23] SessionImpl:2400 - Could not synchronize database state with session09:15:30,203 ERROR [http-8562-Processor23] JDBCTransaction:108 - Could not toggle autocommitnet.sf.hibernate.HibernateException: Session is closed
<tx:annotation-driven/><aop:aspectj-autoproxy/><aop:config proxy-target-advice-ref="txAdvice"/></aop:config><tx:advice id="txAdvice"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="find*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice>
Session is closed; nested exception is net.sf.hibernate.HibernateException: Session is closednet.sf.hibernate.HibernateException: Session is closed