iffiffj 发表于 2013-2-7 13:13:28

下载文件名编码

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

设置下载文件名
downloadName = "所有领域";downloadName = getFileName(request, downloadName) + ".xls";response.setContentType(ContentType.getContentType(downloadName));response.setHeader("Content-disposition", "attachment;filename="+ downloadName);

文件名编码
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.6header = (header == null ? "" : header.toLowerCase());if (Platform.isWindows()) {// Server is Windowsif (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 Linuxif (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;}

判断操作平台
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 testreturn !Platform.isWindows() && !Platform.isMac();}}
页: [1]
查看完整版本: 下载文件名编码