最好能提示是打开还是另存,那位大侠能给个例子,给段代码,小弟谢过了

解决方案 »

  1.   

    response.addHeader("Content-Disposition","attachment;filename="+fileName);
      

  2.   

    http://community.csdn.net/Expert/topic/4719/4719998.xml?temp=.2609674
      

  3.   

    public static boolean downLoad(String path,String name,String filename,HttpServletResponse response)
    {
    File tfile=new File(path+"/" + name +"/"+ filename);
    String contentType = "";
    if(filename.toLowerCase().indexOf(".xls")>0)
    {
    contentType="application/vnd.ms-excel;" +
    "charset=UTF-8;attachment; filename=" + filename;
    }
    else
    {
    contentType="application/vnd.ms-zip;" +
    "charset=UTF-8;attachment; filename=" + filename;
    }
    byte[] buffer = new byte[8192];
    //Create the download files
    int bytesRead = 0;
    if(tfile != null)
    {
    response.setContentType(contentType);
    response.setHeader(
    "content-disposition",
    "attachment; filename=\"" + filename + "\"");
    try
            {
    FileInputStream is = new FileInputStream(tfile);
    BufferedOutputStream oStream =
    new BufferedOutputStream(response.getOutputStream());
    //Get file stream
    while((bytesRead=is.read(buffer,0,8192))!=-1)
    {
    oStream.write(buffer,0,bytesRead);
            }
    oStream.flush();
    if (oStream != null) {
    oStream.close();
    }
    response.flushBuffer();
            }
            catch(IOException ioe)
            {
            System.out.println("ssss==>" + ioe.toString());
            return false;
    //        errors.add("nofile",new ActionMessage("file.not.exsit.error"));
    // this.saveErrors(request,errors);
    //        return (new ActionForward(mapping.getInput()));
            }
            catch(Exception e)
            {
            return false;
           
    //        errors.add("nofile",new ActionMessage("file.not.exsit.error"));
    // this.saveErrors(request,errors);
    //        return (new ActionForward(mapping.getInput()));
            }
            }

    return true;
    }
      

  4.   

    <a href=".../xx.doc">open the document</a>
      

  5.   

    <a href=".../test1.doc">open the word</a>
    <a href=".../test2.xls">open the excel</a>
    并且在web.xml里面加上下面两句:
        <mime-mapping>
            <extension>doc</extension>
            <mime-type>application/vnd.ms-word</mime-type>
        </mime-mapping>
        
        <mime-mapping>
            <extension>xls</extension>
            <mime-type>application/vnd.ms-excel</mime-type>
        </mime-mapping>
      

  6.   

    Sunny319(努力学习java中.) 
    正解,顺便也谢谢你,解决了我一个问题,嘿
      

  7.   

    net_lover(【孟子E章】) ,Sunny319(努力学习java中.) ,For_suzhen(不懂装懂):
    各位大哥,IE不是可以自己打开word,excel,pdf文档的吗?比如:C:\Data\下有1.word,2.excel.3.pdf这三个文件,你直接把C:\Data做个虚拟目录,这样jsp页面就可以直接打开了啊!我以前都是这样用的!不知道各位大哥怎样用的,讨教了!
      

  8.   

    这样页面上点击1.word,就会链接http://localhost:8080/Data/1.word就可以打开了啊!
      

  9.   

    String filepath = "upfile/";
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition","attachment;filename=\""+filename+"\"");
      

  10.   

    zs178(zh-cn) :
    你能解释下你上面的意思吗?是不是什么类型的文件都可以打开呢?我上面的方法什么类型的文件都可以打开的!
      

  11.   

    它那个word,excel,pdf文档不一定在本机啊.一般是放在web服务器上,供客户端访问!
      

  12.   

    这个是本人查的资料,你可以试试:
    对word文档的处理(上传与下载)
    <%@ page contentType="application/msword" %>
    <!-- 以上这行设定本网页为excel格式的网页 -->
    <%
       response.setHeader("Content-disposition","inline; filename=test1.doc"); //线上浏览方式
      // response.setHeader("Content-disposition","attachment; filename=test1.doc");//下载方式
       //以上这行设定传送到前端浏览器时的档名为test1.doc
       //就是靠这一行,让前端浏览器以为接收到一个word档
    %>
    //然后输出动态内容就可以得到一个word文档了
     
    1,打开:
    1)文件头上加:<%@ page  contentType="application/msword"%> 
    xml文件里:
    <mime-mapping>
            <extension>doc</extension>
            <mime-type>application/msword</mime-type>
    </mime-mapping>
    2)可以用js,以下代码来自引用:
    <%@ page contentType="text/html;charset=gb2312" import= "java.io.*"%>
    <HTML>
    <script>
    var wrd=new ActiveXObject("Word.Application")
    wrd.visible=true
    alert ("您的"+wrd.Application.Caption+"安装路径为:\n"+wrd.Application.Path+"\n版本号是:"+ wrd.Application.version+"\n注册使用者是:"+wrd.Application.UserName)
    wrd.Documents.Add()
    //wrd.Documents.Open("c:\\exam.doc")
    wrd.Selection.TypeText("This is some text.")
    wrd.Application.Activate()
    wrd.ActiveDocument.SaveAs("c:\\exam111.doc")
    wrd=null
    </script>
    </HTML>
      

  13.   

    shirley_qi(小菜鸟++ == 学海无崖 ==) :
    假如我把C:\Data下面的所有文件给显示到jsp页面上,然后点击各个文件,就可以打开每个文件,这个怎么实现呢?我C:\Data下的文件有1.doc,2.ppt,3.xml,4.txt,5.pdf...也就是说C:\Data下可能存放各种类型的文件!难道你要对每种文件类型单独处理吗?你上面处理一个word类型的就写那多代码了,其它类型的怎样处理呢?期待着您的回复!