---
title: "strut2 多文件上传"
author: "haige18"
date: "2010-10-27T21:35:44Z"
tags: ["Apache", "HTML", "JSP", "Java", "Struts"]
categories: ["企业架构"]
slug: "strut2"
draft: false
lastmod: "2010-10-27T21:35:44Z"
id: 795947
url: "https://haige18.iteye.com/blog/795947"
---

参照上一篇博文《strut2 文件上传》，多文件上传只需要将属性类型改为**数组类型或者list类型即可**。

 

**用来上传的web页面：**

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%&gt;  
&lt;%@ taglib uri="/struts-tags" prefix="s" %&gt;  
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[http://www.w3.org/TR/html4/loose.dtd](http://www.w3.org/TR/html4/loose.dtd)"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;  
&lt;title&gt;Insert title here&lt;/title&gt;  
&lt;/head&gt;  
&lt;body&gt;  
   
   &lt;s:form action="multipleUpload.action" method="post" enctype="multipart/form-data"&gt;  
       **&lt;s:file label="图片一" name="myFile" &gt;&lt;/s:file&gt;**  
       **&lt;s:file label="图片二" name="myFile" &gt;&lt;/s:file&gt;**  
       **&lt;s:file label="图片三" name="myFile" &gt;&lt;/s:file&gt;**  
       **&lt;s:file label="图片四" name="myFile" &gt;&lt;/s:file&gt;**  
       **&lt;s:file label="图片五" name="myFile" &gt;&lt;/s:file&gt;**  
       &lt;s:submit value="提交" /&gt;  
   &lt;/s:form&gt;  
   
&lt;/body&gt;  
&lt;/html&gt;

 

**处理的Action:**

package com.student.action;

import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.util.Date;  
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class MultipleUpLoadAction extends ActionSupport {  
 private static final int BUFFER\_SIZE = 2 * 1024;  
 **private File\[] myFile;**  
 **private String\[] myFileFileName;**  
 **private String\[] myFileContentType;**

 public File\[] getMyFile() {  
  return myFile;  
 }

 public void setMyFile(File\[] myFile) {  
  this.myFile = myFile;  
 }

 public String\[] getMyFileFileName() {  
  return myFileFileName;  
 }

 public void setMyFileFileName(String\[] myFileFileName) {  
  this.myFileFileName = myFileFileName;  
 }

 public String\[] getMyFileContentType() {  
  return myFileContentType;  
 }

 public void setMyFileContentType(String\[] myFileContentType) {  
  this.myFileContentType = myFileContentType;  
 }

 public String execute() throws Exception {  
  //原文件  
        **File\[] src = getMyFile();**  
        **//目标地址**  
        **String dstPath = ServletActionContext.getServletContext().getRealPath("/")+"/images";**  
        **//循环存放原文件**  
        **for(int i=0;i&lt;src.length;i++){**  
         **//给源文件重新名称**  
         **String imageFileName = getImageFileName(getMyFileFileName()\[i]);**  
         **ServletActionContext.getRequest().setAttribute("imageFileName"+(i+1), imageFileName);**  
         **//声明目标文件**  
         **File dst = new File(dstPath+"/"+imageFileName);**  
         **//将源文件拷贝到目标地**  
         **copy(src\[i], dst);**  
        **}**  
  return SUCCESS;  
 }

 //将指定的文件拷贝到指定的目录  
 public static void copy(File src,File dst){  
  InputStream in = null;  
  OutputStream out = null;  
  try {  
   in = new BufferedInputStream(new FileInputStream(src),BUFFER\_SIZE);  
   out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER\_SIZE);  
   byte\[] b = new byte\[1024];  
   int len = 0;  
   while((len = in.read(b))!= -1){  
    out.write(b, 0, len);  
   }  
  } catch (FileNotFoundException e) {  
   e.printStackTrace();  
  } catch (IOException e) {  
   e.printStackTrace();  
  }finally{  
    try {  
    if(null!=in){  
         in.close();  
     }  
     if(null!=out) {  
         out.close();  
     }  
   } catch (IOException e) {  
    e.printStackTrace();  
   }  
  }  
 }  
   
 //根据当前时间给文件取指定的名字  
 public static String getImageFileName(String fileName){  
  Random rnd = new Random();  
  int rn = rnd.nextInt(1000);  
  return new Date().getTime()+ rn + getExtention(fileName);  
 }  
   
 //获取文件的后缀  
 public static String getExtention(String fileName){  
  int pos = fileName.lastIndexOf(".");  
  return fileName.substring(pos);  
 }

}

**成功后返回的页面：**

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%&gt;  
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[http://www.w3.org/TR/html4/loose.dtd](http://www.w3.org/TR/html4/loose.dtd)"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;  
&lt;title&gt;Insert title here&lt;/title&gt;  
&lt;/head&gt;  
&lt;body&gt;  
   &lt;div style="padding: 3px; border: solid 1px #cccccc; text-align: center"&gt;  
    &lt;img src="images/${imageFileName1}" /&gt;  
    &lt;br/&gt;  
       &lt;img src="images/${imageFileName2}" /&gt;  
    &lt;br/&gt;  
       &lt;img src="images/${imageFileName3}" /&gt;  
    &lt;br/&gt;  
       &lt;img src="images/${imageFileName4}" /&gt;  
    &lt;br/&gt;  
       &lt;img src="images/${imageFileName5}" /&gt;  
   &lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;

 

**配置Action的文件：**

&lt;action name="upload" class="com.student.action.UpLoadAction1"&gt;  
   &lt;interceptor-ref name="fileUpload"&gt;  
    &lt;param name="allowedTypes"&gt;  
     image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,image/JPG  
    &lt;/param&gt;  
    &lt;param name="maximumSize"&gt;1048576&lt;/param&gt;  
   &lt;/interceptor-ref&gt;  
   &lt;interceptor-ref name="defaultStack" /&gt;  
   &lt;result name="success"&gt;/success.jsp&lt;/result&gt;  
   &lt;result name="input"&gt;/upload.jsp&lt;/result&gt;  
  &lt;/action&gt;

 

 

来自：[http://blog.sina.com.cn/s/blog\_6145ed810100eb98.html](http://blog.sina.com.cn/s/blog_6145ed810100eb98.html)