您的位置:首页 > 编程语言 > Java开发

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: