日期:2014-05-17  浏览次数:20711 次

关于 浏览器问题【页面导出】 功能的实现 ?
代码

<%@page pageEncoding="UTF-8"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.FileInputStream"%>
<%
String path=null;
try{
String name = request.getParameter("name");
String newname = request.getParameter("fileName");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(name.getBytes("GBK"), "ISO-8859-1"));
response.setContentType("application/x-msdownload");
path = newname;
out.clear(); //添加
out = pageContext.pushBody();//添加
InputStream is = new FileInputStream(path);
OutputStream os = response.getOutputStream();
byte[] bs = new byte[1024];
int len = 0;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
os.flush();
}
os.close();
is.close();

}catch(Exception e){
e.printStackTrace();
}
%>


链接该导出页面。
问题:
ie8以上的不能导出。360浏览器能提示导出。

------解决方案--------------------

request.setCharacterEncoding("UTF-8");  //防止获取中文乱码
    String path=null;
    try{
        String name = request.getParameter("name");
        String newname = request.getParameter("fileName");
        response.setHeader("Content-disposition", "attachment;filename="
                + new String(name.getBytes("GBK"), "ISO-8859-1"));
        response.setContentType("application/x-msdownload");
        path = application.getRealPath("/")+newname;
        out.clear();             //添加  
        out = pageContext.pushBody();//添加 
        InputStream is = new FileInputStream(path);
        OutputStream os = response.getOutputStream();
        byte[] bs = new byte[1024]; 
        int len = 0;
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
            os.flush();
        }
        os.close();
        is.close();  
         
    }catch(Exception e){
        e.printStackTrace();
    }


采用ie9跟Google都木有问题!
主要是你的newname要确定路径上没有问题!!!
你可以调试或者直接输出看看newname的值!
------解决方案--------------------
楼主把文件路径打印