<%@taglibprefix="s"uri="/struts-tags"%>

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

JS+Struts2多文件上傳完整示例

系統 1941 0
1、JSP頁面:
JS控制增加刪除多個上傳文件框,代碼如下:
Java代碼
  1. <%@pagelanguage= "java" pageEncoding= "UTF-8" %>
  2. <%@taglibprefix= "s" uri= "/struts-tags" %>
  3. <!DOCTYPEhtmlPUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  4. <htmlxmlns= "http://www.w3.org/1999/xhtml" >
  5. <head>
  6. <% @include file= "../../_head.html" %>
  7. <title> 文件上傳 </title>
  8. <metahttp-equiv= "pragma" content= "no-cache" >
  9. <metahttp-equiv= "cache-control" content= "no-cache" >
  10. <metahttp-equiv= "expires" content= "0" >
  11. <scriptlanguage= "javascript" type= "text/javascript"
  12. src= "../js/common/common.js" ></script>
  13. <scripttype= "text/javascript" >
  14. varpos= 1 ;
  15. functionaddFileComponent(){
  16. varelTable=document.getElementById( 'uploadTable' ).getElementsByTagName( 'tbody' )[ 0 ];
  17. varelTr=document.getElementById( 'fileTr' );
  18. varelTr2=document.getElementById( 'op' );
  19. varnewEleTr=elTr.cloneNode( true );
  20. newEleTr.id= "fileTr" +pos;
  21. newEleTr.style.display= "" ;
  22. inputs=newEleTr.getElementsByTagName( 'input' );
  23. inputs[ 0 ].id= "file" +pos;
  24. varelInput=inputs[ 1 ];
  25. elInput.onclick=delFileComponent;
  26. elInput.id= "delbutton" +pos++;
  27. elTable.insertBefore(newEleTr,elTr2);
  28. }
  29. functiondelFileComponent(){
  30. varelTable=document.getElementById( 'uploadTable' ).getElementsByTagName( 'tbody' )[ 0 ];
  31. vartrArr=elTable.getElementsByTagName( "tr" );
  32. varel=event.srcElement;
  33. for (j= 0 ;j<trArr.length;j++){
  34. tr=trArr[j];
  35. if (tr.getElementsByTagName( "input" )[ 1 ]==el){
  36. elTable.removeChild(tr);
  37. pos--;
  38. break ;
  39. }
  40. }
  41. }
  42. functionisValidateFile(obj){
  43. varextend=obj.value.substring(obj.value.lastIndexOf( "." )+ 1 );
  44. if (extend== "" ){
  45. } else {
  46. if (!(extend== "xls" ||extend== "doc" )){
  47. alert( "請上傳后綴名為xls或doc的文件!" );
  48. varnf=obj.cloneNode( true );
  49. nf.value= '' ;
  50. obj.parentNode.replaceChild(nf,obj);
  51. return false ;
  52. }
  53. }
  54. return true ;
  55. }
  56. </script>
  57. </head>
  58. <body>
  59. <%@includefile= "/common/message.jsp" %>
  60. <div class = "body-box" >
  61. <div class = "rhead" >
  62. <div class = "rpos" >
  63. 文件上傳 (可同時上傳多份文件)
  64. </div>
  65. <div class = "clear" ></div>
  66. </div>
  67. <s:formid= "ops" action= "csc_mUploadFile" theme= "simple"
  68. cssClass= "rhead" enctype= "multipart/form-data" >
  69. <tableid= "uploadTable" width= "100%" border= "0" >
  70. <tr>
  71. <td>
  72. <inputtype= "file" id= "file0" name= "uploadFile" size= "50"
  73. onchange= "isValidateFile(this);" />
  74. </td>
  75. </tr>
  76. <trid= "fileTr" style= "display:none;" >
  77. <td>
  78. <inputtype= "file" size= "50" name= "uploadFile"
  79. onchange= "isValidateFile(this);" />
  80. &nbsp;
  81. <inputtype= "button" value= "刪除" />
  82. </td>
  83. </tr>
  84. <trid= "op" >
  85. <td>
  86. <inputtype= "submit" id= "uploadbutton" value= "上傳" />
  87. &nbsp;
  88. <inputtype= "button" value= "添加" id= "addbutton"
  89. onClick= "addFileComponent();" />
  90. &nbsp;
  91. </td>
  92. </tr>
  93. </table>
  94. </s:form>
  95. <table class = "pn-ltable" width= "100%" cellspacing= "1" cellpadding= "0"
  96. border= "0" >
  97. <thead class = "pn-lthead" >
  98. <tr>
  99. <th>
  100. 序號
  101. </th>
  102. <th>
  103. 文件名
  104. </th>
  105. <th>
  106. 上傳時間
  107. </th>
  108. </tr>
  109. </thead>
  110. <tbody class = "pn-ltbody" >
  111. <tronmouseover= "Pn.LTable.lineOver(this);"
  112. onmouseout= "Pn.LTable.lineOut(this);"
  113. onclick= "Pn.LTable.lineSelect(this);" >
  114. <td>
  115. </td>
  116. <td>
  117. </td>
  118. <td>
  119. </td>
  120. </tr>
  121. </tbody>
  122. </table>
  123. </div>
  124. </body>
  125. </html>


