﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-thisliy-随笔分类-struts2</title><link>http://www.blogjava.net/thisliy/category/41289.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 15 Aug 2009 11:46:34 GMT</lastBuildDate><pubDate>Sat, 15 Aug 2009 11:46:34 GMT</pubDate><ttl>60</ttl><item><title>实现自己的拦截器框架</title><link>http://www.blogjava.net/thisliy/archive/2009/08/15/291258.html</link><dc:creator>liyang</dc:creator><author>liyang</author><pubDate>Sat, 15 Aug 2009 05:16:00 GMT</pubDate><guid>http://www.blogjava.net/thisliy/archive/2009/08/15/291258.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: AOP技术是spring框架的一个重要特征。通过该特性能够在函数运行之前，之后，或者异常处理的时候执行我们需要的一些操作。 下面我们就是需要抛开AOP，Spring这样成型的框架不用，而仅仅使用java反射机制中的Proxy,InvocationHandler来实现类似Spring框架的拦截器的效果。 动态代理DynamicProxy 首先，在设计这个拦截器框架之前，我们需...&nbsp;&nbsp;<a href='http://www.blogjava.net/thisliy/archive/2009/08/15/291258.html'>阅读全文</a><img src ="http://www.blogjava.net/thisliy/aggbug/291258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/thisliy/" target="_blank">liyang</a> 2009-08-15 13:16 <a href="http://www.blogjava.net/thisliy/archive/2009/08/15/291258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Struts2文件下载</title><link>http://www.blogjava.net/thisliy/archive/2009/08/14/291153.html</link><dc:creator>liyang</dc:creator><author>liyang</author><pubDate>Fri, 14 Aug 2009 06:31:00 GMT</pubDate><guid>http://www.blogjava.net/thisliy/archive/2009/08/14/291153.html</guid><description><![CDATA[<span style="font-family: 微软雅黑;">
<span style="font-size: 10pt;">
<div id="article_body">
<p>一个简单的利用struts2做文件下载的demo&#8230;&#8230;</p>
<p>首先配好struts：</p>
<p>web.xml</p>
<div>
<div style="border: 1px dotted #aaaaaa; margin: 8px; padding: 2px; overflow: auto; color: #555555; height: 300px;">
<ol>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span><span style="font-weight: bold; color: black;">?&gt;</span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;web-app</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"2.4"</span> </span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">"http://java.sun.com/xml/ns/j2ee"</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">"http://www.w3.org/2001/XMLSchema-instance"</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">"http://java.sun.com/xml/ns/j2ee </span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span><span style="font-weight: bold; color: black;">&gt;</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;welcome-file-list<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;welcome-file<span style="font-weight: bold; color: black;">&gt;</span></span></span>index.jsp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/welcome-file<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/welcome-file-list<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>struts2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.apache.struts2.dispatcher.FilterDispatcher<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-class<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>struts2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/web-app<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
</ol>
</div>
</div>
<p>struts.xml——这里是重点</p>
<div>
<div style="border: 1px dotted #aaaaaa; margin: 8px; padding: 2px; color: #555555;">
<ol>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #00bbdd;">&lt;!DOCTYPE struts PUBLIC</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #00bbdd;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #00bbdd;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"http://struts.apache.org/dtds/struts-2.0.dtd"&gt;</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;struts<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;package</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"default"</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">"struts-default"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"download"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"action.DownloadAction"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;result</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"stream"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"contentType"</span><span style="font-weight: bold; color: black;">&gt;</span></span>application/octet-stream<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"inputName"</span><span style="font-weight: bold; color: black;">&gt;</span></span>inputStream<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"contentDisposition"</span><span style="font-weight: bold; color: black;">&gt;</span></span>attachment;filename="${fileName}"<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"bufferSize"</span><span style="font-weight: bold; color: black;">&gt;</span></span>4096<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/result<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/action<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/package<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/struts<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
    </li>
</ol>
</div>
</div>
<p>当result为stream类型时，struts2会自动根据你配置好的参数下载文件。</p>
<p>其中主要使用的参数是：<br />
contentType 指定下载文件的文件类型 —— application/octet-stream 表示无限制<br />
inputName 流对象名 —— 比如这里写inputStream，它就会自动去找Action中的getInputStream方法。<br />
contentDisposition 使用经过转码的文件名作为下载文件名 —— 默认格式是attachment;filename="${fileName}"，将调用该Action中的getFileName方法。<br />
bufferSize 下载文件的缓冲大小</p>
<p>之后写个DownloadAction：</p>
<div>
<div style="border: 1px dotted #aaaaaa; margin: 8px; padding: 2px; overflow: auto; color: #555555; height: 300px;">
<ol>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="font-weight: bold; color: #000000;">package</span> action;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #a1a100;">import java.io.InputStream;</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #a1a100;">import org.apache.struts2.ServletActionContext;</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="font-weight: bold; color: #000000;">public</span> <span style="font-weight: bold; color: #000000;">class</span> DownloadAction <span style="color: #66cc66;">{</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="font-weight: bold; color: #aaaadd;">String</span></a> fileName;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #993333;">void</span> setFileName<span style="color: #66cc66;">(</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="font-weight: bold; color: #aaaadd;">String</span></a> fileName<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">this</span>.<span style="color: #006600;">fileName</span> = fileName;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="font-weight: bold; color: #aaaadd;">InputStream</span></a> getInputStream<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">return</span> ServletActionContext.<span style="color: #006600;">getServletContext</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>.<span style="color: #006600;">getResourceAsStream</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"/"</span> + fileName<span style="color: #66cc66;">)</span>;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky"><span style="font-weight: bold; color: #aaaadd;">String</span></a> execute<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-weight: bold; color: #000000;">return</span> <span style="color: #ff0000;">"success"</span>;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span></div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;">&nbsp;</div>
    </li>
    <li>
    <div style="border-left: 1px dotted #aaaaaa; padding-left: 12px; color: #000000;"><span style="color: #66cc66;">}</span></div>
    </li>
