ファイル名コードのダウンロード


ダウンロードファイルのコードはブラウザによって異なり、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.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;
}

判断操作プラットフォーム

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();
	}
}