struts2 实现中文文件名的文件下载
2012-11-12 03:03
537 查看
原理
在struts.xml配置文件中,加入以下代码修改struts默认编码<constant name="struts.i18n.encoding" value="UTF-8"></constant>action中通过下面代码把传递过来的文件名转码成中文的文件名
String chiniseFileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");如果需要使下载到的文件名不是乱码,需要做如下设置:action的配置中加入
<param name="contentType">application/x-;charSet=ISO8859-1</param> <param name="contentDisposition">attachment;filename=${fileName}</param>filename=${fileName} 直接引用action中用System.out输入显示乱码的fileName, 给出的参数charSet="ISO8859-1"会使系统自动把乱码的fileName从ISO8859-1编码转换到系统所用编码。
请确保你发布的webapp的路径下有要下载的文件。
具体代码如下:
jsp页面代码:<%@page import="java.net.URLEncoder"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="download?fileName=中文名.rar">中文名.rar</a> <br> <a href="download?fileName=abc.rar">abc.rar</a> </body> </html>action代码:
package com.rhcloud.meteora.action; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport { /** * */ private static final long serialVersionUID = 6192288724388613030L; private String fileName; private FileInputStream targetFile; @Override public void validate() { // TODO Auto-generated method stub super.validate(); } public String getFileName() throws UnsupportedEncodingException { return fileName; } public void setFileName(String fileName) throws UnsupportedEncodingException { this.fileName = fileName; } public InputStream getTargetFile() throws UnsupportedEncodingException { System.out.println("aaaaaaaaaaaaaaaaaaaaa"); String zhFileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); System.out.println(zhFileName); String aString = ServletActionContext.getServletContext().getRealPath("") + System.getProperty("file.separator") + zhFileName; System.out.println(aString); File file = new File(aString); System.out.println(file + " " + file.isFile()); try { targetFile = new FileInputStream(file); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } System.out.println(targetFile); return targetFile; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }struts的配置文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.i18n.encoding" value="UTF-8"></constant><package name="default" namespace="/" extends="struts-default"><action name="download" class="com.rhcloud.meteora.action.DownloadAction"><result type="stream"><param name="inputName">targetFile</param><param name="bufferSize">4096</param><param name="contentType">application/x-;charSet=ISO8859-1</param> <param name="contentDisposition">attachment;filename=${fileName}</param></result></action></package></struts>源代码下载地址:
http://pan.baidu.com/share/link?shareid=132417&uk=3307773300
相关文章推荐
- Struts2实现文件下载功能代码分享(文件名中文转码)
- struts2文件下载功能实现(文件名中文转码)
- struts2使用action方式实现文件下载(支持中文文件名)
- struts2使用action方式实现文件下载(支持中文文件名)
- struts2使用action方式实现文件下载(支持中文文件名)
- struts2 Annotation 实现文件下载功能 文件名中文乱码问题
- struts2 Annotation 实现文件下载功能 文件名中文乱码问题
- struts2 实现文件上传下载 (下载支持中文文件名)代码
- struts2 实现文件上传下载 (下载支持中文文件名)代码
- Struts2 通过&quot;流&quot;下载文件时显示中文文件名
- Struts2文件下载时,中文文件名和chorm不兼容问题
- Ruby on Rails实现文件的下载以及中文文件名乱码问题
- struts2下载文件时文件名如果包含中文出现错误
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- struts2 文件下载文件名中文乱码及变成空格问题
- Java实现文件下载并解决中文文件名乱码
- 使用struts2实现下载功能遇到的文件中文问题
- jsp实现文件下载与中文文件名乱码问题解决
- FileUpload实现文件上传下载3-修改文件名显示包含中文的文件
- jsp实现文件下载与中文文件名乱码问题解决