</ol>
</div>
</div>
<p>* 注意使用getResourceAsStream方法时，文件路径必须是以&#8220;/&#8221;开头，且是相对路径。这个路径是相对于项目根目录的。<br />
* 可以用return new FileInputStream(fileName)的方法来得到绝对路径的文件。</p>
<p>在WEB-INF下随意丢一个test.txt，部署好后进入浏览器，输入tomcat地址/项目路径/download.action?fileName=test.txt即可下载到该文件。</p>
<p><strong>附:contentType类型.</strong><br />
'ez' =&gt; 'application/andrew-inset', <br />
'hqx' =&gt; 'application/mac-binhex40', <br />
'cpt' =&gt; 'application/mac-compactpro', <br />
'doc' =&gt; 'application/msword', <br />
'bin' =&gt; 'application/octet-stream', <br />
'dms' =&gt; 'application/octet-stream', <br />
'lha' =&gt; 'application/octet-stream', <br />
'lzh' =&gt; 'application/octet-stream', <br />
'exe' =&gt; 'application/octet-stream', <br />
'class' =&gt; 'application/octet-stream', <br />
'so' =&gt; 'application/octet-stream', <br />
'dll' =&gt; 'application/octet-stream', <br />
'oda' =&gt; 'application/oda', <br />
'pdf' =&gt; 'application/pdf', <br />
'ai' =&gt; 'application/postscript', <br />
'eps' =&gt; 'application/postscript', <br />
'ps' =&gt; 'application/postscript', <br />
'smi' =&gt; 'application/smil', <br />
'smil' =&gt; 'application/smil', <br />
'mif' =&gt; 'application/vnd.mif', <br />
'xls' =&gt; 'application/vnd.ms-excel', <br />
'ppt' =&gt; 'application/vnd.ms-powerpoint', <br />
'wbxml' =&gt; 'application/vnd.wap.wbxml', <br />
'wmlc' =&gt; 'application/vnd.wap.wmlc', <br />
'wmlsc' =&gt; 'application/vnd.wap.wmlscriptc', <br />
'bcpio' =&gt; 'application/x-bcpio', <br />
'vcd' =&gt; 'application/x-cdlink', <br />
'pgn' =&gt; 'application/x-chess-pgn', <br />
'cpio' =&gt; 'application/x-cpio', <br />
'csh' =&gt; 'application/x-csh', <br />
'dcr' =&gt; 'application/x-director', <br />
'dir' =&gt; 'application/x-director', <br />
'dxr' =&gt; 'application/x-director', <br />
'dvi' =&gt; 'application/x-dvi', <br />
'spl' =&gt; 'application/x-futuresplash', <br />
'gtar' =&gt; 'application/x-gtar', <br />
'hdf' =&gt; 'application/x-hdf', <br />
'js' =&gt; 'application/x-javas</p>
<p style="text-indent: 2em;"><wbr>cript', <br />
'skp' =&gt; 'application/x-koan', <br />
'skd' =&gt; 'application/x-koan', <br />
'skt' =&gt; 'application/x-koan', <br />
'skm' =&gt; 'application/x-koan', <br />
'latex' =&gt; 'application/x-latex', <br />
'nc' =&gt; 'application/x-netcdf', <br />
'cdf' =&gt; 'application/x-netcdf', <br />
'sh' =&gt; 'application/x-sh', <br />
'shar' =&gt; 'application/x-shar', <br />
'swf' =&gt; 'application/x-shockwave-flash', <br />
'sit' =&gt; 'application/x-stuffit', <br />
'sv4cpio' =&gt; 'application/x-sv4cpio', <br />
'sv4crc' =&gt; 'application/x-sv4crc', <br />
'tar' =&gt; 'application/x-tar', <br />
'tcl' =&gt; 'application/x-tcl', <br />
'tex' =&gt; 'application/x-tex', <br />
'texinfo' =&gt; 'application/x-texinfo', <br />
'texi' =&gt; 'application/x-texinfo', <br />
't' =&gt; 'application/x-troff', <br />
'tr' =&gt; 'application/x-troff', <br />
'roff' =&gt; 'application/x-troff', <br />
'man' =&gt; 'application/x-troff-man', <br />
'me' =&gt; 'application/x-troff-me', <br />
'ms' =&gt; 'application/x-troff-ms', <br />
'ustar' =&gt; 'application/x-ustar', <br />
'src' =&gt; 'application/x-wais-source', <br />
'xhtml' =&gt; 'application/xhtml+xml', <br />
'xht' =&gt; 'application/xhtml+xml', <br />
'zip' =&gt; 'application/zip', <br />
'au' =&gt; 'audio/basic', <br />
'snd' =&gt; 'audio/basic', <br />
'mid' =&gt; 'audio/midi', <br />
'midi' =&gt; 'audio/midi', <br />
'kar' =&gt; 'audio/midi', <br />
'mpga' =&gt; 'audio/mpeg', <br />
'mp2' =&gt; 'audio/mpeg', <br />
'mp3' =&gt; 'audio/mpeg', <br />
'aif' =&gt; 'audio/x-aiff', <br />
'aiff' =&gt; 'audio/x-aiff', <br />
'aifc' =&gt; 'audio/x-aiff', <br />
'm3u' =&gt; 'audio/x-mpegurl', <br />
'ram' =&gt; 'audio/x-pn-realaudio', <br />
'rm' =&gt; 'audio/x-pn-realaudio', <br />
'rpm' =&gt; 'audio/x-pn-realaudio-plugin', <br />
'ra' =&gt; 'audio/x-realaudio', <br />
'wav' =&gt; 'audio/x-wav', <br />
'pdb' =&gt; 'chemical/x-pdb', <br />
'xyz' =&gt; 'chemical/x-xyz', <br />
'bmp' =&gt; 'image/bmp', <br />
'gif' =&gt; 'image/gif', <br />
'ief' =&gt; 'image/ief', <br />
'jpeg' =&gt; 'image/jpeg', <br />
'jpg' =&gt; 'image/jpeg', <br />
'jpe' =&gt; 'image/jpeg', <br />
'png' =&gt; 'image/png', <br />
'tiff' =&gt; 'image/tiff', <br />
'tif' =&gt; 'image/tiff', <br />
'djvu' =&gt; 'image/vnd.djvu', <br />
'djv' =&gt; 'image/vnd.djvu', <br />
'wbmp' =&gt; 'image/vnd.wap.wbmp', <br />
'ras' =&gt; 'image/x-cmu-raster', <br />
'pnm' =&gt; 'image/x-portable-anymap', <br />
'pbm' =&gt; 'image/x-portable-bitmap', <br />
'pgm' =&gt; 'image/x-portable-graymap', <br />
'ppm' =&gt; 'image/x-portable-pixmap', <br />
'rgb' =&gt; 'image/x-rgb', <br />
'xbm' =&gt; 'image/x-xbitmap', <br />
'xpm' =&gt; 'image/x-xpixmap', <br />
'xwd' =&gt; 'image/x-xwindowdump', <br />
'igs' =&gt; 'model/iges', <br />
'iges' =&gt; 'model/iges', <br />
'msh' =&gt; 'model/mesh', <br />
'mesh' =&gt; 'model/mesh', <br />
'silo' =&gt; 'model/mesh', <br />
'wrl' =&gt; 'model/vrml', <br />
'vrml' =&gt; 'model/vrml', <br />
'css' =&gt; 'text/css', <br />
'html' =&gt; 'text/html', <br />
'htm' =&gt; 'text/html', <br />
'asc' =&gt; 'text/plain', <br />
'txt' =&gt; 'text/plain', <br />
'rtx' =&gt; 'text/richtext', <br />
'rtf' =&gt; 'text/rtf', <br />
'sgml' =&gt; 'text/sgml', <br />
'sgm' =&gt; 'text/sgml', <br />
'tsv' =&gt; 'text/tab-separated-values', <br />
'wml' =&gt; 'text/vnd.wap.wml', <br />
'wmls' =&gt; 'text/vnd.wap.wmlscript', <br />
'etx' =&gt; 'text/x-setext', <br />
'xsl' =&gt; 'text/xml', <br />
'xml' =&gt; 'text/xml', <br />
'mpeg' =&gt; 'video/mpeg', <br />
'mpg' =&gt; 'video/mpeg', <br />
'mpe' =&gt; 'video/mpeg', <br />
'qt' =&gt; 'video/quicktime', <br />
'mov' =&gt; 'video/quicktime', <br />
'mxu' =&gt; 'video/vnd.mpegurl', <br />
'avi' =&gt; 'video/x-msvideo', <br />
'movie' =&gt; 'video/x-sgi-movie', <br />
'ice' =&gt; 'x-conference/x-cooltalk'</p>
</div>
</span>
</span>
<img src ="http://www.blogjava.net/thisliy/aggbug/291153.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/thisliy/" target="_blank">liyang</a> 2009-08-14 14:31 <a href="http://www.blogjava.net/thisliy/archive/2009/08/14/291153.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>