smartupload -- 批量上传模块

来源:互联网 发布:银行卡余额生成软件 编辑:程序博客网 时间:2024/06/01 12:01

啥也不说了,直接看代码

1. JSP --> 注意:注释标记的对应,例如三个<!-- *** -->下面的内容是对应的。

  1. <%@ page contentType="text/html;charset=GBK"%>
  2. <html>
  3.     <head>
  4.         <title>Upload Page</title>
  5.         <script type="text/javascript">
  6.             function addUpload(id_num) {
  7.                 var file = document.getElementById(id_num+1);
  8.                 file.style.display='block';
  9.             }
  10.             function displayTxt(id_num) {
  11.                 var txt = document.getElementById(id_num);
  12.                 txt.style.display='block';
  13.             }
  14.         </script>
  15.     </head>
  16.     <body>
  17.         <center>
  18.         <form action="user" method="post" enctype="multipart/form-data">
  19.             <table border="2" bgcolor="#ffff99">
  20.                 <tr>
  21.                     <td>Upload: </td>
  22.                     <td>                                                                             <!-- *** -->
  23.                         <input type="file" name="upDoc" onChange="addUpload(0)">
  24.                         <select value="重命名" onChange="displayTxt(10)">
  25.                             <option>不命名</option>
  26.                             <option>重命名</option>
  27.                         </select>
  28.                     </td>
  29.                     <td><input id="10" name="0" style="display:none"></td>
  30.                 </tr>
  31.                 <%
  32.                     for (int i = 1; i < 10; i++) {
  33.                 %>
  34.                               <!-- *** -->
  35.                     <tr id="<%=i%>" style="display:none">
  36.                         <td>Upload: </td>
  37.                         <td>                                                                                          <!-- *** --> 
  38.                             <input type="file" name="upDoc" onChange="addUpload(<%=i%>)">
  39.                                                                                                             <!-- +++ -->
  40.                             <select value="重命名" onChange="displayTxt(<%=10+i%>)">
  41.                                 <option>不命名</option>
  42.                                 <option>重命名</option>
  43.                             </select>
  44.                         </td>                   <!-- +++ -->       <!-- 动态命名每个text -->
  45.                         <td><input id="<%=10+i%>" name="<%=i*11%>" style="display:none"></td>
  46.                     </tr>
  47.                 <%
  48.                     }
  49.                 %>
  50.                 <tr>
  51.                     <td colspan="4" align="center">
  52.                         <input type="hidden" name="status" value="upload">
  53.                         <input type="submit" value="INSERT">
  54.                         <input type="reset" value="RESET">
  55.                     </td>
  56.                 </tr>
  57.             </table>
  58.         </form>
  59.         </center>
  60.     </body>
  61. </html>

2. Servlet

  1. private ServletConfig config = null;
  2. public void init(ServletConfig config) throws ServletException {
  3.     this.config = config;
  4. }
  5. public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
  6.     // 1.准备工作
  7.     SmartUpload smart = null;
  8.     String status = null;
  9.     try {
  10.         smart = new SmartUpload();
  11.         smart.initialize(config, req, resp); //对smart进行初始化
  12.         smart.upload(); //smart准备工作, 我觉得也属于初始化工作,不知道为什么作者要把这个方法单独放出来
  13.         status = smart.getRequest().getParameter("status");
  14.     } catch (SmartUploadException sue) {
  15.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
  16.     } catch (ServletException se) {
  17.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + se.toString());
  18.         throw se;
  19.     } catch (IOException ioe) {
  20.         System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
  21.         throw ioe;
  22.     }   
  23.     
  24.     // 2.批量上传 && 批量修改上传文件名
  25.     if("upload".equals(status)) {
  26.         for (int i = 0; i < smart.getFiles().getCount(); i++) {
  27.             if (!smart.getFiles().getFile(i).isMissing()) {
  28.                 //int类型转String类型原来如此:Integer.toString(int i);
  29.                 //i * 11是根据jsp中对"重命名text"的命名规律来定义的; 10个"重命名text"的name分别为"0"、"11"、"22"..."99"。
  30.                 String upDocNameNum = Integer.toString(i * 11);
  31.                 String upDocName = smart.getRequest().getParameter(upDocNameNum);
  32.                 String ext = smart.getFiles().getFile(i).getFileExt();
  33.                 try {
  34.                     if ("".equals(upDocName)) {
  35.                         //smart.save("/upload"); //将**全部**上传文件保存到指定目录下
  36.                         /*
  37.                          * 如果此处用smart.save(), 全部不修改文件名or全部修改文件名都没有问题;但部分修改/部分不修改的话会有bug, 修改了文件名的文件会按照"原文件名"和"修改后文件名"上传两份。
  38.                          * 原因就是sava()是保存全部上传文件。实际上"全部不修改文件名"or"全部修改文件名"只是由于文件名冲突, 后面上传的覆盖了前面上传的而已。
  39.                          * 如果部分修改, 每次循环都会复制全部的上传文件, 而且又没有文件名冲突。所以就会发生上面所诉的bug。
  40.                          * 改正方法:使用我现在代码中的语句。
  41.                          */
  42.                         smart.getFiles().getFile(i).saveAs("/upload/" + smart.getFiles().getFile(i).getFileName()); //不修改上传文件名时的保存方法
  43.                     } else {
  44.                         smart.getFiles().getFile(i).saveAs("/upload/" + upDocName + "." + ext); //修改上传文件名时的保存方法:这个方法不抛出ServletException异常
  45.                     }
  46.                 } catch (SmartUploadException sue) {
  47.                     System.err.println(this.getClass().getName() + ": smart初始化 --> " + sue.toString());
  48.                 } catch (IOException ioe) {
  49.                     System.err.println(this.getClass().getName() + ": smart初始化 --> " + ioe.toString());
  50.                     throw ioe;
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }
原创粉丝点击