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="#" οnclick="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>