`
roway
  • 浏览: 48182 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

struts2文件上传下载

 
阅读更多

一.struts1中的FormFile对于文件上传已经比较简单了,但struts2比它还简单。

二.要上传文件的注意事项:
a.表单的method一定要是post
b.enctype="multipart/form-data"
c.文件域

三.表单中默认的enctype="application/x-www-form-urlencoded":
  说明表单中的信息会作为一个字符串来提交。

四.文件的上传本身就是一个io的操作。

五.

在struts.xml中配置:这个表示是上传文件时使用的临时文件。
<constant name="struts.multipart.saveDir" value="c:\"></constant>

六.

action中对应文件上传域的属性设置(表单中文件上传域的name="file")

File file:
String fileFileName:对应表单域中文件上传域的name+FileName(固定的,有点像spring中的IOC)
String fileContentType:同上原则。
七.获取路径:

ServletActionContext().getRequest().getRealPath("/upload");

example1------>上传单个文件

upload.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>struts2处理文件上传</title>
    
  </head>
  
  <body>
    <form action="uploadAction" method="post"
			enctype="multipart/form-data">
			<table align="center" border="1">
				<tr>
					<td>
						UserName:
					</td>
					<td>
						<input type="text" name="username">
					</td>
				</tr>
				<tr>
					<td>
						File:
					</td>
					<td>
						<input type="file" name="file">
					</td>
				</tr>
				<tr>
					<td align="center">
						<input type="submit" value="提交">
					</td>
					<td align="center">
						<input type="button" value="重置">
					</td>
				</tr>
			</table>
		</form>
  </body>
</html>

UploadAction

//struts2处理单文件上传。
public class UploadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String username;

	private File file;

	// 应表单域中文件上传域的name+FileName(固定的,有点像spring中的IOC)
	private String fileFileName;

	// 同上
	private String fileContentType;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	@Override
	public String execute() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/upload");

		InputStream is = new FileInputStream(file);

		File file = new File(path, this.getFileFileName());

		OutputStream os = new FileOutputStream(file);

		byte[] b = new byte[1024];

		int bs = 0;

		while ((bs = is.read(b)) > 0) {
			os.write(b, 0, bs);
		}

		is.close();

		os.close();

		return SUCCESS;
	}
}

example2------》上传多个文件

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>struts2处理文件上传</title>
		<script type="text/javascript">
			function addMore(){
				//获取指定位置
				var td=document.getElementById("more");

				//创建要增加的标记
				var br=document.createElement("<br>");
				var input=document.createElement("<input>");
				var button=document.createElement("<input>");

				//设置标记的属性
				input.type="file";
				input.name="file";

				button.type="button";
				button.value="删   除";

				//设置按钮的单击事件
				button.onclick=function(){
					//移出指定父标记的指定子标记
					td.removeChild(br);
					td.removeChild(input);
					td.removeChild(button);
				}

				//增加指定父标记的指定子标记
				td.appendChild(br);
				td.appendChild(input);
				td.appendChild(button);
			}
		</script>
	</head>

	<body>
		<s:actionerror cssStyle="color:red;text-align:center" />

		<form action="uploadAction2_1" method="post"
			enctype="multipart/form-data">
			<table align="center" border="1" width="60%">
				<tr>
					<td>
						UserName:
					</td>
					<td>
						<input type="text" name="username">
					</td>
				</tr>
				<tr>
					<td>
						File:
					</td>
					<td id="more">
						<input type="file" name="file">
						<input type="button" value="上传更多..." onclick="addMore()">
					</td>
				</tr>
				<tr>
					<td align="center">
						<input type="submit" value="提交">
					</td>
					<td align="center">
						<input type="button" value="重置">
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

//struts2处理单文件上传。(集合和数组都可以实现多文件上传)
public class UploadAction2 extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String username;

	// 1-n文件的集合
	private List<File> file;

	private List<String> fileFileName;

	private List<String> fileContentType;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	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;
	}

	//上传指定的多个文件
	@Override
	public String execute() throws Exception {
		// 如果用户没有上传文件
		if (file != null) {

			String path = ServletActionContext.getServletContext().getRealPath(
					"/upload");
			for (int i = 0; i < file.size(); i++) {

				InputStream is = new FileInputStream(file.get(i));

				File file = new File(path, this.getFileFileName().get(i));

				OutputStream os = new FileOutputStream(file);

				byte[] b = new byte[1024];

				int bs = 0;

				while ((bs = is.read(b)) > 0) {
					os.write(b, 0, bs);
				}

				is.close();

				os.close();
			}
			return SUCCESS;
		} else {
			this.addActionError("请选择文件上传!");
			return INPUT;
		}
	}

	//上传任意数量的文件
	public String uploadmore() throws Exception {
		return this.execute();
	}

}

<action name="uploadAction2"
			class="com.luowei.struts2_01.action.UploadAction2">
			<result name="success">/uploadresult.jsp</result>
			<result name="input">/upload2.jsp</result>
			<interceptor-ref name="fileUpload">
				<param name="maximumSize">40960</param>
				<param name="allowedExtensions">txt,ppt,doc</param>
			</interceptor-ref>
			<interceptor-ref name="MyInterceptorStack"></interceptor-ref>
		</action>

下载

<action name="downloadAction"
			class="com.luowei.struts2_01.action.DownloadAction">
			<result name="success" type="stream">
				<param name="contentType">text/plain</param>
				<param name="contentDisposition">
					filename="spring精华.txt"
				</param>
				<param name="inputName">downloadFile</param>
			</result>
		</action>

		<action name="downloadAction2"
			class="com.luowei.struts2_01.action.DownloadAction">
			<result name="success" type="stream">
				<param name="contentType">text/plain</param>
				<param name="contentDisposition">
					filename="演讲稿.doc"
				</param>
				<param name="inputName">inputStream</param>
			</result>
		</action>

		<action name="downloadAction3"
			class="com.luowei.struts2_01.action.DownloadAction">
			<result name="success" type="stream">
				<param name="contentType">text/plain</param>
				<param name="contentDisposition">
					filename="spring精华.xls"
				</param>
				<param name="inputName">inputStream2</param>
			</result>
		</action>

		<action name="downloadAction4"
			class="com.luowei.struts2_01.action.DownloadAction">
			<result name="success" type="stream">
				<param name="contentType">
					application/vnd.ms-powerpoint
				</param>
				<param name="contentDisposition">
					filename="spring精华.txt"
				</param>
				<param name="inputName">inputStream3</param>
			</result>
		</action>

//文件下载的action
public class DownloadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	// 这个是文件下载的核心。
	public InputStream getDownloadFile() {
		return ServletActionContext.getServletContext().getResourceAsStream(
				"/upload/spring精华.txt");
	}

	// 这个是文件下载的核心。
	public InputStream getInputStream() {
		return ServletActionContext.getServletContext().getResourceAsStream(
				"/upload/演讲稿.doc");
	}
	
	// 这个是文件下载的核心。
	public InputStream getInputStream2() {
		return ServletActionContext.getServletContext().getResourceAsStream(
				"/upload/spring精华.xls");
	}
	
	// 这个是文件下载的核心。
	public InputStream getInputStream3() {
		return ServletActionContext.getServletContext().getResourceAsStream(
				"/upload/spring精华.txt");
	}
	
	@Override
	public String execute() throws Exception {

		return SUCCESS;
	}

}

1.contentType:要下载文件的类型(也是去tomcat/conf/web.xml去找对应的文件类型)默认为text/plain
2.contentLength:下载文件的大小,一般不用写。
3.contentDisipostion:下载对话框中文件的名称------》filename="指定文件名"(中文会乱码...待续)
4.inputName:重要!------》对应要下载的文件的action中的获取指定文件方法名称的......通过反射的方式去寻找)
5.bufferSize:缓存的大小,一般不用写

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics