DownloadAction.java
package dsh.bikegis.action.tool;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import org.apache.struts2.ServletActionContext;
import dsh.bikegis.system.SysAction;
/**
* 文件下載的action
* @author NanGuoCan
*
*/
public class DownloadAction extends SysAction {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String path="/kmldata/";
private String fileName;// 初始的通过param指定的文件名属性
public InputStream getInputStream() throws Exception {
String realPath=path+this.decodeFileName();
System.out.println(ServletActionContext.getServletContext().getResourceAsStream(realPath)+"這是或得的inputstream流");
// 通过 ServletContext,也就是application 来读取数据
return ServletActionContext.getServletContext().getResourceAsStream(realPath);
}
public String execute() {
String realPath = ServletActionContext.getServletContext()
.getRealPath(path + this.decodeFileName());
System.out.println(realPath+"這是第一個文件的路徑");
if (new File(realPath).exists()) {
return ActionSupport.SUCCESS;
}else{
this.errMesg="您要下載的文件不存在";
return ActionSupport.ERROR;
}
}
/**
* 對前臺傳來的fileName進行解碼
*/
public String decodeFileName(){
String tempFileName=null;
try {
tempFileName=URLDecoder.decode(fileName,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(tempFileName);
return tempFileName;
}
/*
* @getDownloadFileName
* 此方法对应的是struts.xml文件中的:
* <param name="contentDisposition">
* attachment;filename="${downloadFileName}"
* </param>
* 这个属性设置的是下载工具下载文件时显示的文件名,
* 要想正确的显示中文文件名,我们需要对fileName再次编码
* 否则中文名文件将出现乱码,或无法下载的情况
* */
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
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="downLoadAction" namespace="/load" extends="bikeGIS">
<action name="downLoad" class="downloadAction">
<result name="success" type="stream">
<!-- 动态文件下载的,事先并不知道未来的文件类型,那么我们可以把它的值设置成为:application/octet-stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错; -->
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<!-- 指定action类中的方法名(本例中对应于getInputStream方法) -->
<param name="inputName">inputStream</param>
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性 , 对应action类中的方法 getDownloadFileName() -->
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">4096</param>
</result>
<result name="error">/WEB-INF/error.jsp</result>
</package>
</struts>
前臺頁面引用方法:这个为写在jquery的ajax方法中的,可以使用js的encodeURI方法,
<a href="${requestScope.basePath}/load/downLoad.action?fileName='+encodeURI(encodeURI(cityMeg[i].roadkml))+'">'+cityMeg[i].kmlname+'</a>
如果直接写在html中的话可以采用下面的方法
<a href="#" onclick="loadLink('${around.medicial}');">下載</a>
然后在body里面写一个js方法
<script type="text/javascript">
function loadLink(around){
location.href='${requestScope.basePath}/load/downAroundAction.action?fileName='+encodeURI(encodeURI(around));
}
</script>
分享到:
相关推荐
使用struts2实现文件下载功能,可以直接运行!
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2中实现文件上传 struts2中实现CRUD struts2中的OGNL struts2的新表单标志的使用 struts2与AJAX一 struts2与AJAX二 struts2与AJAX三 struts2中用Spring实现IOC struts2中的零配置与IOC struts2介绍之使用连接...
网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载...
Struts 2中实现文件下载 文件下载
该文档总结了struts2框架技术实现下载功能的全部过程,是我在做开发时总结出来的,现在分享给大家。
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
struts实现的文件上传下载功能,最大可以上传到150MB
struts1和struts2分别实现文件上传下载功能
使用struts2框架进行文件的上传并限制文件的大小与类型,使用struts2框架实现文件下载
struts与hibernate实现文件的上传与动态下载 主要就是运用struts2.2和hibernate3.6进行文件上传与下载,并且能动态的下载,里面包括了详细的构建文档,以及代码
博文链接:https://ychw668.iteye.com/blog/122843
Struts2文件流方式导出下载excel、Txt、image图片,直接返回给浏览器提示下载,不生成临时文件,直接保存在客户端
MVC Struts2框架搭建,Jquery Ajax异步数据交互,内涵需要的jar包及ppt讲解
struts2文件下载,实现多文件上传下载,使用struts的file标签实现
Struts2实现文件上传的功能,简洁方便,欢迎下载。
用的时候自己在D:\apache\Tomcat 6.0\webapps\Filedownload下创建文件夹upload往文件夹里放一个1.xls
Struts2 code jar+实现struts2步骤文档
通过struts2框架实现Struts2文件上传下载以及显示。综合实现数据的利用。
Struts2的开发文档,学习Struts2时可以用来查阅API相关接口说明,便于分析框架原理