简单的下载功能实现(struts2)

[align=center][size=xx-large][color=red][b]简单的下载功能实现(struts2)[/b][/color][/size][/align][size=medium][color=red][b]1.前台页面:[/b][/color][/size]

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript">
function changeValidateCode(obj) {
/***
* 获取当前的时间作为参数,无具体意义
* 每次请求需要一个不同的参数,否则可能会返回同样的验证码
* 这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。
*/
var timenow = new Date().getTime();

obj.src="randPic.action?d="+timenow;
}
</script>
</head>

<body>
<form name="" action="Login">
验证码:<s:textfield name="code"></s:textfield><img src="randPic.action" onclick="changeValidateCode(this)" title="点击图片刷新验证码"/>
<br/><input type="submit" value="登陆"/><input type="reset" value="重置"/>
</form>

<s:a href="download/view">查看图片</s:a><br/>
<s:a href="download/save">下载图片</s:a>
</body>
</html>



[size=medium][color=red][b]2.struts.xml配置:[/b][/color][/size]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="image" extends="struts-default" namespace="/">
<action name="randPic" class="com.org.momo.action.RandomAction">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>

<action name="Login" class="com.org.momo.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>
</package>

<package name="download_pic" extends="struts-default" namespace="/download">
<action name="view" class="com.org.momo.action.SimpleFileDownloadAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="contentDisposition">filename="picture.jpg"</param>
<param name="inputName">simpleDownloadStream</param>
</result>
</action>
<action name="save" class="com.org.momo.action.SimpleFileDownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">filename="picture.jpg"</param>
<param name="inputName">simpleDownloadStream</param>
</result>
</action>

</package>
</struts>


[size=medium][color=red][b]3.SimpleFileDownloadAction.java[/b][/color][/size]

package com.org.momo.action;

import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;

public class SimpleFileDownloadAction extends ActionSupport implements ServletContextAware
{
private static final long serialVersionUID = 620526993016670680L;
private ServletContext context;

public void setServletContext(ServletContext context){
this.context = context;
}

public InputStream getSimpleDownloadStream(){
return context.getResourceAsStream("/download/10.JPG");
}

public String execute(){
return SUCCESS;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值