2、Action后臺處理上傳文件:
Java代碼
  1. //uploadFile對應頁面<inputtype="file"name="uploadFile">
  2. private List<File>uploadFile;
  3. //文件名對應uploadFile+“FileName”,要不獲取不到文件名
  4. private List<String>uploadFileFileName;
  5. // 文件上傳
  6. public StringmUploadFile(){
  7. if ( null ==uploadFile){
  8. this .addActionError( "請上傳文件!" );
  9. } else {
  10. StringfileName= "" ;
  11. try {
  12. //在自己代碼中控制 文件上傳 的服務器目錄
  13. Stringdirectory=ServletActionContext.getServletContext().getRealPath( "/uploads" );
  14. //判斷該目錄是否存在,不存在則創建
  15. FileUtil.makeDir(directory);
  16. //循環處理上傳的文件
  17. for ( int i= 0 ,j=uploadFile.size();i<j;i++){
  18. fileName=uploadFileFileName.get(i);
  19. StringfilePath=directory+File.separator+fileName;
  20. FileUtil.uploadFile(uploadFile.get(i), new File(filePath));
  21. }
  22. } catch (IOExceptione){
  23. this .addActionMessage( "" );
  24. }
  25. this .addActionMessage( " 文件上傳 成功!" );
  26. }
  27. return "fileUpload" ;
  28. }

FileUtil代碼如下:
Java代碼
  1. public class FileUtil{
  2. private static final int BUFFER_SIZE= 16 * 1024 ;
  3. public static void uploadFile(Filesrc,Filedst) throws IOException{
  4. InputStreamin= null ;
  5. OutputStreamout= null ;
  6. try {
  7. in= new BufferedInputStream( new FileInputStream(src),BUFFER_SIZE);
  8. out= new BufferedOutputStream( new FileOutputStream(dst),
  9. BUFFER_SIZE);
  10. byte []buffer= new byte [BUFFER_SIZE];
  11. while (in.read(buffer)> 0 ){
  12. out.write(buffer);
  13. }
  14. } finally {
  15. if ( null !=in){
  16. in.close();
  17. }
  18. if ( null !=out){
  19. out.close();
  20. }
  21. }
  22. }
  23. public static StringgetExtention(StringfileName){
  24. int pos=fileName.lastIndexOf( "." );
  25. return fileName.substring(pos);
  26. }
  27. public static void makeDir(Stringdirectory){
  28. Filedir= new File(directory);
  29. if (!dir.isDirectory()){
  30. dir.mkdirs();
  31. }
  32. }
  33. public static StringgenerateFileName(StringfileName)
  34. throws UnsupportedEncodingException{
  35. DateFormatformat= new SimpleDateFormat( "yyMMddHHmmss" );
  36. StringformatDate=format.format( new Date());
  37. Stringextension=fileName.substring(fileName.lastIndexOf( "." ));
  38. fileName= new String(fileName.getBytes( "iso8859-1" ), "gb2312" );
  39. return fileName+ "_" +formatDate+ new Random().nextInt( 10000 )
  40. +extension;
  41. }
  42. }



擴展:
1.可以實現帶進度條的上傳與下載;
2.可以用xml文件記錄上傳的文件清單,并且可以根據頁面對上傳文件的操作來修改相應的xml文件;

JS+Struts2多文件上傳完整示例


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 综合久久综合 | 国产香蕉久久精品综合网 | 欧美成人午夜精品一区二区 | 99久久免费精品国产免费高清 | 久久人人网| 五月婷婷激情视频 | 国产精品精品 | 国产精品你懂的在线播放 | 国产亚洲精品xxx | 久久香蕉国产线看免费 | 欧美洲久久日韩欧美 | 国内精品久久久久久影院老狼 | 欧美一区精品二区三区 | www.午夜色| 欧美一区二区三区精品影视 | 大学生一级毛片 | 爆操白虎 | 亚洲国产精品成人综合色在线婷婷 | 在线观看国产情趣免费视频 | 九九九精品| 骚碰97| 97在线视频99播放 | 伊色综合久久之综合久久 | 精品国产成人高清在线 | 在线视频自拍 | 欧美黄色免费在线观看 | 毛片大全免费观看 | 一级一毛片a级毛片欧美 | 国产精品福利影院 | 久久精品免费观看视频 | 久久精品一区二区三区日韩 | 97久久影院 | 免费香蕉视频 | 99欧美在线 | 久久精品综合免费观看 | 天天干天天摸 | 久久精品视频1 | 亚洲国产精品久久久久666 | 久久久亚洲欧洲国产 | 嫩草影院麻豆久久视频 | 国产精品1区2区3区 国产精品1区2区3区在线播放 |