原创:多个文件的上传

来源:互联网 发布:面向切面编程原理 编辑:程序博客网 时间:2024/04/29 03:21

多个文件上传的步骤:
第一步创建视图界面:
  <h1>
   多个文件的上传
  </h1>
  <s:form action="./uploads.action" method="post"
   enctype="multipart/form-data">
   <s:textfield name="title" label="标题"></s:textfield>
   <s:file name="upload" label="上传文件1"></s:file>
   <s:file name="upload" label="上传文件2"></s:file>
   <s:file name="upload" label="上传文件3"></s:file>
   <s:file name="upload" label="上传文件4"></s:file>
   <s:submit value="上传"></s:submit>

  </s:form>
第二步:创建配置文件的内容:
    <package name="test" extends="struts-default" namespace="/">
       
        <global-results>
            <result name="input">/index.jsp</result>
        </global-results>
       
        <action name="uploads" class="edu.ac.UploadsAction"  method="uploadFiles">
          <param name="savePath">/uploads</param>
           <result>/sc.jsp</result>
        </action>
    
    </package>

第三:创建使用Array的Action类
package edu.ac;

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 org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadsAction extends ActionSupport {
 /** 封装请求参数的普通表单名称 */
 private String title;
 /** 采用数组封装多个文件域的文件内容 */
 private File[] upload;
 /** 采用数组封装多个文件域的文件类型 文件域名称 ContentType */
 private String[] uploadContentType;
 /** 采用数组封装多个文件域的文件的名称 文件域名称 FileName */
 private String[] uploadFileName;
 /** 接受文件依赖注入保存文件路径属性 */
 private String savePath;

 /** 通过set get 方法获取传递参数 */

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 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 String getSavePath() {
  return ServletActionContext.getRequest().getRealPath(savePath);
 }

 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }

 public String uploadFiles() {
  System.out.println("---------多文件上传正在使用Array处理中------------");
  // 取得文件的保存路径
  File file = new File(getSavePath());
  if (!file.exists()) {
   file.mkdirs();
  }
  // 取得文件上传的文件数组
  File[] files = getUpload();
  /** 输入流:进行读取操作 */
  InputStream is = null;
  /** 输出流:进行写操作 */
  OutputStream os = null;

  try {
   for (int i = 0; i < files.length; i++) {
    /** 读取文件的创建 */
    is = new BufferedInputStream(new FileInputStream(files[i]));
    /** 写入文件的创建 */
    os = new BufferedOutputStream(new FileOutputStream(
      getSavePath() + "//" + getUploadFileName()[i]));
    byte buffer[] = new byte[1024];
    int len = 0;
    while ((len = is.read(buffer)) > 0) {
     os.write(buffer, 0, len);
     os.flush();/**立即写入*/
    }

   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    /** 关闭流 */
    if (is != null) {
     is.close();
    }
    if (os != null) {
     os.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  return SUCCESS;
 }

}

第五步:创建成功界面显示效果:
 <h1>显示所有上传的文件</h1>
       
        <s:iterator var="filename" value="uploadFileName">
           <img src="<s:property value='%{savePath}'/>/<s:property value='%{filename}'/>"/>
        </s:iterator>
第六步:也可以采用List实现上传
package edu.ac;

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.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadsListAction extends ActionSupport {
 /** 封装请求参数的普通表单名称 */
 private String title;
 /** 采用List封装多个文件域的文件内容 */
 private List<File> upload;
 /** 采用List封装多个文件域的文件类型 文件域名称 ContentType */
 private List<String> uploadContentType;
 /** 采用List封装多个文件域的文件的名称 文件域名称 FileName */
 private List<String> uploadFileName;
 /** 接受文件依赖注入保存文件路径属性 */
 private String savePath;

 /** 通过set get 方法获取传递参数 */

 public String getSavePath() {
  return ServletActionContext.getRequest().getRealPath(savePath);
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 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 void setSavePath(String savePath) {
  this.savePath = savePath;
 }

 public String uploadFiles() {
  System.out.println("---------多文件上传正在使用List处理中------------");
  // 取得文件的保存路径
  File file = new File(getSavePath());
  if (!file.exists()) {
   file.mkdirs();
  }
  // 取得文件上传的文件List
  List<File> files = getUpload();
  /** 输入流:进行读取操作 */
  InputStream is = null;
  /** 输出流:进行写操作 */
  OutputStream os = null;

  try {
   for (int i = 0; i < files.size(); i++) {
    /** 读取文件的创建 */
    is = new BufferedInputStream(new FileInputStream(files.get(i)));
    /** 写入文件的创建 */
    os = new BufferedOutputStream(new FileOutputStream(
      getSavePath() + "//" + getUploadFileName().get(i)));
    byte buffer[] = new byte[1024];
    int len = 0;
    while ((len = is.read(buffer)) > 0) {
     os.write(buffer, 0, len);
     os.flush();/**立即写入*/
    }

   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    /** 关闭流 */
    if (is != null) {
     is.close();
    }
    if (os != null) {
     os.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  return SUCCESS;
 }

}
/**可以简单修改配置文件中Action的Class内容进行测试 修改后的结果如下*/
    <package name="test" extends="struts-default" namespace="/">
       
        <global-results>
            <result name="input">/index.jsp</result>
        </global-results>
       
        <action name="uploads" class="edu.ac.UploadsListAction"  method="uploadFiles">
          <param name="savePath">/uploads</param>
           <result>/sc.jsp</result>
        </action>
    
    </package>

备注:以上是采用的上传的都是图片的测试。