struts2 文件上传和下载

login.jsp:

<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java"
	errorPage=""%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
</head>
<body>
	<h3>登录页面</h3>
	<s:form action="login">
		<s:textfield name="user" label="用户名" />
		<s:textfield name="pass" label="密码" />
		<s:submit value="登录" />
	</s:form>
</body>
</html>
stuts2DownUpload.jsp:

<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java"
	errorPage=""%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Struts 2的文件上传和下载</title>
</head>

<body>
	<h1>Struts 2的文件上传</h1>
	<s:form action="uploadPro" enctype="multipart/form-data">
		<s:textfield name="title" label="文件标题" />
		<br />
		<s:file name="upload" label="选择文件" />
		<br />
		<s:submit value="上传" />
	</s:form>


	<h1>Struts 2的文件下载</h1>
	<ul>
		<li>下载疯狂Java联盟的Logo: <a href="download.action">下载图形文件</a>
		</li>
		<li>下载疯狂Java联盟的Logo的压缩文件: <a href="download2.action">下载压缩文件</a>
		</li>
	</ul>
</body>
</html>
succ.jsp:

<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java"
	errorPage=""%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>上传成功</title>
</head>
<body>
	上传成功!
	<br /> 文件标题:
	<s:property value=" + title" />
	<br /> 文件为:
	<img src="<s:property value="'uploadFiles/' 
		+ uploadFileName"/>" />
	<br />

</body>
</html>
struts.xml:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<!-- 配置Struts 2国际化资源文件的baseName -->
	<constant name="struts.custom.i18n.resources" value="mess" />
	<!-- 配置Struts 2应用的编码集 -->
	<constant name="struts.i18n.encoding" value="GBK" />
	<package name="lee" extends="struts-default">
		<action name="download" class="org.crazyit.app.action.FileDownloadAction">
			<!-- 指定被下载资源的位置 -->
			<param name="inputPath">\images\疯狂联盟.jpg</param>
			<!-- 配置结果类型为stream的结果 -->
			<result name="success" type="stream">
				<!-- 指定下载文件的文件类型 -->
				<param name="contentType">image/jpg</param>
				<!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
				<param name="inputName">targetFile</param>
				<!-- 显示在下载文件筐中的文件名 -->
				<param name="contentDisposition">filename="wjc_logo.jpg"</param>
				<!-- 指定下载文件的缓冲大小 -->
				<param name="bufferSize">4096</param>
			</result>
		</action>

		<action name="download2" class="org.crazyit.app.action.AuthorityDownAction">
			<!-- 定义被下载文件的物理资源 -->
			<param name="inputPath">\images\wjc_logo.zip</param>
			<result name="success" type="stream">
				<!-- 指定下载文件的文件类型 -->
				<param name="contentType">application/zip</param>
				<!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
				<!-- 在action中获得 -->
				<param name="inputName">targetFile</param>
				<!-- 下载框现显示的名字 -->
				<param name="contentDisposition">filename="wjc_logo.zip"</param>
				<!-- 指定下载文件的缓冲大小 -->
				<param name="bufferSize">4096</param>
			</result>
			<!-- 定义一个名为login的结果 -->
			<result name="login">/WEB-INF/content/login.jsp</result>
		</action>

		<action name="login" class="org.crazyit.app.action.LoginAction">
			<result>/WEB-INF/content/stuts2DownUpload.jsp</result>
		</action>


		<!-- 配置处理文件上传的Action -->
		<action name="uploadPro" class="org.crazyit.app.action.UploadAction">
			<!-- 动态设置Action的属性值 -->
			<param name="savePath">/images</param>
			<!-- 配置Struts 2默认的视图页面 -->
			<result>/WEB-INF/content/succ.jsp</result>
		</action>
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>

	</package>
</struts>
LoginAction.java:

