struts2 下传文件

struts2 上传文件

作者:ThirdteenDevil 十三妖

qq : 181907667

 

我感觉struts2的上传比struts1上传要好,struts1用到的是FormFile将上传的文件封装起来,而struts2是直接用的File类,更容易操作。闲言少叙 见代码

 

jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>struts2 upload</title>
  </head>
  <body>
     <form action="multiFileUpload.action" method="POST" enctype="multipart/form-data">
        文件标题:<input type="text" name="title" size="50"/><br/>
        选择文件:<input type="file" name="upload" size="50"/><br/>
    
        选择文件:<input type="file" name="upload" size="50"/><br/>
      
        选择文件:<input type="file" name="upload" size="50"/><br/>
       <input type="submit" value=" 上传 "/>       
    </form>
  </body>
</html>

 注意表单属性enctype="multipart/form-data!上传必须加这个属性!

 

创建处理上传请求的Action类

package action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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 MultiFileUploadAction extends ActionSupport {
	private static final int BUFFER_SIZE = 16 * 1024;
    // 文件标题
    private String title;
    // 用File数组来封装多个上传文件域对象
    private File[] upload;
    // 用String数组来封装多个上传文件名
    private String[] uploadFileName;
    // 用String数组来封装多个上传文件类型
    private String[] uploadContentType;
    // 保存文件的目录路径(通过依赖注入)
    private String savePath;
    // 以下为所有属性的getter和setter。省略。。。
    // 自己封装的一个把源文件对象复制成目标文件对象
    private 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[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public String execute() throws Exception {
        File[] srcFiles = this.getUpload();
        // 处理每个要上传的文件
        for (int i = 0; i < srcFiles.length; i++) {
            // 根据服务器的文件保存地址和原文件名创建目录文件全路径
            String dstPath = ServletActionContext.getServletContext()
                    .getRealPath(this.getSavePath())
                    + "\\" + this.getUploadFileName()[i];
            File dstFile = new File(dstPath);
            this.copy(srcFiles[i], dstFile);
        }
        return SUCCESS;
    }
	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[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
}

 

 

配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="s2" extends="struts-default">
    	
        <action name ="multiFileUpload" 
class ="action.MultiFileUploadAction">
<!-- 动态设置Action中的savePath属性的值 -->
            <param name="savePath">/upload</param>
            <result name ="success">/index.jsp</result>
        </action >
    </package>
</struts>