如题,在FF下测试是正常的,chrome 也正常,IE7以上版本也正常,惟独IE6不行。
可在 http://www.tuanhaoduo.com 主页左上角上测试(绝非广告贴,还请管理员谅解)。
求解,谢谢了。
@Controller @RequestMapping("/*.do") public class IndexController extends BaseController { // 省略其他无关函数 @RequestMapping public String down4desk(HttpServletRequest request, HttpServletResponse response) { response.setContentType("application/x-msdownload"); String content = ""; try { response.setHeader("Content-Disposition", "attachment;filename=" + new String("团好多.url".getBytes("gbk"), "iso-8859-1")); content = FileUtil.read(Constant.HTMLROOT + "tuanhaoduo.url", true, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return renderText(request, response, content); } }
FileUtil类中的相关函数:
/** * 读取文件 * * @param filename * @param isSet * 是否需要加上换行 * @param strCode * 编码,如GBK * @return * @throws IOException */ public static String read(String filename, boolean isSet, String strCode) throws IOException { File file = new File(filename); InputStreamReader read = new InputStreamReader( new FileInputStream(file), strCode); BufferedReader reader = new BufferedReader(read); String line = ""; StringBuffer readfile = new StringBuffer(); while ((line = reader.readLine()) != null) { readfile.append(line); if (isSet) readfile.append("\r\n"); } reader.close(); read.close(); return readfile.toString(); }
其中,Constant.HTMLROOT 的值从配置中取得,值为:/home/htmlfile/tuan/
模板语言用到了 apache volicity, 控制器用到了 springMVC。
我在ubuntu下试了一下下载一个.url的文件,ff是正常的名字。
如果你有截包的工具的话,如fiddler,截包看一下,浏览器接收到的contentTye和Content-Disposition是不是正确的?
下面是我的测试代码跟传输数据有关的部分。
[code="java"]InputStream is = null;
OutputStream os = null;
try {
resp.setContentType("application/octet-stream");
resp.setHeader("Content-disposition", "attachment;filename=" + new String(fn.getBytes("utf-8"), "iso-8859-1"));
is = new FileInputStream(file);
os = resp.getOutputStream();
int count = 0;
byte[] buffer = new byte[8192];
while((count = is.read(buffer)) != -1){
os.write(buffer, 0, count);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}[/code]
问题在:down4desk.do
几个浏览器比较了下:
是IE6取到的名称是错误的:down4desk_do
其他几个浏览器能正确取到名称:团好多.url
我也不是很清楚,希望有所提示吧!
把contentType由application/x-msdownload改成application/octet-stream试试看?
HTML文件是能取到的,怎么出现两个名字?
MIME类型各个浏览器的处理效果有可能不一样的,另外可能是ie6比较挫吧。。。果断放弃ie6吧。。。
文件名应该是Content-Disposition头的filename字段决定的吧,截包看一下这个字段是否正确呢?
关于这两个mime类型介绍的资料:
http://mimeapplication.net/x-msdownload
http://mimeapplication.net/octet-stream