简单smartupload

上传操作依然使用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

  1. <form action="user" method="post" enctype="multipart/form-data">
  2.     <table border="2" bgcolor="#ffff99">
  3.         <tr>
  4.             <td>UID: </td>
  5.             <td><input type="text" name="uid"></td>
  6.         </tr>
  7.         <tr>
  8.             <td>ACTOR: </td>
  9.             <td><input type="text" name="actor"></td>
  10.         </tr>
  11.         <tr>
  12.             <td>Upload: </td>
  13.             <td><input type="file" name="upDoc"><input name="upDocName"></td>
  14.         </tr>
  15.         <tr>
  16.             <td colspan="2" align="center">
  17.             <input type="hidden" name="status" value="insert">
  18.             <input type="submit" value="INSERT">
  19.             <input type="reset" value="RESET">
  20.             </td>
  21.         </tr>
  22.     </table>
  23. </form>

1. Servlet: 获取config对象, 为SmartUpload对象初始化做准备

  1. private ServletConfig config = null;
  2. public void init(ServletConfig config) throws ServletException {
  3.     this.config = config;
  4. }

2. Servlet: 获取status状态值

  1. SmartUpload smart = null;
  2. String status = null;
  3.         
  4. /*
  5. * 因为Insert页面的表单有enctype="multipart/form-data"属性, 所以接收从Insert表单传来的数据不能再用req.getAttribute();要改用smart.getRequest().getParameter();
  6. * 但又不要不妨碍其他表单的提交,这里只能用一个分支语句将其分开。
  7. */
  8. if (req.getParameter("status") != null) {
  9.     status = req.getParameter("status");
  10. else {
  11.     smart = new SmartUpload();
  12.     try {
  13.         smart.initialize(config, req, resp); //对smart进行初始化
  14.         smart.upload(); //smart准备工作, 我觉得也属于初始化工作,不知道为什么作者要把这个方法单独放出来
  15.     } catch (SmartUploadException sue) {
  16.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
  17.     } catch (ServletException se) {
  18.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
  19.         throw se;
  20.     } catch (IOException ioe) {
  21.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
  22.         throw ioe;
  23.     }   
  24.     status = smart.getRequest().getParameter("status");
  25. }

3. Servlet: insert()方法中进行上传(伴随着其他数据的数据库提交)

  1. if("insert".equals(status)) {
  2.     //保存基本数据
  3.     user = new User();
  4.     user.setUid(smart.getRequest().getParameter("uid"));
  5.     user.setActor(smart.getRequest().getParameter("actor"));
  6.     DAOFactory.getUserDAOInstance().insert(user);
  7.     flag_boolean = true;
  8.     flag_String = "uOperation";
  9.     req.setAttribute("flag_boolean"new Boolean(flag_boolean));
  10.     req.setAttribute("flag_String", flag_String);
  11.     path = "../common/confirm.jsp";
  12.     
  13.     //上传文件
  14.     String upDocName = smart.getRequest().getParameter("upDocName");
  15.     String ext = smart.getFiles().getFile(0).getFileExt();
  16.     try {
  17.         if ("".equals(upDocName)) {
  18.             smart.save("/upload"); //不修改上传文件名时的保存方法
  19.         } else {
  20.             smart.getFiles().getFile(0).saveAs("/upload/" + upDocName + "." + ext); //修改上传文件名时的保存方法:这个方法不抛出ServletException异常
  21.         }
  22.     } catch (SmartUploadException sue) {
  23.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
  24.     } catch (ServletException se) {
  25.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
  26.         throw se;
  27.     } catch (IOException ioe) {
  28.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
  29.         throw ioe;
  30.     }   
  31. }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值