1、获取附件的后缀名
String fileName = " test.txt ";
int lastIndex = fileName.lastIndexOf(".");
int length = fileName.length();
String suffix = fileName.substring(lastIndex, length); // suffix值为 " .txt "
如果考虑用String的split方法进行分割时,注意:
分割符号是点的话,对应 String.split("[.]")
分割符号是竖线的话,对应 String.split("\\|")
分割符号是星号的话,对应 String.split("\\*")
分割符号是斜线的话,对应 Sring.split("\\\\")
2、设置响应头中的中文问题
response.setHeader("Content-disposition","attachment;filename="+fileName);
客户端不能正确显示中文,而且会有如下错误:
Internet Explorer 无法下载fileDownload.do(来自localhost)。
Internet Explorer 无法打开该Internet站点。请求的站点不可用,或找不到。请以后再试。
解决的办法,是先将fileName进行转码,就可以了:
fileName = new String(fileName.getBytes("GBK"),"iso8859-1");
response.setHeader("Content-disposition","attachment;filename="+fileName);