struts2.0实现多个文件上传的二种方法(通过数组和集合来实现)

时间:2022-05-24 03:59:50

一:所要的包

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

commons-logging-1.0.4.jar

commons-logging-api-1.1.jar

freemarker-2.3.8.jar

jakarta.jar

ognl-2.6.11.jar

struts2-core-2.0.8.jar

xwork-2.0.4.jar

二:上传文件jsp页面 FileUploadMany.jsp

<form action="upload.action" method="post" enctype="multipart/form-data">
 文件标题:<input type="text" name="title" /><br>
 选择第一个文件:<input type="file" name="upload" /><br>
 选择第二个文件:<input type="file" name="upload" /><br>
 选择第三个文件:<input type="file" name="upload" /><br>
 <input value="上传" type="submit" />
 </form>

三:struts.xml配置文件

<action name="upload" class="com.yysoft.uploadfile.FileUploadActionMany">
   <interceptor-ref name="fileUpload">  --该拦截器来处理
   <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>  --为图片类型
   </interceptor-ref>
   <interceptor-ref name="defaultStack"/>
   <param name="savePath">/images</param>
   <result name="input">/FileUploadMany.jsp</result>
   <result>/uploadsuccessMany.jsp</result>
  </action>

四:FileUploadActionMany.java action处理类  --一种方法,通过String数组来现实

package com.yysoft.uploadfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadActionMany extends ActionSupport {
 
 private String title;
 private File[] upload;
 private String[] uploadContentType;
 private String[] uploadFileName;
 private static final int BUFFER_SIZE = 16 * 1024 ;

// 接受依赖注入的属性

 private String savePath;

// 接受依赖注入的方法

 public void setSavePath(String value)

 {

 this.savePath = value;

 }


 private String getSavePath() throws Exception

 {

 return ServletActionContext.getRequest().getRealPath(savePath);

 }


 public void setTitle(String title) {

 this.title = title;

 }


 public void setUpload(File[] upload) {

 this.upload = upload;

 }


 public void setUploadContentType(String[] uploadContentType) {

 this.uploadContentType = uploadContentType;

 }


 public void setUploadFileName(String[] uploadFileName) {

 this.uploadFileName = uploadFileName;

 }


 public String getTitle() {

 return (this.title);

 }


 public File[] getUpload() {

 return (this.upload);

 }


 public String[] getUploadContentType() {

 return (this.uploadContentType);

 }


 public String[] getUploadFileName() {

 return (this.uploadFileName);

 }

 public String execute() throws Exception{
  File[] files = getUpload();
  FileOutputStream fos=null;
  FileInputStream fis=null;
  System.out.println("files:"+files);
  for (int i = 0 ; i < files.length ; i++){
  // 以服务器的文件保存地址和原文件名建立上传文件输出流
   System.out.println("getSavePath():"+getSavePath());
   System.out.println("getUploadFileName().get(i):"+getUploadFileName()[i]);
   System.out.println("files.get(i):"+files[i]);
   System.out.println("uploadContentType[i]"+uploadContentType[i]);
   
    fos= new FileOutputStream(getSavePath() + "//" + getUploadFileName()[i]);
    fis= new FileInputStream(files[i]);
  
   byte[] buffer = new byte[1024];
  
   int len = 0;
  
   while ((len = fis.read(buffer)) > 0){
    fos.write(buffer , 0 , len);
   }
   fos.close();
   fos=null;
   fis.close();
   fis=null;
  }
   return SUCCESS;
 }
}

五LFileUploadActionMany1.java 另一种方法,action处理类,通过List集合来操作

package com.yysoft.uploadfile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadActionMany1 extends ActionSupport {
 
 
   
    private static final int BUFFER_SIZE = 16 * 1024 ;

    private List <File> uploads = new ArrayList <File> ();
    private List <String> uploadFileNames = new ArrayList <String>();
    private List <String> uploadContentTypes = new ArrayList <String>();

   
   
    public List<File> getUploads() {
  return uploads;
 }
 public void setUploads(List<File> uploads) {
  this.uploads = uploads;
 }
 
 
 public List<String> getUploadsFileName(){
  return this.uploadFileNames;
 }
 public void setUploadsFileName(List<String> uploadFileName){
  this.uploadFileNames = uploadFileName;
 }
 

 
 private static void copy(File src, File dst)  {
        try  {
           InputStream in = null ;
           OutputStream out = null ;
            try  {               
               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                byte [] buffer = new byte [BUFFER_SIZE];
                while (in.read(buffer) > 0 )  {
                   out.write(buffer);
               }
            } finally  {
                if ( null != in)  {
                   in.close();
               }
                 if ( null != out)  {
                   out.close();
               }
           }
        } catch (Exception e)  {
           e.printStackTrace();
       }
   }
  
    private static String getExtention(String fileName)  {
        int pos = fileName.lastIndexOf( "." );
        return fileName.substring(pos);
   }

   @Override
    public String execute(){
    System.out.println("uploads"+uploads.size());
    System.out.println("uploadFileNames"+uploadFileNames.size());
    for(int i=0;i<uploads.size();i++){
     System.out.println("上传来了....................");
     System.out.println("myFile======="+uploads.get(i));
     System.out.println("fileName========="+uploadFileNames.get(i));
    
        String imageFileName = new Date().getTime()+ i + getExtention(uploadFileNames.get(i));
        System.out.println("imageFileName:"+imageFileName);
        File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/images" ) + "/" + imageFileName);
        System.out.println(uploads.get(i));
        System.out.println(imageFile);
        copy(uploads.get(i), imageFile);
    }
       return SUCCESS;
   }

}