做了个功能,有上传图纸的功能(CAD图纸),已经实现,在页面显示时,要提供对CAD图纸的下载功能。原来只是做了个链接,如<a href="../../aa.dwg">下载图纸</a>,发现IE打开了一个新页面,都是乱码,如果是xls,doc,pdf等格式的文件,IE会弹出一个文件下载/另存为的对话框,提供下载。但是IE无法对DWG文件提供下载识别。
现在想通过代码实现文件下载,但是遇到问题了,代码实现的话会用到文件流和response对象,但我做的项目是用公司平台,页面发出请求到一个JAVA类文件中处理,而这个类也是平台固定的继承了一个特定的抽象类,但是该类不提供response对象,所以我无法得到response对象来操作,已经头晕了,不知道该怎么办。
各位高手帮帮忙。给个思路,最好贴个详细的代码。很急!分不够可以再加,解决就给分。

解决方案 »

  1.   

    ...但是该类不提供response对象,
    那response对象不可以从其它地方来吗?
      

  2.   

    如果是tomcat,找到conf/web.xml
    加入:    <mime-mapping>
            <extension>dwg</extension>
            <mime-type>application/octet-stream</mime-type>
        </mime-mapping>里面象这样的内容很多。
      

  3.   

    public Hashtable<String, Object> download(String fileid, String basePath,
    Hashtable<String, Object> hsTmp, HttpServletRequest request,
    HttpServletResponse response) {

    TBmpMtannex tbx1 = new TBmpMtannex();//存储文件信息的持久类
    tbx1 = (TBmpMtannex) (daoTool
    .find("from TBmpMtannex as a where a.MFileId='" + fileid + "'"))
    .get(0);//根据ID找到那条数据
    // HttpServlet servlet = null;
    String path = tbx1.getMSpace();//
    // String path
    // =servlet.getServletContext().getRealPath("")+"\\upload\\";
            String fileName = tbx1.getMFileName();//
    fileName = fileName.substring(fileName.indexOf("=") + 1);//
    String filePath = path;//
    String file = filePath + "/" + fileName;//
    System.out.println(file);//
    File fl=new File(file);//
    if(!fl.exists()){//
    response.setContentType("text/html;charset=UTF-8");//
    PrintWriter out;//
    try {
    //String message="对不起,文件不存在!";
    out = response.getWriter();//
    out.write("<script language='javascript'>alert('对不起,文件不存在!');history.go(-1);</script>");//
    out.flush();//
    out.close();//

    } catch (IOException e) {
    System.out.println("文件不存在"+e);
    }

    }
    FileInputStream fis = null;//
    OutputStream os = null;//
    byte[] bos = new byte[1024];//
    int length = 0;//
    try {
    response.setContentType("application/x-msdownload");//
    response.setHeader("Content-disposition", "attachment;filename="//
    + new String(fileName.getBytes("gb2312"), "iso8859-1"));//
    fis = new FileInputStream(file);//
    os = response.getOutputStream();//
    while ((length = fis.read(bos)) != -1) {//
    os.write(bos, 0, length);//
    os.flush();//
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    try {
    if (os != null)
    os.close();//
    if (fis != null)
    fis.close();//
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    } return hsTmp;
    }
    我的是这样写的   你看看 对你的 有用没 
      

  4.   


    不好意思啊,我的JAVA处理类,类似于Servlet是无法获取response类的......
      

  5.   

    建议你上传是就做处理,判断如果是dwg后缀的就给它加一个后缀名,这样不就可以勒吗。希望对你有帮助
      

  6.   

    是什么web容器? tomcat还是JBoss还是其他?
      

  7.   

    1、tomcat容器:
     #3楼正解
    2、jboss容器:
      jboss\server\default\deploy\jbossweb-tomcat50.sar\conf\web.xml
      做同样的修改即可。
    3、其他的容器
      有待研究!