Action类:
@SuppressWarnings("serial")
public class DownFileAction extends ActionSupport {
// 依赖注入的属性,struts.xml中动态的指定
private String inputPath;
// 只需要set方法
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
/**
* 配置stream类型结果时,需指定inputName;inputName为方法名去掉get前缀,并且首字母小写的字符串;
* 比如此例中的inputName为 targetFile
*
* @return
*/
public InputStream getTargetFile() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath); //该方法适用于相对路径,相对项目根目录webroot
//String realPath = inputPath;
//File file = new File(realPath);
//return new FileInputStream(file); //该方法适用于获取本地绝对路径的文件,inputPath为绝对路径,格式类似 D:\\iosh\\log\\run.log
}
}
Struts配置文件:
<action name="openCJoinFile" class="com.haiyi.action.DownFileAction">
<!-- 指定被下载资源的位置 -->
<param name="inputPath">CJoin-记事本.txt</param>
<!-- 配置结果类型为stream的结果 -->
<result name="success" type="stream">
<!-- 指定下载文件的文件类型 -->
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
<param name="inputName">targetFile</param>
<!-- 文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,默认为直接显示文件 -->
<param name="contentDisposition">attachment;filename="CJoin.txt"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
JSP:
<a href="<%=basePath%>openCJoinFile.action" target="_self">主日志</a>
ContentType文件类型:
序号 | 内容类型 | 文件扩展名 | 描述 |
1 | application/msword | doc | Microsoft Word |
2 | application/octet-stream bin | dms lha lzh exe class | 可执行程序 |
3 | application/pdf | | Adobe Acrobat |
4 | application/postscript | ai eps ps | PostScript |
5 | appication/powerpoint | ppt | Microsoft Powerpoint |
6 | appication/rtf | rtf | rtf 格式 |
7 | appication/x-compress | z | unix 压缩文件 |
8 | application/x-gzip | gz | gzip |
9 | application/x-gtar | gtar | tar 文档 (gnu 格式 ) |
10 | application/x-shockwave-flash | swf | MacroMedia Flash |
11 | application/x-tar | tar | tar(4.3BSD) |
12 | application/zip | zip | winzip |
13 | audio/basic | au snd | sun/next 声音文件 |
14 | audio/mpeg | mpeg mp2 | Mpeg 声音文件 |
15 | audio/x-aiff | mid midi rmf | Midi 格式 |
16 | audio/x-pn-realaudio | ram ra | Real Audio 声音 |
17 | audio/x-pn-realaudio-plugin | rpm | Real Audio 插件 |
18 | audio/x-wav | wav | Microsoft Windows 声音 |
19 | image/cgm | cgm | 计算机图形元文件 |
20 | image/gif | gif | COMPUSERVE GIF 图像 |
21 | image/jpeg | jpeg jpg jpe | JPEG 图像 |
22 | image/png | png | PNG 图像 |
text/html HTML
text/plain TXT
text/xml XML
text/json json字符串
如果路径要从例如 log4j.properties 文件路获取,需要对上面代码做一点修改:
public InputStream getSysLog() throws Exception {
InputStream ins = DownFileAction.class.getResourceAsStream("../../../log4j.properties"); //获取log4j.properties文件
Properties p = new Properties();
try{
p.load(ins); //加载
}catch(Exception e){
e.printStackTrace();
}
String realPath = p.getProperty("log4j.appender.sysLog.File"); //通过key获取value值
System.out.println(realPath);
File file = new File(realPath);
return new FileInputStream(file);
}
同时,把 struts.xml 中的 inputPath 那行删掉,无需配置路径