文件上傳也是一個(gè)老生常談的問(wèn)題了。struts提供了很方便的文件上傳組件,利用struts,很容易開(kāi)發(fā)文件上傳的系統。本文結合ibatis和JSTL開(kāi)發(fā)一個(gè)簡(jiǎn)便而通用的文件上傳系統。通過(guò)本文,你可以學(xué)到如何在struts里配置文件上傳功能、struts1.1的動(dòng)態(tài)form配置和使用、如果簡(jiǎn)單的使用ibatis,如果簡(jiǎn)單使用的JSTL。
首先我們要在struts-config里加上一句,來(lái)指定臨時(shí)目錄以及限制大小。
然后只要在form中加上enctype="multipart/form-data" 就可以實(shí)現struts的上傳了,十分簡(jiǎn)單吧?
下面我們在數據庫中建立一張表。
create sequence Attachment_seq; |
字段的內容都很簡(jiǎn)單。
下面建立ibatis的SQL map文件。建立標準的insert、update、delete和find的SQL。相信看過(guò)前面系列文章的朋友對此已經(jīng)很熟悉了。
|
下一小就是建立數據操作層的類(lèi)代碼
/* import java.util.HashMap; import java.util.List; import com.ewuxi.champion.exception.DaoException; import com.ibatis.db.sqlmap.SqlMap; /** * @author champion * *attachment數據庫操作對象 */ public class AttachDb { /** * @param vo * @throws DaoException * 插入一條記錄 */ public void insert(HashMap vo) throws DaoException { try { SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao()); sqlMap.executeUpdate("insertattachmentDao", vo); } catch (Exception e) { throw new DaoException(e); } } /** * @param vo * @throws DaoException * 刪除一條記錄 */ public void delete(HashMap vo) throws DaoException { try { SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao()); sqlMap.executeUpdate("deleteByPrimaryKeyattachmentDao", vo); } catch (Exception e) { throw new DaoException(e); } } /** * @param vo * @throws DaoException * 修改一條記錄 */ public void update(HashMap vo) throws DaoException { try { SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao()); sqlMap.executeUpdate("updateByPrimaryKeyattachmentDao", vo); } catch (Exception e) { throw new DaoException(e); } } /** * @param vo * @return * @throws DaoException * 查找一條記錄 */ public HashMap findByPk(HashMap vo) throws DaoException { try { SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao()); return (HashMap) sqlMap.executeQueryForObject( "findByPrimaryKeyattachmentDao", vo); } catch (Exception e) { throw new DaoException(e); } } public List find(Object vo) throws DaoException { try { SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao()); return (List) sqlMap.executeQueryForList("findattachmentDao", vo); } catch (Exception e) { throw new DaoException(e); } }} |
這一層的代碼也是多次見(jiàn)到的老朋友了。事實(shí)上對于大多數數據庫操作,我們都只需要上面這么一點(diǎn)代碼。然后我們建立add的action方法
public ActionForward add( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { Service.initSet(); if (form instanceof DynaActionForm) { DynaActionForm fm = (DynaActionForm) form; FormFile ff = (FormFile) fm.get("upload"); if (ff != null && ff.getFileSize()>0) { String fileName = Service.getPath().substring( 0, Service.getPath().length() - "/WEB-INF/classes".length()) + "file/" + ff.getFileName(); HashMap map = new HashMap(); map.put(Constattachment.ATTACHDESC, fm.get("desc")); map.put(Constattachment.ATTACHFILENAME, ff.getFileName()); map.put( Constattachment.ATTACHMIMETYPE, ff.getContentType()); map.put(Constattachment.ATTACHCREATIONDATE, new Date()); map.put(Constattachment.ATTACHMODIFIEDDATE, new Date()); map.put( Constattachment.ATTACHFILESIZE, String.valueOf(ff.getFileSize())); map.put( Constattachment.ATTACHFILEPATH, "/file/" + ff.getFileName()); map.put( Constattachment.ATTACHCREATIONIP, request.getRemoteAddr()); FileManager.saveFile(fileName, ff); AttachDb attachDb = new AttachDb(); DaoCommon.startTransaction(); attachDb.insert(map); DaoCommon.commit(); request.setAttribute("url", fm.get("url")); request.setAttribute( "fileName", "/file/" + ff.getFileName()); log.info(ff.getFileName()); return mapping.findForward("success"); } } ActionErrors errors = new ActionErrors(); errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.general", "請選擇一個(gè)文件!")); saveErrors(request, errors); return mapping.findForward("false"); } catch (DaoException e) { log.error(e, e); DaoCommon.rollBack(); ActionErrors errors = new ActionErrors(); errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.general", "數據庫操作錯誤!")); saveErrors(request, errors); return mapping.findForward("false"); } catch (FileNotFoundException e) { log.error(e, e); ActionErrors errors = new ActionErrors(); errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.general", "文件保存錯誤!")); saveErrors(request, errors); return mapping.findForward("false"); } catch (IOException e) { log.error(e, e); ActionErrors errors = new ActionErrors(); errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.general", "文件操作錯誤!")); saveErrors(request, errors); return mapping.findForward("false"); } catch (Exception e) { log.error(e, e); ActionErrors errors = new ActionErrors(); errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.general", "意外錯誤!")); saveErrors(request, errors); return mapping.findForward("false"); } } |
從上面可以看出,文件的保存工作很簡(jiǎn)單,只有三句
| DynaActionForm fm = (DynaActionForm) form; FormFile ff = (FormFile) fm.get("upload"); FileManager.saveFile(fileName, ff); |
此處有一個(gè)DynaActionForm,通過(guò)DynaActionForm我們可以節省一個(gè)ActionForm的工作了。當然,這也多了一個(gè)配置工作。在struts-conifg.xml里加一個(gè)
type="org.apache.struts.action.DynaActionForm"> |
這樣,struts在頁(yè)面提交以后會(huì )自動(dòng)去找upload、rul和 desc這三個(gè)輸入 ,并將它轉成相應的數據類(lèi)型。
所以我們很容易得到一個(gè)FormFile對象,而這個(gè)對象就包含了上傳文件的所有信息。因此,我們的數據表相應的信息也有了
| map.put(Constattachment.ATTACHFILENAME, ff.getFileName()); map.put( Constattachment.ATTACHMIMETYPE, ff.getContentType()); map.put( Constattachment.ATTACHFILESIZE, String.valueOf(ff.getFileSize())); map.put( Constattachment.ATTACHFILEPATH, "/file/" + ff.getFileName()); |
最后我們只要簡(jiǎn)單的調用函數插入數據庫
| DaoCommon.startTransaction(); attachDb.insert(map); DaoCommon.commit(); |
核心技術(shù)基本講解完畢。下面講一下實(shí)現的流程。大家可以先看一下demo。首先是一個(gè)index頁(yè)面,可以選擇一個(gè)上傳一個(gè)圖片文件直接顯示出來(lái)。也可以把內容顯示在input框中。
| ‘)">‘)"> |
index.jsp的主要內容如上:主要特別的一點(diǎn)的地方就是
| AttachDb attachDb = new AttachDb(); DaoCommon.startTransaction(); request.setAttribute("fileslist", attachDb.find(new HashMap())); DaoCommon.rollBack(); |
主要就是列出表中所有文件。放到fileslist這個(gè)對象中去。然后指到attachlist.jsp這個(gè)jsp文件去。作為view層,attachlist很簡(jiǎn)單的。
">添加文件
|
首先我們可以看看怎么顯示表格
刪除的代碼也簡(jiǎn)單,把ATTACHID放入hashMap,然后執行刪除(注,文件沒(méi)有實(shí)際刪除,不過(guò)要實(shí)現文件刪除的代碼也很簡(jiǎn)單。)。最后也是讀取所有的文件,再返回到本頁(yè)
AttachDb attachDb = new AttachDb(); DaoCommon.startTransaction(); request.setAttribute("fileslist", attachDb.find(new HashMap())); DaoCommon.commit(); |
添加文件指向attach.jsp。這個(gè)文件是上傳的主要文件,實(shí)現一個(gè)實(shí)際的上傳界面。
|
聯(lián)系客服