一、准备工作:
首先,你要确定你下载的文件,是存放在服务器中还是存在数据库中,一般存在数据库中的都是比较大的项目,我们常用的就是将文件保存在服务器中,下面就是我关于存在在服务器中文件的下载。
其次,你要准备Struts 2的jar包,创建一个工程,导入jar包,配置好Struts 2,这都是很简单的了,不需要我在详细配置了,O(∩_∩)O~
二、下载
1、web代码:
一帮都经常是一个超链接,就可以下载了,我们现在也模拟此功能,代码如下:
2、Action代码:
private String newfilename; // 文件名称(文件在服务器上的名称)
/**
* Struts 2 下载主程序
* @return
*/
public String fileDownload(){
newfilename = ""; // 通过数据库查询得到要下载文件的名称
if(ServletActionContext.getServletContext().getResourceAsStream("/"+UploadConfigurationRead.getInstance().getConfigItem("downloadFilePath").trim()+"/" + newfilename) == null){
ServletActionContext.getRequest().getSession().setAttribute("ErrorMsg", "资源因故丢失");
return "downloadError";
} else {
ServletActionContext.getRequest().getSession().setAttribute("Msg", "恭喜,下载成功");
return "fileDownload";
}
}
public void setFileName(){
String fileName = newfilename;
try {
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.fileName = fileName;
}
public String getFileName(){
try {
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
public InputStream getDownloadFile(){
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream("/"+UploadConfigurationRead.getInstance().getConfigItem("downloadFilePath").trim()+"/" + newfilename);
}
/**
* setter、getter Method
* @return
*/
public String getNewfilename() {
return newfilename;
}
public void setNewfilename(String newfilename) {
this.newfilename = newfilename;
}
}
3、UploadConfigurationRead类
/**
* 动态读取配置文件类
*
* @author zontin - wlf
* @date 2011-03-08 15:00
*
*/
public class UploadConfigurationRead {
/**
* 属性文件全名,需要的时候请重新配置PFILE
*/
private static String PFILE = "upload.properties";
/**
* 配置文件路径
*/
@SuppressWarnings("unused")
private URI uri = null;
/**
* 属性文件所对应的属性对象变量
*/
private long m_lastModifiedTime = 0;
/**
* 对应于属性文件的文件对象变量
*/
private File m_file = null;
/**
* 属性文件所对应的属性对象变量
*/
private Properties m_props = null;
/**
* 唯一实例
*/
private static UploadConfigurationRead m_instance = new UploadConfigurationRead();
/**
* 私有构造函数
*
* @throws URISyntaxException
*/
private UploadConfigurationRead() {
try {
m_lastModifiedTime = getFile().lastModified();
if (m_lastModifiedTime == 0) {
System.err.println(PFILE + "file does not exist!");
}
m_props = new Properties();
m_props.load(new FileInputStream(getFile()));
} catch (URISyntaxException e) {
System.err.println(PFILE+"文件路径不正确");
e.printStackTrace();
} catch (Exception e) {
System.err.println(PFILE+"文件读取异常");
e.printStackTrace();
}
}
/**
* 查找ClassPath路径获取文件
*
* @return File对象
* @throws URISyntaxException
*/
private File getFile() throws URISyntaxException {
URI fileUri = this.getClass().getClassLoader().getResource(PFILE).toURI();
m_file = new File(fileUri);
return m_file;
}
/**
* 静态工厂方法
*
* @return 返回ConfigurationRead的单一实例
*/
public synchronized static UploadConfigurationRead getInstance() {
return m_instance;
}
/**
* 读取一特定的属性项
*/
public String getConfigItem(String name, String defaultVal) {
long newTime = m_file.lastModified();
// 检查属性文件是否被修改
if (newTime == 0) {
// 属性文件不存在
if (m_lastModifiedTime == 0) {
System.err.println(PFILE + " file does not exist!");
} else {
System.err.println(PFILE + " file was deleted!!");
}
return defaultVal;
} else if (newTime > m_lastModifiedTime) {
m_props.clear();
try {
m_props.load(new FileInputStream(getFile()));
} catch (Exception e) {
System.err.println("文件重新读取异常");
e.printStackTrace();
}
}
m_lastModifiedTime = newTime;
String val = m_props.getProperty(name);
if (val == null) {
return defaultVal;
} else {
return val;
}
}
/**
* 读取一特定的属性项
*
* @param name
* 属性项的项名
* @return 属性项的值(如此项存在), 空(如此项不存在)
*/
public String getConfigItem(String name) {
return getConfigItem(name, "");
}
}
4、Struts 2 配置文件struts.xml代码如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="cn.com" extends="struts-default">
<action name="download_*" class="cn.com.downloadAction" method="{1}">
<result name="fileDownload" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">downloadFile</param>
</result>
<result name="downloadError">/downLoadError.jsp</result><!-- 下载失败页面 -->
</action>
</package>
</struts>
5、属性文件upload.properties配置:
6、测试。。。。。。。。。。