Struts2批量上传文件

来源:互联网 发布:soundstructure软件 编辑:程序博客网 时间:2024/06/05 04:19

struts.xml

  <!-- 文件上传的拦截器栈 -->
       <interceptors>
        <interceptor-stack name="fileUploadStack"> 
   <interceptor-ref name="fileUpload">
       <param name="maximumSize">409600</param>
    <param name="allowedTypes">image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg</param>
   </interceptor-ref>
   <interceptor-ref name="basicStack"/> 
  </interceptor-stack> 
      </interceptors>
      
       <default-interceptor-ref name="fileUploadStack"></default-interceptor-ref>
       <action name="upload" class="com.gdf.struts2.action.UploadAction">
   <result name="success">/test/uploadResult.jsp</result>
   <result name="input">/test/upload.jsp</result>
    <interceptor-ref name="fileUploadStack"></interceptor-ref>
  </action>

 

UploadAction.java

 

package com.gdf.struts2.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.gdf.util.Upload;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class UploadAction extends ActionSupport
{
 private String username;

 private String password;

 private List<File> file;

 private List<String> fileFileName;

 private List<String> fileContentType;
 

 public String getUsername()
 {
  return username;
 }

 public void setUsername(String username)
 {
  this.username = username;
 }

 public String getPassword()
 {
  return password;
 }

 public void setPassword(String password)
 {
  this.password = password;
 }

 public List<File> getFile()
 {
  return file;
 }

 public void setFile(List<File> file)
 {
  this.file = file;
 }

 public List<String> getFileFileName()
 {
  return fileFileName;
 }

 public void setFileFileName(List<String> fileFileName)
 {
  this.fileFileName = fileFileName;
 }

 public List<String> getFileContentType()
 {
  return fileContentType;
 }

 public void setFileContentType(List<String> fileContentType)
 {
  this.fileContentType = fileContentType;
 }

 @Override
 public String execute() throws Exception
 {
  
  Upload upload=new Upload();
  //ActionContext ctx = ActionContext.getContext();
  //HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
  //System.out.println(com.gdf.util.SessionManager.getUser(request).getId());
  ArrayList list=upload.upload(file, this.getFileFileName(),"/files/");
  if (list.size()>0){
   return INPUT;
  }else{
    return SUCCESS;
  }
 }

}

 

 

Upload.java

 

 

package com.gdf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

public class Upload {
  @SuppressWarnings("unchecked")
  /**
   * @file:文件域列表
   * @fileName:上传文件的文件名
   * @path:文件上传的目录,相对目录。
   * 说明:文件上传所存放目录的规则:网站根目录下 files/会员ID/网站类型名称/上传文件分类名称/
   * @返回结果:一个存放上传文件所在位置相对路径的ArrayList
   * */
  public ArrayList upload(List file,List fileName,String path) throws IOException{
   String root = ServletActionContext.getRequest().getRealPath(path);
   File dir=new File(root);
   if (dir.exists()==false){
    dir.mkdirs();
   }
   ArrayList list=new ArrayList();
   for (int i = 0; i < file.size(); ++i)
  {
   InputStream is = new FileInputStream(file.get(i).toString());  
   SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");//格式化时间输出 
   String Rname=sdf.format(new Date());//取得当前时间,Date()是java.util包里的,这作为真实名称 
   String name=fileName.get(i).toString();//得到上传文件的原名称 
   name=Rname+name;//重命名文件名称,命名规则为:时间+原文件名称
   File destFile = new File(root, name);

   OutputStream os = new FileOutputStream(destFile);

   byte[] buffer = new byte[400];

   int length = 0;

   while ((length = is.read(buffer)) > 0)
   {
    os.write(buffer, 0, length);
   }
   list.add(path+name);
   is.close();

   os.close();
  }
   return list;
  }
}

 


upload.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%@ taglib prefix="s" uri="/struts-tags" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Struts2批量上传文件</title>
<script src="/js/check_upload_file.js"></script>
<script src="/js/validate.js"></script>
<script type="text/javascript">

function addMore()
{
 var td = document.getElementById("more");
 var br = document.createElement("br");
 var input = document.createElement("input");
 var button = document.createElement("input");
 var span=document.createElement("span");
 input.type = "file";
 input.name = "file";
 button.type = "button";
 button.value = "Remove";
 
 button.onclick = function()
 {
  td.removeChild(br);
  td.removeChild(input);
  td.removeChild(button);
  td.removeChild(span);
 }
 input.onchange=function(){
   clearTooltip(this);
   checkExt(this);
 }
 td.appendChild(br);
 td.appendChild(input);
 td.appendChild(span);
 td.appendChild(button);
 
}

</script>

</head>

<body>

 <table align="center" width="50%">
   <tr>
    <td>

     <s:fielderror cssStyle="color:red" />

    </td>
   </tr>
  </table>


  <s:form action="upload" theme="simple" enctype="multipart/form-data">

   <table align="center" width="50%" border="1">
    <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>
      file
     </td>

     <td id="more">
      <s:file name="file" onchange="clearTooltip(this);checkExt(this);" ></s:file><span></span>
      <input type="button" value="Add More.." onclick="addMore()">
     </td>
    </tr>
    
    <tr>
     <td>
      <s:submit value=" submit "></s:submit>
     </td>

     <td>
      <s:reset value=" reset "></s:reset>
     </td>
    </tr>
   </table>

  </s:form>
</body>

</html>

原创粉丝点击