问题描述
try{Stringpath="D:/到货验收模板.xls";//path是指欲下载的文件的路径。Filefile=newFile(path);//取得文件名。Stringfilename=file.getName();//取得文件的后缀名。Stringext=filename.substring(filename.lastIndexOf(".")+1).toUpperCase();//以流的形式下载文件。InputStreamfis=newBufferedInputStream(newFileInputStream(path));byte[]buffer=newbyte[fis.available()];fis.read(buffer);fis.close();//清空responseresponse.reset();//设置response的Headerresponse.addHeader("Content-Disposition","attachment;filename="+newString(filename.getBytes()));response.addHeader("Content-Length",""+file.length());OutputStreamtoClient=newBufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();out.clear();out=pageContext.pushBody();}catch(IOExceptionex){ex.printStackTrace();}弹出来的对话框是未知的类型
解决方案
解决方案二:
//文件下载publicActionForwarddown(ActionMappingmapping,ActionFormform,HttpServletRequestreq,HttpServletResponseresp)throwsIOException{Stringurl=req.getParameter("url");String[]str=url.split("\_");//获得文件从放的相对路径ServletContextcontext=req.getSession().getServletContext();Stringrealurl=context.getRealPath("/file/tmp/"+str[0]+"/"+url);//下载文件url=URLEncoder.encode(url,"UTF-8");resp.setContentType("application/x-download");resp.setHeader("Content-Disposition","attachment;filename="+url);InputStreamin=newFileInputStream(realurl);OutputStreamout=resp.getOutputStream();byte[]b=newbyte[in.available()];in.read(b);out.write(b);out.close();in.close();returnnull;}
解决方案三:
response.setContentType("application/octet-stream");自己搜下excel的mimetype,不是application/octet-stream
解决方案四:
网上很多实例的,自己好好找找
解决方案五:
该回复于2011-04-18 09:25:27被版主删除
解决方案六:
response.reset();response.setContentType("application/vnd.ms-excel;charset=uft-8");response.setHeader("Content-Disposition","attachment;filename="+fileName);
解决方案七:
hanfeng_st您好!能再提供给别的像这种写法的application/vnd.ms-excel的别的类型的。比如word,ppt,pdf等其他的吗
解决方案八:
Stringpath="D:/到货验收模板.xls";应该是Stringpath="D:\到货验收模板.xls";的吧!
解决方案九:
引用6楼xinxin867662924的回复:
hanfeng_st您好!能再提供给别的像这种写法的application/vnd.ms-excel的别的类型的。比如word,ppt,pdf等其他的吗
关于下载文件,所需要的文件类型,我这里可以帮助楼提供word,ppt,pdf的类型名称,分别如下:xls:application/vnd.ms-excelword:application/mswordppt:application/vnd.ms-powerpointpdf:application/pdf若楼主还想要其它文件的类型,可以再发出来,我一一提供。希望楼主能给分,呵呵,初到CSDN不久望见谅..积蓄人气和资源..