难做到 ,txt文件就是个很棘手的问题 ,临时转换扩展名可以试下

解决方案 »

  1.   

    谢谢tantaiyizu,就没有一个方法??用户传上的*.rar文件他都是用浏览器打开的,而且是乱码。但是*.zip和*.exe文件他是弹出对话框提示保存的。
      

  2.   

    http://blog.csdn.net/chinmo/archive/2008/01/29/2071979.aspx
    不知道这个方法对你有什么提示作用没?
      

  3.   


    <%@ page contentType="text/html; charset=GBK"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    <%@ page import="java.io.File"%>
    <%@page import="java.io.InputStream"%>
    <%@page import="java.io.FileInputStream"%>
    <%@page import="com.cpp.util.Constant"%>
    <%
        String path = request.getParameter("path") ;
        if(path==null || "".equals(path)){
            out.write("<script>") ;
            out.write("alert('要下载的文件不存在');") ;
            out.write("history.go(-1);") ;
            out.write("</script>") ;
        }
        String realPath = Constant.BASE_DIR + path ;
        File file = new File(realPath) ;
        if(!file.exists()){
            out.write("<script>") ;
            out.write("alert('要下载的文件不存在');") ;
            out.write("history.go(-1);") ;
            out.write("</script>") ;
        }else{
            long l = file.length() ;
            InputStream in = new FileInputStream(file) ;
            String fs = file.getName() ;
            if(in!=null){
                response.reset();
                response.setContentType("application/x-msdownload");
                response.setHeader("Content-disposition","attachment; filename="+new String(fs.getBytes("GBK"),"iso8859-1"));
                //以上输出文件元信息
                response.setContentLength((int)l);      //设置输入文件长度            byte[] b = new byte[2048]; 
                int len = 0; 
                while((len=in.read(b)) >0)
                    response.getOutputStream().write(b,0,len);      //向浏览器输出
                in.close();         //关闭文件输入流
                response.flushBuffer();
                out.clear();
                out = pageContext.pushBody();
            }
        }
    %>这个download.jsp文件应该可以 要下载时后面加参数path=
    无论任何文件均转化为输出流下载
      

  4.   

    Constant.BASE_DIR 一个常量 是我配置的web根目录 你改成自己的就可以
      

  5.   

    有没有在前台用js就可以check掉的,而不用改后台代码。