上传操作依然使用form表单,传输的数据都很大,所以只能用post方式提交。
如要做上传操作,需要对表单的数据进行封装。
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upDoc">
<input type="submit" value="Upload">
</form>
由于上传表单要被封装,如果在上传表单中存在text,select等其他输入内容,则在表单提交处无法使用request.getParameter()接收text,select等输入的数据。这时要使用smartupload为用户提供的request方法接收text,select等输入的数据。以我的TestCase为例:
0. JSP
- <form action="user" method="post" enctype="multipart/form-data">
- <table border="2" bgcolor="#ffff99">
- <tr>
- <td>UID: </td>
- <td><input type="text" name="uid"></td>
- </tr>
- <tr>
- <td>ACTOR: </td>
- <td><input type="text" name="actor"></td>
- </tr>
- <tr>
- <td>Upload: </td>
- <td><input type="file" name="upDoc"><input name="upDocName"></td>
- </tr>
- <tr>
- <td colspan="2" align="center">
- <input type="hidden" name="status" value="insert">
- <input type="submit" value="INSERT">
- <input type="reset" value="RESET">
- </td>
- </tr>
- </table>
- </form>
1. Servlet: 获取config对象, 为SmartUpload对象初始化做准备
- private ServletConfig config = null;
- public void init(ServletConfig config) throws ServletException {
- this.config = config;
- }
2. Servlet: 获取status状态值
- SmartUpload smart = null;
- String status = null;
- /*
- * 因为Insert页面的表单有enctype="multipart/form-data"属性, 所以接收从Insert表单传来的数据不能再用req.getAttribute();要改用smart.getRequest().getParameter();
- * 但又不要不妨碍其他表单的提交,这里只能用一个分支语句将其分开。
- */
- if (req.getParameter("status") != null) {
- status = req.getParameter("status");
- } else {
- smart = new SmartUpload();
- try {
- smart.initialize(config, req, resp); //对smart进行初始化
- smart.upload(); //smart准备工作, 我觉得也属于初始化工作,不知道为什么作者要把这个方法单独放出来
- } catch (SmartUploadException sue) {
- System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
- } catch (ServletException se) {
- System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
- throw se;
- } catch (IOException ioe) {
- System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
- throw ioe;
- }
- status = smart.getRequest().getParameter("status");
- }
3. Servlet: insert()方法中进行上传(伴随着其他数据的数据库提交)
- if("insert".equals(status)) {
- //保存基本数据
- user = new User();
- user.setUid(smart.getRequest().getParameter("uid"));
- user.setActor(smart.getRequest().getParameter("actor"));
- DAOFactory.getUserDAOInstance().insert(user);
- flag_boolean = true;
- flag_String = "uOperation";
- req.setAttribute("flag_boolean", new Boolean(flag_boolean));
- req.setAttribute("flag_String", flag_String);
- path = "../common/confirm.jsp";
- //上传文件
- String upDocName = smart.getRequest().getParameter("upDocName");
- String ext = smart.getFiles().getFile(0).getFileExt();
- try {
- if ("".equals(upDocName)) {
- smart.save("/upload"); //不修改上传文件名时的保存方法
- } else {
- smart.getFiles().getFile(0).saveAs("/upload/" + upDocName + "." + ext); //修改上传文件名时的保存方法:这个方法不抛出ServletException异常
- }
- } catch (SmartUploadException sue) {
- System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
- } catch (ServletException se) {
- System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
- throw se;
- } catch (IOException ioe) {
- System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
- throw ioe;
- }
- }