0

我有一个生成 sql 文件的应用程序,我的问题是当我尝试下载文件并且请求中有重音时,我得到以下结果:

delete from gfe_param_fa where parfa_ident=9 and parfa_code_doc='ANNEXEC17' and parfa_libelle_doc='Dde certificat d''hérédité' and parfa_interaction='NON' and parfa_identifiant='POLICE' and parfa_remise='LETTRE' and parfa_application='ANNEXE_CO' and nvl(parfa_tri1,'NULL') = 'NULL' and nvl(parfa_tri2,'NULL') = 'NULL' and nvl(parfa_tri3,'NULL') = 'NULL' and parfa_archivage='OUI' and parfa_chemise='ANNEXE_GESTION' and parfa_type_appli_au='CL' and parfa_application_cl='ANNEXE_CO' and nvl(parfa_application_au,'NULL') = 'NUL

代替

delete from gfe_param_fa where parfa_ident=9 and parfa_code_doc='ANNEXEC17' and parfa_libelle_doc='Dde certificat d''hérédité' and parfa_interaction='NON' and parfa_identifiant='POLICE' and parfa_remise='LETTRE' and parfa_application='ANNEXE_CO' and nvl(parfa_tri1,'NULL') = 'NULL' and nvl(parfa_tri2,'NULL') = 'NULL' and nvl(parfa_tri3,'NULL') = 'NULL' and parfa_archivage='OUI' and parfa_chemise='ANNEXE_GESTION' and parfa_type_appli_au='CL' and parfa_application_cl='ANNEXE_CO' and nvl(parfa_application_au,'NULL') = 'NULL';

我已将问题的原因与口音隔离开来;只要请求中没有重音,它就不会发生。

这是我的下载功能:

public void download(String request) throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();
    BufferedInputStream input = null;
    BufferedOutputStream output = null;
    try {
        input = new BufferedInputStream(new ByteArrayInputStream(
                request.getBytes()), DEFAULT_BUFFER_SIZE);
        ec.responseReset();
        ec.setResponseContentType("application/sql");
        ec.setResponseContentLength(request.length());
        ec.setResponseHeader("Content-Disposition",
                "attachment; filename=\"" + "update.sql" + "\"");
        ec.setResponseHeader("Refresh", "1; url = main.xhtml");
        output = new BufferedOutputStream(ec.getResponseOutputStream(),
                DEFAULT_BUFFER_SIZE);

        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
    } finally {
        close(output);
        close(input);
    }
    fc.responseComplete();
}
4

1 回答 1

1

看这个:

ec.setResponseContentLength(request.length())

和这个

input = new BufferedInputStream(new ByteArrayInputStream(
            request.getBytes()), DEFAULT_BUFFER_SIZE);

这些行一起构成了错误。

第二行给你多少字节,是否等于字符串中的字符数?没有。“request.getBytes()”使用默认编码返回字符串的字节,这取决于平台。(它会随着操作系统/JVM/语言等而改变)。另一方面,对于给定的字符串, request.length() 将始终相同。

相反,您想要:

 // explicit character encoding, so it never changes
byte[] requestBytes = request.getBytes("UTF-8");  

然后使用该字节数组来初始化您的流确定内容长度。

于 2013-09-03T13:57:09.247 回答