实现文件下载的时候 ,也是参考了很多人写的博客,了解了下载的过程之后,结合各位博主的文章,也算是实现了这一功能,如若不明白的尽请留言询问,如若文章有雷同,也请见谅(ps:这个功能的实现是在很久之前,参考的文章也已忘记)
jsp
给下载按钮一个url:${ pageContext.request.contextPath }/document_download.action?uploadFileName="+value+"
根据需要,给uploadFileName传值
action 配置
/***
* 实现文件下载
*/
//定义输入流,名称和xml里面的<param name="inputName">的名称一样
private InputStream inputStream;
private String inputPath;//下载文件目录
生成get、set方法//文件下载
public String download () throws FileNotFoundException, Exception{
//转码,解决中文名字问题
uploadFileName = new String(uploadFileName.getBytes("ISO-8859-1"), "UTF8");
String path=ServletActionContext.getServletContext().getRealPath(uploadFileName);
//根据文件夹路径和文件名,创建file文件
File file = new File(path);
//用输入流读取文件
inputStream = new FileInputStream(file);
//return new BufferedInputStream(new FileInputStream(path+"\\"+FileName));
uploadFileName=new String(uploadFileName.getBytes (),"iso8859-1");
return "download"; //返回的是xml的对应的action的名称
}
struts 配置
<!-- 文件下载 -->
<result name="download" type="stream"> <!--定义返回值为流-->
<param name="inputName">inputStream</param> <!--方法内部使用的流的名称-->
<!--文件下载的处理方式-->
<param name="contentDisposition">attachment;filename="${uploadFileName}"</param>
<param name="bufferSize">204800</param> <!--下载缓冲区的大小-->
</result>