没有扩展程序的Chrome下载文件(IE和Mozilla做得很好)

时间:2014-12-11 18:08:17

标签: jsp google-chrome java-ee

我正在尝试向用户发送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;
}

如果我更改了contentType,它对于IE或Mozilla来说很好,它们都会根据新的内容类型更改扩展名(即使它不是文档的好内容)

但Chrome ..它没有为下载的文件添加任何扩展程序,我无法找到原因。

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

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以通过添加Content-Disposition标头来设置文件名:

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

这也可能看起来比浏览器可能生成的丑陋名称更好。