package org.crazyit.app.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class LoginAction implements Action 
{
	private String user;
	private String pass;
	
	//user属性的setter和getter方法
	public void setUser(String user)
	{
		this.user = user;
	}
	public String getUser()
	{
		return this.user;
	}

	//pass属性的setter和getter方法
	public void setPass(String pass)
	{
		this.pass = pass;
	}
	public String getPass()
	{
		return this.pass;
	}
	
	public String execute()
	{
		ActionContext.getContext().getSession()
			.put("user" , getUser());
		return SUCCESS;
	}
}
UploadAction.java:

package org.crazyit.app.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.*;

/**
 * Description: <br/>
 * 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> <br/>
 * Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
 * This program is protected by copyright laws. <br/>
 * Program Name: <br/>
 * Date:
 * 
 * @author Yeeku.H.Lee kongyeeku@163.com
 * @version 1.0
 */

public class UploadAction implements Action {
	// 封装文件标题请求参数的属性
	private String title;
	// 封装上传文件域的属性
	private File upload;
	// 封装上传文件类型的属性
	private String uploadContentType;
	// 封装上传文件名的属性
	private String uploadFileName;
	// 直接在struts.xml文件中配置的属性
	private String savePath;

	// 接受struts.xml文件配置值的方法
	public void setSavePath(String value) {
		this.savePath = value;
	}

	// 返回上传文件的保存位置
	private String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	// 文件标题的setter和getter方法
	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return (this.title);
	}

	// 上传文件对应文件内容的setter和getter方法
	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return (this.upload);
	}

	// 上传文件的文件类型的setter和getter方法
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadContentType() {
		return (this.uploadContentType);
	}

	// 上传文件的文件名的setter和getter方法
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return (this.uploadFileName);
	}

	@Override
	public String execute() throws Exception {
		// 以服务器的文件保存地址和原文件名建立上传文件输出流
		FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
				+ getUploadFileName());
		FileInputStream fis = new FileInputStream(getUpload());
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();
		System.out.println(uploadFileName);
		System.out.println(getSavePath() + "\\" + getUploadFileName());
		return SUCCESS;
	}
}
FileDownloadAction.java:

package org.crazyit.app.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.*;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class FileDownloadAction
	extends ActionSupport 
{
	//该属性可以在配置文件中动态指定该属性值
	private String inputPath;
	//依赖注入该属性值的setter方法
	public void setInputPath(String value)
	{
		inputPath = value;
	}
	/*
	定义一个返回InputStream的方法,
	该方法将作为被下载文件的入口,
	且需要配置stream类型结果时指定inputName参数,
	inputName参数的值就是方法去掉get前缀、首字母小写的字符串
	*/
	public InputStream getTargetFile() throws Exception 
	{
		//ServletContext提供getResourceAsStream()方法
		//返回指定文件对应的输入流 
		return ServletActionContext.getServletContext()
			.getResourceAsStream(inputPath);
	}
}
AuthorityDownAction.java:

package org.crazyit.app.action;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.*;

import java.util.Map;
import java.io.InputStream;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class AuthorityDownAction
	implements Action 
{
	private String inputPath;
	public void setInputPath(String value)
	{
		inputPath = value;
	}
	
	public InputStream getTargetFile() throws Exception 
	{
		//ServletContext提供getResourceAsStream()方法
		//返回指定文件对应的输入流 
		return ServletActionContext.getServletContext()
			.getResourceAsStream(inputPath);
	}
	
	public String execute() throws Exception
	{
		//取得ActionContext实例
		ActionContext ctx = ActionContext.getContext();
		//通过ActionContext访问用户的HttpSession
		Map session = ctx.getSession();
		String user = (String)session.get("user");
		//判断Session里的user是否通过检查
		if ( user !=  null && user.equals("chao"))
		{
			return SUCCESS;
		}
		ctx.put("tip"
			, "您还没有登录,或者登录的用户名不正确,请重新登录!");
		return LOGIN;
	}
}
工程代码地址:http://download.csdn.net/detail/u014071669/7197847

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值