`
ijavagos
  • 浏览: 1191052 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

阅读更多
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. <%@includefile="../../_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. returnfalse;
  52. }
  53. }
  54. returntrue;
  55. }
  56. </script>
  57. </head>
  58. <body>
  59. <%@includefile="/common/message.jsp"%>
  60. <divclass="body-box">
  61. <divclass="rhead">
  62. <divclass="rpos">
  63. 文件上传(可同时上传多份文件)
  64. </div>
  65. <divclass="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. <tableclass="pn-ltable"width="100%"cellspacing="1"cellpadding="0"
  96. border="0">
  97. <theadclass="pn-lthead">
  98. <tr>
  99. <th>
  100. 序号
  101. </th>
  102. <th>
  103. 文件名
  104. </th>
  105. <th>
  106. 上传时间
  107. </th>
  108. </tr>
  109. </thead>
  110. <tbodyclass="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. privateList<File>uploadFile;
  3. //文件名对应uploadFile+“FileName”,要不获取不到文件名
  4. privateList<String>uploadFileFileName;
  5. //文件上传
  6. publicStringmUploadFile(){
  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(inti=0,j=uploadFile.size();i<j;i++){
  18. fileName=uploadFileFileName.get(i);
  19. StringfilePath=directory+File.separator+fileName;
  20. FileUtil.uploadFile(uploadFile.get(i),newFile(filePath));
  21. }
  22. }catch(IOExceptione){
  23. this.addActionMessage("");
  24. }
  25. this.addActionMessage("文件上传成功!");
  26. }
  27. return"fileUpload";
  28. }

FileUtil代码如下:
Java代码 复制代码
  1. publicclassFileUtil{
  2. privatestaticfinalintBUFFER_SIZE=16*1024;
  3. publicstaticvoiduploadFile(Filesrc,Filedst)throwsIOException{
  4. InputStreamin=null;
  5. OutputStreamout=null;
  6. try{
  7. in=newBufferedInputStream(newFileInputStream(src),BUFFER_SIZE);
  8. out=newBufferedOutputStream(newFileOutputStream(dst),
  9. BUFFER_SIZE);
  10. byte[]buffer=newbyte[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. publicstaticStringgetExtention(StringfileName){
  24. intpos=fileName.lastIndexOf(".");
  25. returnfileName.substring(pos);
  26. }
  27. publicstaticvoidmakeDir(Stringdirectory){
  28. Filedir=newFile(directory);
  29. if(!dir.isDirectory()){
  30. dir.mkdirs();
  31. }
  32. }
  33. publicstaticStringgenerateFileName(StringfileName)
  34. throwsUnsupportedEncodingException{
  35. DateFormatformat=newSimpleDateFormat("yyMMddHHmmss");
  36. StringformatDate=format.format(newDate());
  37. Stringextension=fileName.substring(fileName.lastIndexOf("."));
  38. fileName=newString(fileName.getBytes("iso8859-1"),"gb2312");
  39. returnfileName+"_"+formatDate+newRandom().nextInt(10000)
  40. +extension;
  41. }
  42. }



扩展:
1.可以实现带进度条的上传与下载;
2.可以用xml文件记录上传的文件清单,并且可以根据页面对上传文件的操作来修改相应的xml文件;
分享到:
评论

相关推荐

    struts中多文件上传简单示例

    可以实现一次性上传多个文件,使用JavaScript动态添加一个上传文本框。是用struts2实现的

    ajaxfileupload.js

    ajaxfileupload.js多文件上传和单文件,可传参,是做多图片上传时收集的,可进行多图片上传,进行后台传参,还有相关struts2相关示例.

    java-springmvc-mybatis-easyui

    java-springmvc-mybatis-easyuiSpringMvc3.2.x+mybatis3.1.x+EasyUI1.3.4+Maven架构的...编译环境:maven:3.x+当前示例演示了包括:权限控制、超大附件文件上传、EasyUI基本组件使用等等功能,具体请自行查看演示功能

    JSP网络编程从基础到实践的实例代码

    实例2 HTML与JavaScript交互示例 第4章 JSP语法 实例3 JSP程序的基本结构 实例4 简单数据类型综合应用实例 实例5 包装类综合应用实例 实例6 数组应用实例 实例7 字符截取程序示例 实例8 查找字符串程序示例 ...

    jsp网络编程从基础到实践

    实例2 HTML与JavaScript交互示例 第4章 JSP语法 实例3 JSP程序的基本结构 实例4 简单数据类型综合应用实例 实例5 包装类综合应用实例 实例6 数组应用实例 实例7 字符截取程序示例 实例8 查找字符串程序示例 ...

    JSP网络编程从基础到实践 实例代码

    实例2 HTML与JavaScript交互示例 第4章 JSP语法 实例3 JSP程序的基本结构 实例4 简单数据类型综合应用实例 实例5 包装类综合应用实例 实例6 数组应用实例 实例7 字符截取程序示例 实例8 查找字符串程序示例 ...

    JSP网络编程从基础到实践

    实例34 随机读取文件程序示例 实例35 故事接龙 实例36 文件上传 实例37 在浏览器中打开文件 实例38 文件下载 实例39 用jspSmartUpload组件实现文件上传 实例40 应用jspSmartUpload组件进行...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目源代码

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    java开源包2

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    various-simple-examples:各种示例集,易于使用(VUESpringBootThymeleafJettySSHSSMElasticSearchRabbitMQlombokJerseyWebSocketShardingJDBCREST)

    :springboot 原生上传文件操作(包含 springboot1.5 和 2.0 版本) :springboot整合lombok、elasticsearch、jersey、rabbitmq(整合之前需要安装相应的环境) :springboot 整合 websocket 可实现聊天室功能 :...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    java开源包1

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包11

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包3

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包6

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包5

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包10

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包4

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

Global site tag (gtag.js) - Google Analytics