Jsp Chrome无扩展名下载文件(IE和mozilla做得很好)

Jsp Chrome无扩展名下载文件(IE和mozilla做得很好),jsp,google-chrome,jakarta-ee,Jsp,Google Chrome,Jakarta Ee,我正在尝试向用户发送msword文件 下面是创建outputStream的代码,这是一个已有10多年历史的应用程序: private PrintWriter parse(long docId) throws BusinessException { HttpServletResponse response = get_JessContext().getServletResponse(); response.setContentType("application/msword");

我正在尝试向用户发送msword文件

下面是创建outputStream的代码,这是一个已有10多年历史的应用程序:

private PrintWriter parse(long docId) throws BusinessException {
    HttpServletResponse response = get_JessContext().getServletResponse();
    response.setContentType("application/msword");

    PrintWriter out;

    try {
        out = response.getWriter();
    } catch (IOException e1) {
        e1.printStackTrace();
        out = null;
    }
    BufferedReader document = null;
    FileInputStream file = null;
    String docUrl = new String(EMPTY_STRING);
    StringBuffer agenceDoc = new StringBuffer(EMPTY_STRING);
    int startIndex = -1;
    int endIndex = -1;
    String parameter = null;
    InputStreamReader inputStream = null;
    try {

        if (docId == -1) {
            adminService.getDocument(agenceDoc, get_UserAgenceId()
                    .longValue(), getDocNameNormalise());

        } else

        if (docId > 0) {
            adminService.getDocument(agenceDoc, get_UserAgenceId()
                    .longValue(), docId);
        }

        if (docId == -1) {
            if (agenceDoc.toString().equals(EMPTY_STRING)) {

                docUrl = JSPFile.getDocsdir()
                        + JSPFile.getFile(get_JessContext(),
                                getDocNameNormalise());

                file = new FileInputStream(docUrl);
                inputStream = new InputStreamReader(file);
            } else {
                logger.debug("agenceDoc.toString() du document="
                        + agenceDoc.toString());
                inputStream = new InputStreamReader(
                        new StringBufferInputStream(agenceDoc.toString()));
            }
        } else if (docId > 0) {
            inputStream = new InputStreamReader(
                    new StringBufferInputStream(agenceDoc.toString()));
        } else {
            logger.debug(" JSPCourrier : doc_id non valide");
            throw new Exception();
        }
        document = new BufferedReader(inputStream);

        String line = null;
        String lineKeep = null;
        String lineOut = null;
        boolean first = true;
        boolean enteteComplet = true;
        do {
            line = document.readLine();
            if (line != null) {
                if (lineKeep != null) {
                    String lineTemp = lineKeep + line;
                    line = lineTemp;
                    lineKeep = null;
                }
                if (enteteComplet) {

                    startIndex = 0;
                    lineOut = null;
                    first = true;
                    do {
                        if (startIndex != 0) {
                            startIndex += START_SEPARATOR.length();
                        }
                        if (startIndex == 0 && !first)
                            startIndex += START_SEPARATOR.length();
                        startIndex = line.indexOf(START_SEPARATOR,
                                startIndex);
                        if (startIndex != -1) {
                            if (first) {
                                lineOut = line.substring(0, startIndex);
                                first = false;
                            } else {
                                lineOut += line.substring(endIndex
                                        + END_SEPARATOR.length(),
                                        startIndex);
                            }
                            endIndex = line.indexOf(END_SEPARATOR,
                                    startIndex);
                            if (endIndex == -1) {
                                lineKeep = line.substring(startIndex);
                                startIndex = -1;
                            }
                            if (endIndex != -1) {
                                parameter = line.substring(startIndex
                                        + START_SEPARATOR.length(),
                                        endIndex);
                                lineOut += getParametre(parameter);
                            }
                        }
                    } while (startIndex != -1);
                    if (lineOut != null) {
                        if (lineKeep == null) {
                            lineOut += line.substring(endIndex
                                    + END_SEPARATOR.length());
                        }
                    } else {
                        lineOut = line;
                    }

                    out.println(lineOut + "\r\n");
                }
            }
        } while (line != null);
    } catch (BusinessException be) {
        throw be;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document != null)
            try {
                document.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        if (file != null)
            try {
                file.close();
            } catch (IOException e3) {
                e3.printStackTrace();
            }
    }
    return out;
}
如果我更改内容类型,IE或Mozilla也可以,它们都会根据新的内容类型更改扩展名,即使对文档来说不是很好的扩展名

但是铬。。它没有向下载的文件添加任何扩展名,我也不知道为什么

有关信息,下载的文件是.rtf文件,但必须使用MS Word读取


感谢您的帮助

您可以通过添加内容处置标题来设置文件名:

response.setContentType("application/msword");
response.addHeader("Content-Disposition", "filename=whatever.rtf");

这也可能比浏览器可能产生的难看的名字看起来更好。

太棒了!你一次解决了我的两个问题:非常感谢!我需要了解标题^^