`
xyzroundo
  • 浏览: 508107 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

struts2多文件动态下载及中文解决方案

 
阅读更多
<p>来源:<a href="http://jianglubin1.blog.163.com/blog/static/13177713420101245426740/">http://jianglubin1.blog.163.com/blog/static/13177713420101245426740/</a></p>
<p> </p>
<p>关于文件上传的例子,网上已经有很多,所以本文就不谈文件上传,重点是文件下载及文件名为中文的时候各个浏览器出现乱码的解决方案。
</p>
<p> </p>
<p>下面是jsp文件的代码:</p>
<p> </p>
相应的DownloadAction.java的代码:<br><ol>
<li>
<span>&lt;</span><span>html</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;</span><span>head</span><span>&gt;</span>
</li>
<li>
<span>   </span><span>&lt;</span><span>title</span><span>&gt;</span><span>download</span><span>&lt;/</span><span>title</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;/</span><span>head</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;</span><span>body</span><span>&gt;</span>
</li>
<li>
<span>   </span><span>&lt;</span><span>%</span>
</li>
<li>
<span>    //取得服务器</span><span>"</span><span>/download/file</span><span>"</span><span>目录的物理路径</span>
</li>
<li>
<span>    </span><span>String</span><span> </span><span>path</span><span> = </span><span>request</span><span>.</span><span>getRealPath</span><span>(</span><span>"</span><span>/download/file</span><span>"</span><span>);</span>
</li>
<li>
<span>    //取得</span><span>"</span><span>/download/file</span><span>"</span><span>目录的</span><span>file</span><span>对象</span>
</li>
<li>
<span>    </span><span>File</span><span> </span><span>file</span><span> = </span><span>new</span><span> </span><span>File</span><span>(</span><span>path</span><span>);</span>
</li>
<li>
<span>    //取得</span><span>file</span><span>目录下所有文件</span>
</li>
<li>
<span>    </span><span>File</span><span>[] </span><span>files</span><span> = </span><span>file</span><span>.</span><span>listFiles</span><span>();</span>
</li>
<li>
</li>
<li>
<span>   </span><span>for</span><span> (</span><span>int</span><span> </span><span>i</span><span> = </span><span>0</span><span>; </span><span>i</span><span> &lt; </span><span>files</span><span>.</span><span>length</span><span>; </span><span>i</span><span>++) {</span>
</li>
<li>
</li>
<li>
<span>    </span><span>String</span><span> </span><span>fname</span><span> = </span><span>files</span><span>[</span><span>i</span><span>].</span><span>getName</span><span>();</span>
</li>
<li>
</li>
<li>
<span>    //对文件名进行</span><span>url</span><span>编码(</span><span>UTF-8</span><span>指明</span><span>fname</span><span>原来的编码,</span><span>UTF-8</span><span>一般由本地编码</span><span>GBK</span><span>代替)</span>
</li>
<li>
<span>     </span><span>fname</span><span> = </span><span>java</span><span>.</span><span>net</span><span>.</span><span>URLEncoder</span><span>.</span><span>encode</span><span>(</span><span>fname</span><span>, </span><span>"</span><span>UTF-8</span><span>"</span><span>);</span>
</li>
<li>
</li>
<li>
<span>    </span><span>out</span><span>.</span><span>println</span><span>(</span><span>"</span><span>&lt;a href=download.action?name=</span><span>"</span><span> + </span><span>fname</span><span> + </span><span>"</span><span>&gt;</span><span>"</span>
</li>
<li>
<span>     + </span><span>files</span><span>[</span><span>i</span><span>].</span><span>getName</span><span>() + </span><span>"</span><span>&lt;/a&gt;&lt;br&gt;</span><span>"</span><span>);</span>
</li>
<li><span>    }</span></li>
<li>
<span>   %</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;/</span><span>body</span><span>&gt;</span>
</li>
<li>
<span> </span><span>&lt;/</span><span>html</span><span>&gt;</span>
</li>
</ol> <span>相应的<span lang="EN-US">DownloadAction.java</span>的代码:<span lang="EN-US"><br><br></span></span>
<p> </p>
<ol>
<li>
<span>package</span><span> </span><span>com</span><span>.</span><span>test</span><span>.</span><span>action</span><span>;</span>
</li>
<li>
</li>
<li>
<span>import</span><span> </span><span>java</span><span>.</span><span>io</span><span>.</span><span>InputStream</span><span>;</span>
</li>
<li>
<span> </span><span>import</span><span> </span><span>java</span><span>.</span><span>io</span><span>.</span><span>UnsupportedEncodingException</span><span>;</span>
</li>
<li>
<span> </span><span>import</span><span> </span><span>com</span><span>.</span><span>opensymphony</span><span>.</span><span>xwork2</span><span>.</span><span>ActionSupport</span><span>;</span>
</li>
<li>
<span> </span><span>import</span><span> </span><span>org</span><span>.</span><span>apache</span><span>.</span><span>struts2</span><span>.</span><span>ServletActionContext</span><span>;</span>
</li>
<li>
</li>
<li>
<span>public</span><span> </span><span>class</span><span> </span><span>DownloadAction</span><span> </span><span>extends</span><span> </span><span>ActionSupport</span><span> </span><span>{</span>
</li>
<li>
<span>     </span><span>private</span><span> </span><span>static</span><span> </span><span>final</span><span> </span><span>long</span><span> </span><span>serialVersionUID</span><span> = </span><span>6329383258366253255</span><span>L</span><span>;</span>
</li>
<li><span> </span></li>
<li>
<span>     </span><span>private</span><span> </span><span>String</span><span> </span><span>fileName</span><span>;</span>
</li>
<li><span> </span></li>
<li>
<span>     </span><span>public</span><span> </span><span>void</span><span> </span><span>setFileName</span><span>(){</span>
</li>
<li>
<span>         </span><span>//得到请求下载的文件名</span>
</li>
<li>
<span>         </span><span>String</span><span> </span><span>fname</span><span>=</span><span>ServletActionContext</span><span>.</span><span>getRequest</span><span>()</span><span>.</span><span>getParameter</span><span>(</span><span>"</span><span>name</span><span>"</span><span>)</span><span>; </span>
</li>
<li>
<span>         </span><span>try</span><span> </span><span>{</span>
</li>
<li>
<span>         </span><span>/*</span>
</li>
<li><span>          * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。</span></li>
<li><span>          * 这里使用request.setCharacterEncoding解码无效.</span></li>
<li><span>          * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件</span></li>
<li>
<span>          * */</span><span>  </span>
</li>
<li>
<span>               </span><span>fname</span><span> = </span><span>new</span><span> </span><span>String</span><span>(</span><span>fname</span><span>.</span><span>getBytes</span><span>(</span><span>"</span><span>ISO-8859-1</span><span>"</span><span>)</span><span>, </span><span>"</span><span>UTF-8</span><span>"</span><span>)</span><span>;</span>
</li>
<li>
</li>
<li>
<span>       </span><span>}</span><span> </span><span>catch</span><span> </span><span>(</span><span>Exception</span><span> </span><span>e</span><span>)</span><span> </span><span>{</span>
</li>
<li>
<span>             </span><span>e</span><span>.</span><span>printStackTrace</span><span>()</span><span>;</span>
</li>
<li>
<span>        </span><span>}</span><span>  </span>
</li>
<li>
<span>        </span><span>this</span><span>.</span><span>fileName</span><span>=</span><span>fname</span><span>;  </span>
</li>
<li>
<span>        </span><span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>fileName</span><span>)</span><span>; </span>
</li>
<li>
<span>    </span><span>}</span>
</li>
<li><span> </span></li>
<li>
<span>       </span><span>/*</span>
</li>
<li>
<span>        *</span><span> @getFileName</span>
</li>
<li><span>        * 此方法对应的是struts.xml文件中的:</span></li>
<li><span>        * &lt;param &gt;attachment;filename="${fileName}"&lt;/param&gt;</span></li>
<li><span>        * 这个属性设置的是下载工具下载文件时显示的文件名,</span></li>
<li><span>        * 要想正确的显示中文文件名,我们需要对fileName再次编码</span></li>
<li><span>        * 否则中文名文件将出现乱码,或无法下载的情况</span></li>
<li><span>        * */</span></li>
<li>
<span>      </span><span>public</span><span> </span><span>String</span><span> </span><span>getFileName</span><span>()</span><span> </span><span>throws</span><span> </span><span>UnsupportedEncodingException</span><span> </span><span>{</span>
</li>
<li><span>  </span></li>
<li>
<span>          </span><span>fileName</span><span>=</span><span>new</span><span> </span><span>String</span><span>(</span><span>fileName</span><span>.</span><span>getBytes</span><span>()</span><span>,</span><span>"</span><span>ISO-8859-1</span><span>"</span><span>)</span><span>;</span>
</li>
<li><span>  </span></li>
<li>
<span>          </span><span>return</span><span> </span><span>fileName</span><span>;</span>
</li>
<li>
<span>      </span><span>}</span>
</li>
<li>
</li>
<li>
<span>     </span><span>/*</span>
</li>
<li>
<span>       *</span><span> @getDownloadFile</span>
</li>
<li><span>       * 此方法对应的是struts.xml文件中的:</span></li>
<li><span>       * &lt;param &gt;downloadFile&lt;/param&gt;</span></li>
<li><span>       * 返回下载文件的流,可以参看struts2的源码</span></li>
<li><span>       * */</span></li>
<li>
<span>      </span><span>public</span><span> </span><span>InputStream</span><span> </span><span>getDownloadFile</span><span>()</span><span> </span><span>{</span>
</li>
<li><span>  </span></li>
<li>
<span>         </span><span>this</span><span>.</span><span>setFileName</span><span>()</span><span>;</span>
</li>
<li>
</li>
<li>
<span>         </span><span>return</span><span> </span><span>ServletActionContext</span><span>.</span><span>getServletContext</span><span>()</span><span>.</span><span>getResourceAsStream</span><span>(</span><span>"</span><span>/download/file/</span><span>"</span><span> + </span><span>fileName</span><span>)</span><span>;</span>
</li>
<li>
<span>      </span><span>}</span>
</li>
<li>
</li>
<li>
<span>     @</span><span>Override</span>
</li>
<li>
<span>      </span><span>public</span><span> </span><span>String</span><span> </span><span>execute</span><span>()</span><span> </span><span>throws</span><span> </span><span>Exception</span><span> </span><span>{</span>
</li>
<li>
<span>          </span><span>return</span><span> </span><span>SUCCESS</span><span>;</span>
</li>
<li>
<span>     </span><span>}</span>
</li>
<li>
<span> </span><span>}</span>
</li>
</ol>
<p>struts.xml相应的Action配置:</p>
<ol>
<li>
<span>&lt;</span><span>action</span><span> </span><span>name</span><span>=</span><span>"</span><span>download</span><span>"</span><span> </span><span>class</span><span>=</span><span>"</span><span>com.test.action.DownloadAction</span><span>"</span><span>&gt;</span>
</li>
<li>
<span>      </span><span>&lt;</span><span>result</span><span> </span><span>name</span><span>=</span><span>"</span><span>success</span><span>"</span><span> </span><span>type</span><span>=</span><span>"</span><span>stream</span><span>"</span><span>&gt;</span>
</li>
<li>
<span>          </span><span>&lt;</span><span>param</span><span> </span><span>name</span><span>=</span><span>"</span><span>contentDisposition</span><span>"</span><span>&gt;</span><span>attachment;filename="${fileName}"</span><span>&lt;/</span><span>param</span><span>&gt;</span><span> </span>
</li>
<li>
<span>          </span><span>&lt;</span><span>param</span><span> </span><span>name</span><span>=</span><span>"</span><span>inputName</span><span>"</span><span>&gt;</span><span>downloadFile</span><span>&lt;/</span><span>param</span><span>&gt;</span>
</li>
<li>
<span>      </span><span>&lt;/</span><span>result</span><span>&gt;</span>
</li>
<li>
<span>   </span><span>&lt;/</span><span>action</span><span>&gt;</span>
</li>
</ol>
<p>web.xml:</p>
<ol>
<li>
<span>&lt;</span><span>filter</span><span>&gt;</span>
</li>
<li>
<span>    </span><span>&lt;</span><span>filter-name</span><span>&gt;</span><span>struts2</span><span>&lt;/</span><span>filter-name</span><span>&gt;</span>
</li>
<li>
<span>    </span><span>&lt;</span><span>filter-class</span><span>&gt;</span><span>org.apache.struts2.dispatcher.FilterDispatcher</span><span>&lt;/</span><span>filter-class</span><span>&gt;</span>
</li>
<li>
<span> </span><span>&lt;/</span><span>filter</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;</span><span>filter-mapping</span><span>&gt;</span>
</li>
<li>
<span>    </span><span>&lt;</span><span>filter-name</span><span>&gt;</span><span>struts2</span><span>&lt;/</span><span>filter-name</span><span>&gt;</span>
</li>
<li>
<span>    </span><span>&lt;</span><span>url-pattern</span><span>&gt;</span><span>/*</span><span>&lt;/</span><span>url-pattern</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;/</span><span>filter-mapping</span><span>&gt;</span>
</li>
<li><span> </span></li>
<li>
<span>  </span><span>&lt;</span><span>welcome-file-list</span><span>&gt;</span>
</li>
<li>
<span>    </span><span>&lt;</span><span>welcome-file</span><span>&gt;</span><span>download.jsp</span><span>&lt;/</span><span>welcome-file</span><span>&gt;</span>
</li>
<li>
<span>  </span><span>&lt;/</span><span>welcome-file-list</span><span>&gt;</span>
</li>
</ol>
分享到:
评论
1 楼 wangwenfei1985 2014-02-26  
   

相关推荐

    struts2实现文件下载及中文名字转换解决方案

    在本文中,我们将深入探讨如何使用Struts2框架实现文件下载功能,尤其是处理包含中文名字的附件。Struts2是一个流行的Java Web开发框架,它提供了丰富的功能,包括处理HTTP请求、表单提交以及数据绑定等。对于文件...

    struts2中文学习文档

    从给定的文件信息来看,标题“struts2中文学习文档”和描述“struts2的根本webwork2”表明这是一份关于Struts2框架的学习资料,特别强调了Struts2与WebWork2的关系。Struts2是Apache Struts的一个版本,它是一个用于...

    struts2文件下载(解决了中文乱码问题)

    在处理文件下载时,Struts2提供了一套完整的解决方案,包括处理中文文件名的乱码问题。在标题提到的场景中,开发者可能遇到了在使用Myeclipse 6.5集成开发环境,JDK1.6和Tomcat 6.0服务器环境下,通过Struts2进行...

    struts2 中文文件名文件下载

    在“struts2 中文文件名文件下载”的场景中,我们需要关注的是如何在Struts2框架下正确处理包含中文字符的文件名。首先,我们要理解文件名编码ISO8859-1,这是一个西欧字符编码标准,不支持中文字符。在处理中文...

    Struts2中文乱码问题最终解决方案

    一种可行的解决方案是在Tomcat服务器的配置文件`server.xml`中,修改`&lt;Connector&gt;`标签,添加`URIEncoding="UTF-8"`属性。这将强制服务器解析URL时使用UTF-8编码,从而解决GET请求的中文乱码问题。 4. **Tomcat配置...

    Struts中文文档

    Struts是Apache软件基金会旗下Jakarta项目的一个核心框架,它是一个用于构建企业级Java Web应用程序的开放源代码解决方案。这个“Struts中文文档”包含了关于Struts框架的详细信息,旨在帮助开发者理解和掌握该框架...

    struts2上传下载 解决中文文件名乱码

    这个问题在早期的互联网资源中被广泛讨论,但并非所有解决方案都能适用于所有情况。下面我们将深入探讨这个问题,并提供一种经过验证的有效方法。 首先,我们需要理解乱码问题的根源。在Java中,默认的字符编码通常...

    Struts实现文件下载中文乱码解决方案

    以下是对这个问题的详细分析及解决方案: 首先,问题出现在文件列表的展示和下载链接的生成过程中。在Action中,`request.getRealPath()` 获取了服务器上文件的物理路径,然后遍历目录,将文件名添加到ArrayList中...

    Struts2资源文件在jsp页面中显示乱码解决

    通过上述两种解决方案,我们可以有效解决Struts2资源文件在JSP页面中显示乱码的问题。虽然第一种方法可以达到目的,但其带来的维护不便使其不被推荐;相比之下,安装PropertiesEditor插件不仅操作简便,还能彻底解决...

    struts2配置文件传值中文乱码

    本文将围绕“Struts2配置文件传值中文乱码”这一主题,深入探讨其成因、解决方案以及相关的配置细节,旨在帮助开发者有效解决中文乱码问题,提升用户体验。 ### Struts2框架简介 Struts2是Apache组织下的一个开源...

    struts2中文帮助文档

    这份文档不仅覆盖了基础概念,还深入到了实际开发中可能遇到的问题和解决方案,对于学习和提升Struts2开发技能非常有帮助。无论是初学者还是经验丰富的开发者,都应该充分利用这份资源,以提升项目开发的效率和质量...

    struts2实现文件下载

    对于中文文件名的下载,Struts2提供了灵活的解决方案。我们可以通过在`contentDisposition`中使用`${downloadFileName}`变量,并在对应的Action类中定义`getDownloadFileName()`方法来返回经过编码的文件名。 例如...

    struts中文问题,struts国际化问题——终极解决方案

    ### Struts中文问题与国际化问题——终极解决方案深度解析 #### 引言 在Web应用开发中,特别是使用Struts框架时,中文显示和国际化(Internationalization,简称i18n)问题是开发者常遇的挑战。本文将深入探讨解决...

    Struts中文乱码解决方案

    三、请求参数中的中文乱码问题解决方案: 对于用户提交的含有汉字的请求数据导致的乱码,可以通过自定义 RequestProcessor 来解决。首先,创建一个新的类 `SelfRequestProcessor` 继承自 Struts 内置的 `...

    java Struts2中文学习文档

    Struts2是Java Web开发中的一个强大框架,它在MVC(Model-View-Controller)设计模式的基础上,为开发者提供了一种高效、灵活且可扩展的解决方案。本篇文档将深入探讨Struts2的核心概念、架构以及实际应用,旨在帮助...

    struts2中文教程

    总之,Struts2提供了一套完整的解决方案来构建Java Web应用,其丰富的标签库简化了页面的开发,而强大的拦截器机制使得业务逻辑和控制流程的管理更为灵活。通过深入理解和实践,开发者可以有效地提高开发效率,构建...

    Struts2乱码终极解决办法

    本文将详细介绍如何彻底解决Struts2中的乱码问题,并提供实际可行的解决方案。 #### 二、乱码产生的原因 在Struts2框架中,中文乱码主要由以下几个方面的原因造成: 1. **请求编码问题**:客户端发送请求时未指定...

    struts 2. 5.2解决中文乱码

    在`myStruts`这个压缩包中,可能包含了示例代码、配置文件或测试用例,这些内容可以作为实践上述解决方案的参考。如果你在应用上述步骤后仍然遇到问题,可能需要检查`myStruts`中的具体文件以找出问题所在。例如,...

    struts2实战.pdf 中文完整版

    SSH(Spring、Struts2、Hibernate)是Java开发中的经典组合,它结合了Spring的依赖注入、事务管理,Hibernate的对象关系映射,以及Struts2的MVC框架,提供了一站式的解决方案。这种整合使得开发更加高效,降低了代码...

Global site tag (gtag.js) - Google Analytics