日期:2014-05-18  浏览次数:20769 次

一个JSP DOWNLOAD的问题~~~
private   void   doDownload()   throws   IOException   {

String   filename   =   "模板.xls ";
        response.setHeader( "Content-disposition ", "attachment;filename= "+filename);
        response.setContentType( "application/msexcel ");  
       
        BufferedInputStream   bis   =   null;
OutputStream   out   =   response.getOutputStream();

        try     {    

                bis   =   new   BufferedInputStream(new   FileInputStream(( "C:\\ "+   filename)));
                byte[]   buff   =   new   byte[2048];
                int   bytesRead;

                while(-1   !=   (bytesRead   =   bis.read(buff,   0,   buff.length)))   {
                out.write(buff,0,bytesRead);
                }
               
        }   catch(final     IOException     e)     {    
                        System.out.println     ( "ERROR ");    
        }   finally   {
                        bis.close();    
                        out.close();    
        }  
}
以上代码实装后,我点下载按钮,程序把EXCLE文件直接在HTML上打开了,并没有出现打开和保存的对话框,请问是哪有问题??

------解决方案--------------------
response.setContentType( "application/octet-stream ");
这个楼上说的比较关键,好像差不多!
------解决方案--------------------
response.setContentType( "unknown ");

这个文件类型在系统中与浏览器关联了,结果就变成在IE中打开。
常见的有word, excel, pdf等。因此将文件转成数据流让浏览器不知道其文件类型而达到下载的目的。

------解决方案--------------------
在web.xml中加入
<mime-mapping>
<extension> xls </extension>
<mime-type> application/zip </mime-type>
</mime-mapping>


------解决方案--------------------
如果没关联
response.setContentType( "application/octet-stream ");
这个就可以了。

至少我这里一致那么写的。
------解决方案--------------------
是不是参数不对
------解决方案--------------------
response.setContentType( "text/html; charset=GBK ");
删除 response.setContentType( "application/msexcel ");

------解决方案--------------------
将这个 response.setContentType( "application/msexcel ");
替换成 response.setContentType( "application/x-msdownload;charset=iso-8859-1 ");

加上 out.flush();



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