`

Struts2多文件上传

阅读更多

Struts2多文件上传:
 在Action中配置三个属性:
 private List<File> file;
 
 private List<String> fileFileName;
 
 private List<String> fileContentType;
 /**
  * 完成多文件上传能功的Action
  *
  * @author 张明学
  *
  */
 public class UploadsAction extends ActionSupport {
  private String username;
 
  private String password;
 
  private List<File> file;
 
  private List<String> fileFileName;
 
  private List<String> fileContentType;
 
  @Override
  public String execute() throws Exception {
   String root = ServletActionContext.getRequest().getRealPath("/upload");
 
   if (file != null) {
    for (int i = 0; i < file.size(); i++) {
     InputStream is = new FileInputStream(file.get(i));
 
     File destFile = new File(root, this.fileFileName.get(i));
 
     OutputStream os = new FileOutputStream(destFile);
 
     byte[] buffer = new byte[400];
 
     int length = 0;
 
     while ((length = is.read(buffer)) > 0) {
      os.write(buffer, 0, length);
     }
     os.close();
     is.close();
 
    }
   }
   return SUCCESS;
  }
 
  public List<File> getFile() {
   return file;
  }
 
  public void setFile(List<File> file) {
   this.file = file;
  }
 
  public List<String> getFileContentType() {
   return fileContentType;
  }
 
  public void setFileContentType(List<String> fileContentType) {
   this.fileContentType = fileContentType;
  }
 
  public List<String> getFileFileName() {
   return fileFileName;
  }
 
  public void setFileFileName(List<String> fileFileName) {
   this.fileFileName = fileFileName;
  }
 
  public String getPassword() {
   return password;
  }
 
  public void setPassword(String password) {
   this.password = password;
  }
 
  public String getUsername() {
   return username;
  }
 
  public void setUsername(String username) {
   this.username = username;
  }
 
 }
 在页面中:
 一, 文件上传的个数是静态的:
  <s:form method="post" action="uploads" enctype="multipart/form-data">
     <s:textfield name="username" label="username"></s:textfield>    
     <s:password name="password" label="password"></s:password>
     <s:file name="file" label="file1"></s:file>
     <s:file name="file" label="file2"></s:file>
     <s:file name="file" label="file3"></s:file>
     <s:submit></s:submit>
     <s:reset></s:reset>
    </s:form>
 二,文件上传的个数是动态的:
  //动态添加文件上传个数的JS
  <script language="javascript">
     function addMore()
     {
      var td=document.getElementById("more");
      var br=document.createElement("br");
      var input=document.createElement("input");
      var button=document.createElement("input");
      
      input.type="file";
      input.name="file";
      
      button.type="button"
      button.value="删除";
      button.onclick = function()
      {
       td.removeChild(br);
       td.removeChild(input);
       td.removeChild(button);
      }
      
      td.appendChild(br);
      td.appendChild(input);
      td.appendChild(button);
      
     }
    </script>
 
  <s:form action="uploads" method="post" enctype="multipart/form-data" theme="simple">
   <table border="1" align="center" width="60%">
    <tr>
     <td>用户名:</td>
     <td>
      <s:textfield name="username"></s:textfield>
     </td>
    </tr>
    <tr>
     <td>密  码:</td>
     <td>
      <s:password name="password"></s:password>
     </td>
    </tr>
    <tr>
     <td>文件:</td>
     <td id="more">
      <s:file name="file"></s:file><input type="button" value="更多" onclick="addMore()">
     </td>
    </tr>
    <tr>
     <td>
      <s:submit></s:submit>
     </td>
     <td>
      <s:reset></s:reset>
     </td>
    </tr>
   </table>
  </s:form>   

  • Struts2_10.rar (3.7 MB)
  • 描述: 我的Struts2多文件上传实例
  • 下载次数: 135
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics