我有一个生成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();
}
答案 0 :(得分:1)
看看这个:
ec.setResponseContentLength(request.length())
和这个
input = new BufferedInputStream(new ByteArrayInputStream(
request.getBytes()), DEFAULT_BUFFER_SIZE);
这些行一起构成了这个bug。
第二行给你多少字节,是否等于字符串中的字符数?不。 “request.getBytes()”使用默认编码返回字符串的字节,默认编码取决于平台。 (它将随OS / JVM /语言等而改变)。另一方面,对于给定的字符串,request.length()将始终相同。
相反,你想要:
// explicit character encoding, so it never changes
byte[] requestBytes = request.getBytes("UTF-8");
然后使用该字节数组初始化您的流和以确定内容长度。