您的位置:首页 > 编程语言 > Java开发

Struts2上传与下载(二)- 多文件上传

2013-05-25 10:50 375 查看
其实,回了单文件上传的话,多文件也是一样的道理

1.upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>文件上传示例</title>
</head>
<body>
<h3>1.单文件上传</h3>
<form action="file/common!upload" method="post" enctype="multipart/form-data">
请选择文件:<input type="file" name="sgFile"><br/>

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

<h3>2.多文件上传</h3>
<form action="file/common!upload2" method="post" enctype="multipart/form-data">
请选择文件:<input type="file" name="mulFile"><br/>
请选择文件:<input type="file" name="mulFile"><br/>
请选择文件:<input type="file" name="mulFile"><br/>
请选择文件:<input type="file" name="mulFile"><br/>

<input type="submit" value="上传">
</form>
</body>
</html>

2.FileAction

package com.deppon.demo.struts.action;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.deppon.demo.struts.util.FileUtil;
import com.opensymphony.xwork2.ActionSupport;

public class FileAction extends ActionSupport {
private static final long serialVersionUID = 3130770865205322375L;

/**
* 这里是有一个对应关系的
*/
private File sgFile;//xxx
private String sgFileFileName;//xxxFileName
private String sgFileContentType;//xxxContentType

private File[] mulFile;
private String[] mulFileFileName;

public File[] getMulFile() {
return mulFile;
}

public void setMulFile(File[] mulFile) {
this.mulFile = mulFile;
}

public String[] getMulFileFileName() {
return mulFileFileName;
}

public void setMulFileFileName(String[] mulFileFileName) {
this.mulFileFileName = mulFileFileName;
}

public File getSgFile() {
return sgFile;
}

public void setSgFile(File sgFile) {
this.sgFile = sgFile;
}

public String getSgFileFileName() {
return sgFileFileName;
}

public void setSgFileFileName(String sgFileFileName) {
this.sgFileFileName = sgFileFileName;
}

public String getSgFileContentType() {
return sgFileContentType;
}

public void setSgFileContentType(String sgFileContentType) {
this.sgFileContentType = sgFileContentType;
}

/**
* 单文件上传
* @return
*/
public String upload() {
System.out.println("sgFileFileName->" + sgFileFileName);//可以获取文件的名字
System.out.println("sgFileContentType->" + sgFileContentType);

/*
* 这里获取项目的绝对路径,有2种方法
* 目的都是获取ServletContext,然后,通过getRealPath(),来获取路径。
*
*/

//1.使用ActionContext
// ActionContext ac = ActionContext.getContext();
// ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
// String path = sc.getRealPath("/data");

//2.使用ServletActionContext
StringBuilder path = new StringBuilder(ServletActionContext.getServletContext().getRealPath("/data"));
path.append(File.separator);
path.append(sgFileFileName);

FileUtil.save(sgFile, path.toString());

return SUCCESS;
}

/**
* 多文件上传
* @return
*/
public String upload2() {
//输出文件名
for(String fileName : mulFileFileName) {
System.out.println("--------->" + fileName);
}

String base = ServletActionContext.getServletContext().getRealPath("/data");
StringBuilder path = null;
for(int i=0; i<mulFile.length; i++) {
path = new StringBuilder(base);
path.append(File.separator);
path.append(mulFileFileName[i]);

FileUtil.save(mulFile[i], path.toString());
}

return SUCCESS;
}

}

3.struts.xml

嗯,这个没有变,和上一篇《单文件》一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: