其实没什么好讲的~~~
利用struts2组件实现下载比上传更简单~~
废话不多说,直接来代码~~
download.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件下载</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:a href="download.action">12本Android手机开发考试大纲----下载</s:a> <br>
</body>
</html>
download.java
package com.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class download extends ActionSupport{
public InputStream getDownloadFile(){
return ServletActionContext.getServletContext().getResourceAsStream("/upload/12.doc");
}
public String execute(){
return "success";
}
}
srtruts.xml
<?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>
<package name="default" extends="struts-default">
<!-- 配置一个默认的action,当action不存在时,自动调用默认的action
注意:此默认action必须放在所有action最上面 -->
<default-action-ref name="dd" />
<action name="dd">
<result>/error.jsp</result>
</action>
<action name="download" class="com.action.download">
<result name="success" type="stream">
<param name="contentType">
applaction/vnd.ms-powerpoint
</param>
<param name="contentDisposition">
filename="12.doc"
</param>
<param name="inputName">downloadFile</param>
</result>
</action>
</package>
</struts>
好了,在这里不想多做解释~~,相信大家一看就懂!!
需要注意的一点是<param name="inputName">downloadFile</param>中的“downloadFile”是跟action中的“getDownloadFile()”方法对应的,名字别弄错了~~
其他没什么了~~~
当然,这里面还涉及到汉字乱码,参数传递的问题,这个也没什么讲的,大家自己在下面自己实现吧~~~~
看一下运行截图: