struts文件上传demo

简介: 后台代码
//前端代码
<%@ page language="java" contentType="text/html; charset=gb2312"
  pageEncoding="gb2312"%>
<%@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=gb2312">
    <title>上传文件</title>
  </head>
  <body>
  <!-- 上传文件表单定义 -->
  <s:form action="upload" method="post" enctype="multipart/form-data">
    <tr>
  <!-- 上传文件标签定义 -->
  <td>上传文件:<s:file name="file"></s:file></td>
  </tr>
  <tr>
  <td>再次上传文件:<s:file name="file"></s:file></td>
  </tr>
  <tr>
  <td align="left"><s:submit name="submit" value="提交"></s:submit></td>
  </tr>
  </s:form>
  </body>
</html>

后台代码

package action;
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;
//文件上传Action
public class UploadAction extends ActionSupport {
  //上传文件存放路径
  private final static String UPLOADDIR = "/upload";
  //上传文件集合
  private List<File> file;
  //上传文件名集合
  private List<String> fileFileName;
  //上传文件内容类型集合
  private List<String> fileContentType;
  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;
  }
  public String execute() throws Exception {
    for (int i = 0; i < file.size(); i++) {
      //循环上传每个文件
      uploadFile(i);
    }
    return "success";
  }
  //执行上传功能
  private void uploadFile(int i) throws FileNotFoundException, IOException {
    try {
      InputStream in = new FileInputStream(file.get(i));
      String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
      File uploadFile = new File(dir, this.getFileFileName().get(i));
      OutputStream out = new FileOutputStream(uploadFile);
      byte[] buffer = new byte[1024 * 1024];
      int length;
      while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
      }
      in.close();
      out.close();
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}
目录
相关文章
|
XML Java 数据格式
|
前端开发 JavaScript Java
struts2的文件上传
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来比较繁琐,而且不灵活,在学习了struts2后,struts2为文件上传下载提供了更好的实现机制,在...
946 0
Struts2文件上传
1  在Struts2中上传文件需要 commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar 这两个包。  2  确认页面form表单上的提交方式为POST,enctype属性的值为“multipart/form-data”。
810 0