日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

將xls中的數(shù)據(jù)寫入數(shù)據(jù)庫 - 風(fēng)華少年 - JavaEye技術(shù)網(wǎng)站

 suweixin 2011-01-15

將xls中的數(shù)據(jù)寫入數(shù)據(jù)庫

文章分類:Web前端
Upload.jsp代碼 復(fù)制代碼
  1.  <script>   
  2.   
  3.     function callUpload() {   
  4.      var fileValue = document.FileUpload.file1.value;   
  5.      if (fileValue == "") {   
  6.         alert("請選擇所需上傳的文件!");    
  7.         return false;   
  8.      }   
  9.       showLayer.style.display = "inline";     
  10.      //count()   
  11.      FileUpload.submit();    
  12.        
  13.    }   
  14. </script>   
  15. <body>   
  16. <form  name="FileUpload" method="post" action="uploads" enctype="multipart/form-data" >     
  17.     <table border="0">   
  18.       <tr>   
  19.         <td nowrap>    
  20.           <div align="left">導(dǎo)入的EXCEL文件(導(dǎo)入的明細(xì)會復(fù)蓋原有明細(xì)數(shù)據(jù)):</div>   
  21.         </td>   
  22.       </tr>   
  23.       <tr>   
  24.         <td nowrap>    
  25.         <input type="file" class="mybutton" name="file1" size="50" style="border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px">   
  26.         <input type="submit" class="mybutton" value="導(dǎo)入" name="shangchuan" onClick="return callUpload()" style="border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px" >   
  27.         </td>   
  28.       </tr>    
  29.     </table>   
  30.     <table width="100%">   
  31.         <tr>   
  32.         <td>   
  33.             <hr width="100%">   
  34.         </td>   
  35.       </tr>   
  36.     </table>   
  37.     </form>   
  38. </body>  

 

Upload.java代碼 復(fù)制代碼
  1. protected void doPost(HttpServletRequest request, HttpServletResponse response)   
  2.             throws ServletException, IOException {     
  3.           final long MAX_SIZE = 3 * 1024 * 1024;// 設(shè)置上傳文件最大為 3M   
  4.           String  u_name="";   
  5.            // 允許上傳的文件格式的列表   
  6.           final String[] allowedExt = new String[] { "xls""jpeg""gif""txt",   
  7.              "doc""docx""mp3""wma" };   
  8.           response.setContentType("text/html");   
  9.            // 設(shè)置字符編碼為UTF-8, 這樣支持漢字顯示   
  10.           response.setCharacterEncoding("GBK");   
  11.           //實(shí)例化RequestContext對象   
  12.           RequestContext requestContext = new ServletRequestContext(request);     
  13.           if(FileUpload.isMultipartContent(requestContext)){}    
  14.           // 實(shí)例化一個(gè)硬盤文件工廠,用來配置上傳組件ServletFileUpload   
  15.           DiskFileItemFactory dfif = new DiskFileItemFactory();     
  16.           //上傳文件胡原始路徑   
  17.           String  realpath = this.getServletContext().getRealPath("/")+"ImagesUploadTemp" ;   
  18.           // 設(shè)置存放臨時(shí)文件的目錄   
  19.           dfif.setRepository(new File(realpath));   
  20.           dfif.setSizeThreshold(4096);// 設(shè)置上傳文件時(shí)用于臨時(shí)存放文件的內(nèi)存大小,這里是4K.多于的部分將臨時(shí)存在硬盤    
  21.           // 用以上工廠實(shí)例化上傳組件   
  22.           ServletFileUpload sfu = new ServletFileUpload(dfif);   
  23.           System.err.println(" reapath="+this.getServletContext().getRealPath("/")+"ImagesUploadTemp");   
  24.            // 設(shè)置最大上傳尺寸   
  25.           sfu.setSizeMax(MAX_SIZE);     
  26.           PrintWriter out = response.getWriter();   
  27.           // 從request得到 所有 上傳域的列表   
  28.           List fileList =  null;    
  29.           try {   
  30.                fileList = sfu.parseRequest(request);    
  31.           } catch (FileUploadException e) {// 處理文件尺寸過大異常   
  32.                e.printStackTrace();   
  33.                if (e instanceof SizeLimitExceededException) {   
  34.                    out.println("文件尺寸超過規(guī)定大小:" + MAX_SIZE + "字節(jié)<p />");   
  35.                    out.println("<a href=\"excelInsert.action\" target=\"_top\">返回</a>");   
  36.                    return;   
  37.                }   
  38.                //e.printStackTrace();   
  39.            }   
  40.            // 沒有文件上傳   
  41.            if (fileList == null || fileList.size() == 0) {   
  42.                out.println("文件大小不能為空,請選擇上傳文件<p />");   
  43.                out.println("<a href=\"excelInsert.action\" target=\"_top\">返回</a>");   
  44.                return;   
  45.            }   
  46.            // 得到所有上傳的文件   
  47.            Iterator fileItr = fileList.iterator();   
  48.            // 循環(huán)處理所有文件   
  49.            while (fileItr.hasNext()) {   
  50.                 FileItem fileItem = null;   
  51.                 String path = null;   
  52.                 long size = 0;   
  53.                 // 得到當(dāng)前文件   
  54.                 fileItem = (FileItem) fileItr.next();   
  55.                 // 忽略簡單form字段而不是上傳域的文件域(<input type="text" />等)   
  56.                 if (fileItem == null || fileItem.isFormField()) {   
  57.                  continue;   
  58.                 }   
  59.                 // 得到文件的完整路徑   
  60.                 path = fileItem.getName();     
  61.                 path = new String(path.getBytes("ISO-8859-1"),"UTF-8");   
  62.                 System.out.println("完整路徑="+path);   
  63.                 // 得到文件的大小   
  64.                 size = fileItem.getSize();   
  65.                 if ("".equals(path) || size == 0) {   
  66.                     out.println("請選擇上傳文件<p />");   
  67.                     out.println("<a href=\"excelInsert.action\" target=\"_top\">返回</a>");   
  68.                     return;   
  69.                 }   
  70.        
  71.                 // 得到去除路徑的文件名   
  72.                 String t_name = path.substring(path.lastIndexOf("\\") + 1);   
  73.                 // 得到文件的擴(kuò)展名(無擴(kuò)展名時(shí)將得到全名)   
  74.                 String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);   
  75.                 // 拒絕接受規(guī)定文件?