下载文件名编码

[b]下载文件的编码在不同的浏览器上都有所不同,也与WEB服务器部署的系统有关[/b]

[b]设置下载文件名[/b]

downloadName = "所有领域";
downloadName = getFileName(request, downloadName) + ".xls";
response.setContentType(ContentType.getContentType(downloadName));
response.setHeader("Content-disposition", "attachment; filename="+ downloadName);


[b]文件名编码[/b]

private String getFileName(HttpServletRequest request, String fileName) {
String downloadName = fileName;
try {
String header = request.getHeader("User-Agent");
//Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FDM; CIBA)
//Opera/9.60 (Windows NT 5.1; U; en) Presto/2.1.1
//Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
header = (header == null ? "" : header.toLowerCase());
if (Platform.isWindows()) {// Server is Windows
if (header.indexOf("msie") > -1) {
downloadName = downloadName.replaceAll(" ", "20%");
downloadName = new String(downloadName.getBytes(),
"ISO8859-1");
} else if (header.indexOf("mozilla") > -1) {
downloadName = downloadName.replaceAll(" ", "_");
downloadName = new String(downloadName.getBytes(),
"ISO8859-1");
} else if (header.indexOf("opera") > -1) {
downloadName = downloadName.replaceAll(" ", "_");
downloadName = new String(downloadName.getBytes(),
"ISO8859-1");
} else {
downloadName = downloadName.replaceAll(" ", "_");
downloadName = new String(downloadName.getBytes(),
"ISO8859-1");
}
} else {// Server is Linux
if (header.indexOf("msie") > -1) {
downloadName = URLEncoder.encode(downloadName, "UTF8");
} else if (header.indexOf("mozilla") > -1) {
downloadName = downloadName.replaceAll(" ", "_");
downloadName = new String(fileName.getBytes("UTF-8"),
"ISO8859-1");
} else if (header.indexOf("opera") > -1) {
downloadName = downloadName.replaceAll(" ", "_");
downloadName = new String(downloadName.getBytes(),
"ISO8859-1");
} else {
downloadName = downloadName.replaceAll(" ", "_");
downloadName = new String(downloadName.getBytes(),
"ISO8859-1");
}
}
} catch (Exception e) {}
return downloadName;
}


[b]判断操作平台[/b]

public final class Platform {
private static final int UNSPECIFIED = -1;
private static final int MAC = 0;
private static final int LINUX = 1;
private static final int WINDOWS = 2;
private static final int SOLARIS = 3;
private static final int FREEBSD = 4;
private static final int osType;

static {
String osName = System.getProperty("os.name");
if (osName.startsWith("Linux")) {
osType = LINUX;
} else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
osType = MAC;
} else if (osName.startsWith("Windows")) {
osType = WINDOWS;
} else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) {
osType = SOLARIS;
} else if (osName.startsWith("FreeBSD")) {
osType = FREEBSD;
} else {
osType = UNSPECIFIED;
}
}

private Platform() {}

public static final boolean isMac() {
return osType == MAC;
}

public static final boolean isLinux() {
return osType == LINUX;
}

public static final boolean isWindows() {
return osType == WINDOWS;
}

public static final boolean isSolaris() {
return osType == SOLARIS;
}

public static final boolean isFreeBSD() {
return osType == FREEBSD;
}

public static final boolean isX11() {
// TODO: check FS or do some other X11-specific test
return !Platform.isWindows() && !Platform.isMac();
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值