struts2一个和多个文件上传及下载

struts2的文件上传相比我们自己利用第三方jar包(commons-fileupload-1.2.1.jar   commons-io-1.3.2.jar )要简单的多,当然struts2里面也是用的这两个jar包,不过人家封装好了,方便我们使用。

单文件上传:

1.action定义一个File变量,名字和表单name一样,文件会自动封装到这个对象中去

2.action定义一个String变量,名字是表单name名字+FileName,如下的myfileFileName,文件名字会自动封装到此对象中

3.工具类 FileUtils.copyFile(需要上传的文件, 目标文件)  ,如下FileUtils.copyFile(myfile, deskFile)

 

页面:

  <form action="upload" method="post" enctype="multipart/form-data">
 文件1:<input type="file" name="myfile"></input><br/>
 <input type="submit" value="上传"></input>
 </form>

xml:

<action name="upload" class="ni.jun.yang.action.UploadAction" >
            <param name="path">images</param>  <!-- 定义上传存放的文件夹名字-->
            <result name="success">welcome.jsp</result>
        </action>

action:

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class UploadAction implements Action{
    private File myfile;
    private String myfileFileName;
    private String path;
    public File getMyfile() {
        return myfile;
    }
    public void setMyfile(File myfile) {
        this.myfile = myfile;
    }
    public String getMyfileFileName() {
        return myfileFileName;
    }
    public void setMyfileFileName(String myfileFileName) {
        this.myfileFileName = myfileFileName;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    @Override
    public String execute() throws Exception {
        String filePath = ServletActionContext.getRequest().getRealPath(path);        
        File newFile = new File(filePath);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        File deskFile = new File(filePath + "/"+new Date().getTime()+"_"+myfileFileName); 
        FileUtils.copyFile(myfile, deskFile);
        return SUCCESS;
    }
    
}

多文件上传:

1.和单文件上传一样,只不过用数组或集合来接收文件对象,不过表单中type = file 的标签的name属性要一样,其他和单文件上传一样

页面:

<form action="upload2" method="post" enctype="multipart/form-data">
名字:<input type="text" name="myname"></input><br/>
 文件1:<input type="file" name="myfiles"></input><br/>
 文件2:<input type="file" name="myfiles"></input><br/>
  文件3:<input type="file" name="myfiles"></input><br/>
 <input type="submit" value="上传"></input>
 </form>

xml:

<action name="upload2" class="ni.jun.yang.action.UploadAction2" >
            <param name="path">images2</param>   <!-- 定义存放文件夹的名字-->
            <result name="success">image.jsp</result>
            <result name="error">upload.jsp</result>
        </action>

 

action:

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class UploadAction2 implements Action {
	private String myname;
	private List<File> myfiles;
	private List<String> myfilesFileName;
	private String path;
	private List<String> newName = new ArrayList<>();
		
	public List<String> getNewName() {
		return newName;
	}

	public String getMyname() {
		return myname;
	}

	public void setMyname(String myname) {
		this.myname = myname;
	}

	public List<File> getMyfiles() {
		return myfiles;
	}

	public void setMyfiles(List<File> myfiles) {
		this.myfiles = myfiles;
	}

	public List<String> getMyfilesFileName() {
		return myfilesFileName;
	}

	public void setMyfilesFileName(List<String> myfileFileName) {
		this.myfilesFileName = myfileFileName;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public String execute() throws Exception {
		boolean flag =  false;
		String filePath = ServletActionContext.getRequest().getRealPath(path);		
		File newFile = new File(filePath);
		if(!newFile.exists()){
			newFile.mkdirs();
		}
		List<File> list = getMyfiles();
		List<String> nameList = getMyfilesFileName();
//		System.out.println(list.size());
		if(list != null && list.size() >0){
			for (int i = 0; i < list.size(); i++) {
				String newPath =  new Date().getTime() + "_" + nameList.get(i);
				File deskFile = new File(filePath + "/" +newPath);
//				System.out.println(nameList.get(i));
				newName.add(newPath);
				FileUtils.copyFile(list.get(i), deskFile);
				flag = true;
			}
		}		
		if(flag){
			System.out.println(myname);
			return SUCCESS;
		}else{
			return ERROR;
		}		
	}
}

文件下载:  

1.文件下载和文件上传差不多,找到要下载的文件对象,和要下载到哪儿,创建2个File对象,然后就相当于是把 上传时候 FileUtils.copyFile(需要上传的文件, 目标文件)  中的两个参数换下位置

页面:jstl表达式显示上传的多个文件

<c:forEach items="${newName }" var="image">
 <img src="images2/${image }"><a href="download?filename=${image }">下载</a>
 </c:forEach>

  xml:

<action name="download" class="ni.jun.yang.action.DownloadAction" >
            <param name="path">image</param>
            <result name="success">welcome.jsp</result>
            <result name="error">image.jsp</result>
        </action>

action:

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class DownloadAction implements Action {
	private String filename;
	private String path;
	
	
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public String execute() throws Exception {
		String newPath = "D:\\"+path;  //定义下载的路径  
		String oldPath = ServletActionContext.getRequest().getRealPath("images2");//获取资源的路径
		System.out.println(newPath + "/" +filename);
		System.out.println(oldPath);
		File deskFile = new File(newPath + "/" +filename);  //目标对象
		File oldFile = new File(oldPath+"/"+filename);     //资源对象
		FileUtils.copyFile(oldFile, deskFile);		
		return SUCCESS;
	}

}

  

posted @ 2017-09-28 19:45  白露非霜  阅读(410)  评论(0编辑  收藏  举报
访问量