struts2 实现文件上传和下载

项目目录:


web.xml:主要是配好struts2.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>uploadTest</display-name>

	<!-- struts2配置 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

struts.xml:配好上传下载的两个action。

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

<struts>

	<constant name="struts.devMode" value="true" />

	<package name="default" namespace="/" extends="struts-default">
		
		<!-- 上传 -->
		<action name="uploadAction" class="upload.UploadAction">
			<result>index.jsp</result>
			<result name="input">index.jsp</result>

			<interceptor-ref name="fileUpload">
				<!-- 不写下面两个参数为允许所有大小所有类型的文件的上传 -->
				<param name="maximumSize">1024000</param> 
				<param name="allowedTypes">
					application/msword,image/jpeg
				</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack" />

		</action>
		
		<!-- 下载 -->
		<action name="downloadAction" class="upload.DownloadAction">
			<result type="stream">
				<!-- 文件类型 —— application/octet-stream 表示无限制 -->
				<param name="contentType">application/octet-stream</param>
				<!-- 流对象名 —— 去找Action中的getInputStream方法 -->
                <param name="inputName">inputStream</param>
                <!-- 文件名 —— 将调用该Action中的getFileName方法 -->
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">4096</param>
			</result>
		</action>
	</package>

</struts>

UploadAction.java

package upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private File myFile;//上传的文件,对应表单的file的name属性
	private String myFileContentType;//文件类型,xxxContentType,xxx对应表单file的name属性
	private String myFileFileName;

	@Override
	public String execute() throws Exception {
		if (myFile == null) {
			this.addFieldError("file", "文件不能为空,请选择");
			return INPUT;
		} else {
			InputStream is = new FileInputStream(this.getMyFile());
			OutputStream os = new FileOutputStream(new File("F:/", this.getMyFileFileName()));
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = is.read(buf)) > 0) {
				os.write(buf, 0, length);
			}
			is.close();
			os.close();
		}
		return SUCCESS;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

}

DownloadAction.java

package upload;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private String fileName;//要下载的文件名
	private InputStream inputStream;

	@Override
	public String execute() throws Exception {
		inputStream = ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
		return SUCCESS;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
}

index.jsp:访问:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!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>Insert title here</title>
</head>
<body>

	<!-- 上传 -->
	<s:form action="uploadAction" enctype="multipart/form-data" method="post">
		<s:file name="myFile"></s:file>
		<s:submit />
	</s:form>
	
	<!-- 下载 -->
	<a href="downloadAction?fileName=index.jsp">下载index.jsp文件</a>
	
</body>
</html>

就这样就可以实现基本的上传下载功能了,要实现复杂一点的再往上面加东西。


参考博客:http://www.blogjava.net/thisliy/archive/2009/08/14/291153.html

你可能感兴趣的:(struts2,upload,下载,上传)