---
title: "JSP中文附件名问题"
author: "fansofjava"
date: "2011-12-15T16:37:20Z"
tags: []
categories: ["Web前端"]
slug: "jsp"
draft: false
lastmod: "2011-12-16T09:04:46Z"
id: 1306264
url: "https://fansofjava.iteye.com/blog/1306264"
---

帖一段代码，这段代码用于处理IE与Firefox下的中文编码问题：

```java

String expName = "";
if(request.getHeader("user-agent").indexOf("Firefox") > 0){
xpName = new String("智能服务机当日挂号分析.xls".getBytes("UTF-8"), "ISO-8859-1");
}else{
expName = URLEncoder.encode("智能服务机当日挂号分析.xls", "UTF-8");
}

response.setHeader("Content-Disposition", new StringBuilder("attachment;filename=\"").append(expName).append("\"").toString());
response.setContentType("application/msexcel");
workbook.write();
workbook.close();

```


这段代码可以解决在firefox与IE下附件中文乱码问题。
如果还是有乱码的话，在tomcat/server.xml中URI编码加上:

```xml

 <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8"/>

```