struts2文件上传

文件的上传和下载分为单个文件上传和多个文件上传

一  单个的文件上传

.1.首先写一个jsp页面:

         <body>

   <div align="center">

        <h1>单个文件上传和多个文件上传</h1>

        <form action="${pageContext.request.contextPath}/csdn/upFile.action" enctype="multipart/form-data" method="post">

           上传文件:<input type="file" name="upload"/>

           <input type="submit" value="上传"/>

        </form>

   </div>

  </body>

 

2.访问到的struts.xml文件

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="upload" namespace="/csdn" extends="struts-default">

       <action name="upFile" class="cn.csdn.hr.up.action.UploadAction" method="upload">

           <result>../success.jsp</result>

       </action>

    </package>

</struts>

3.访问到UploadAction.java的action  

package cn.csdn.hr.up.action;

 

import java.io.File;

import java.io.IOException;

 

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UploadAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

 

    /**

     都是规定,File的对象和input中的name(xxx)相同,文件类型和文件名都是xxxContextType,xxxFileName

     */

 

    // 得到上传文件的名称一定与name值一直

    private File upload;

    // 上传文件的类型 ContentType

    private String uploadContentType;

    // 上传文件的名称

    private String uploadFileName;

 

    public File getUpload() {

       return upload;

    }

 

    public void setUpload(File upload) {

       this.upload = upload;

    }

 

    public String getUploadContentType() {

       return uploadContentType;

    }

 

    public void setUploadContentType(String uploadContentType) {

       this.uploadContentType = uploadContentType;

    }

 

    public String getUploadFileName() {

       return uploadFileName;

    }

 

    public void setUploadFileName(String uploadFileName) {

       this.uploadFileName = uploadFileName;

    }

 

    public static long getSerialversionuid() {

       return serialVersionUID;

    }

 

    public String upload() {

       String path = ServletActionContext.getServletContext().getRealPath(

              "/upload");

       // 写到指定路径

       File file = new File(path);

       //判断指定的路径下是否有uplaod,如果没有,自动创建

       if (!file.exists()) {

           file.mkdirs();

       }

       try {

           FileUtils.copyFile(upload, new File(file, uploadFileName));

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       return SUCCESS;

    }

}

二  多个文件的上传

多个文件的上传不同的是在action中的获取到的是数组或者是list集合

 

数组的action为:

package cn.csdn.hr.up.action;

 

import java.io.File;

import java.io.IOException;

 

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UploadsAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

 

    // 得到上传文件的名称一定与name值一直

    private File upload[];

    // 上传文件的类型 ContentType

    private String uploadContentType[];

    // 上传文件的名称

    private String uploadFileName[];

 

    public File[] getUpload() {

       return upload;

    }

 

    public void setUpload(File[] upload) {

       this.upload = upload;

    }

 

    public String[] getUploadContentType() {

       return uploadContentType;

    }

 

    public void setUploadContentType(String[] uploadContentType) {

       this.uploadContentType = uploadContentType;

    }

 

    public String[] getUploadFileName() {

       return uploadFileName;

    }

 

    public void setUploadFileName(String[] uploadFileName) {

       this.uploadFileName = uploadFileName;

    }

 

    public static long getSerialversionuid() {

       return serialVersionUID;

    }

 

    public String uploads() {

       String path = ServletActionContext.getServletContext().getRealPath(

              "/upload");

 

       // 写到指定路径

       File file = new File(path);

       //判断指定的路径下是否有uplaod,如果没有,自动创建

       if (!file.exists()) {

           file.mkdirs();

       }

       try {

           for(int i = 0;i<upload.length;i++){

              FileUtils.copyFile(upload[i], new File(file, uploadFileName[i]));

           }

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       System.out.println("上传文件的名称:" + uploadFileName + "上传的路径:" + path

              + "上传的类型:" + uploadContentType);

       return SUCCESS;

    }

}

 

 

List集合的action为:;

 

 

 

package cn.csdn.hr.up.action;

 

import java.io.File;

import java.io.IOException;

import java.util.List;

 

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UploadListAction extends ActionSupport {

         private static final long serialVersionUID = 1L;

         // 得到上传文件的名称一定与name值一直

         private List<File> upload;

         // 上传文件的类型 ContentType

         private List<String> uploadContentType;

         // 上传文件的名称

         private List<String> uploadFileName;

 

         public List<File> getUpload() {

                   return upload;

         }

 

         public void setUpload(List<File> upload) {

                   this.upload = upload;

         }

 

         public List<String> getUploadContentType() {

                   return uploadContentType;

         }

 

         public void setUploadContentType(List<String> uploadContentType) {

                   this.uploadContentType = uploadContentType;

         }

 

         public List<String> getUploadFileName() {

                   return uploadFileName;

         }

 

         public void setUploadFileName(List<String> uploadFileName) {

                   this.uploadFileName = uploadFileName;

         }

 

         public static long getSerialversionuid() {

                   return serialVersionUID;

         }

 

         public String uploadList() {

                   String path = ServletActionContext.getServletContext().getRealPath(

                                     "/upload");

 

                   // 写到指定路径

                   File file = new File(path);

                   // 判断指定的路径下是否有uplaod,如果没有,自动创建

                   if (!file.exists()) {

                            file.mkdirs();

                   }

        

                   try {

                            for (int i = 0; i < upload.size(); i++) {

                                     FileUtils.copyFile(upload.get(i), new File(file, uploadFileName

                                                        .get(i)));

                            }

                   } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   }

                   System.out.println("上传文件的名称:" + uploadFileName + "上传的路径:" + path

                                     + "上传的类型:" + uploadContentType);

                   return SUCCESS;

         }

 

}


你可能感兴趣的:(struts,String,upload,File,action,import)