【发布时间】:2015-04-22 10:45:10
【问题描述】:
我有这些文件:
in2.txt:
Hello
ñuñííòúçç ùùù
uíúgfl
正确的输出(我在 Eclipse (Linux) 中获得):
Hello
ñuñííòúçç ùùù
uíúgfl
4 ís lèss thàn síx.
注意:
输入(文件)和输出有'ñ'、'í'、...
'4'(在输出中)是输入(文件)的行数。
输出包含字符 'í'、'è'、...
在一个 JSP 文件中,我想通过一个过程获得正确的输出(在 OpenShift.com 上)。所以,我需要改进我的文件(JAVA 和 JSP)。因此,JSP 文件应该向我显示正确的输出(如果我将进程重定向到 out2.txt)。目前我得到'?或其他奇怪的字符。我也试过了,不成功:
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.print(content);
编辑:我的 JSP 文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Try 2</title>
<script type="text/javascript">
</script>
</head>
<body>
<% ProcessBuilder pb = new ProcessBuilder("bash", "-c", "java fileReader2");
Process process = pb.start();
// Process process = Runtime.getRuntime().exec("java fileReader2");
while (process.waitFor()!=0){};
InputStream shellIn = process.getInputStream();
Writer writer = new StringWriter();
int num=1;
char[] buffer=new char[num];
try {
Reader reader = new BufferedReader(new InputStreamReader(shellIn,"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
}
finally{
shellIn.close();
}
String str = writer.toString();%>
<form>
<TEXTAREA NAME="textarea2" ROWS="15" COLS="1024" readonly="readonly"><%=str %>
</TEXTAREA>
</form>
</body>
OpenShift.com 上的输出不正确:
Hello
�u������� ���
u��
5 ?s l?ss th?n s?x.
注意:
缺少字符“gfl”。
我又得到了一行(4+1=5)。
出现奇怪的字符和'?'s。
我的 JAVA 文件:
import java.io.*;
public class fileReader2{
public static void main (String argsv[]){
try{
FileInputStream fis = new FileInputStream("in2.txt");
String content="";
InputStreamReader isr = new InputStreamReader(fis,"utf8");
BufferedReader br = new BufferedReader(isr);
String line;
int i=0;
while((line = br.readLine()) != null){
i++;
content=content.concat(line).concat("\n");
}
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.print(content);
if (i<6){
System.out.print(i+" ís lèss thàn síx.");
}
fis.close();
}catch(Exception e1){}
}
}
编辑 2: 我发现:
我的standalone.xml 位于'.../jbossas/standalone/configuration',并且包含:
...
</extensions>
-<system-properties>
<property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION" value="on"/>
</system-properties>
...
我在这个 XML 文件中添加了 2 个新属性,但目前没有任何反应。我没有找到 domain.xml 文件(以及 '.openshift/action-hooks/pre_start_jbossas-7')。
编辑(4 月 24 日):我创建了一个新的 CLASS 文件,使用这个未来的字符串(Java 代码),或者作为示例:
String s= "\u00F1ñ"
... // Code
这个未来主义字符串有 7 个字符。我想在 JSP 中看到这个输出(一个进程调用我的 CLASS 文件)。正如我告诉你的,我创建了一个新的 CLASS 文件,其中只有一个字符('ñ')。在我的 JSP 文件中,我获得:
241
\u00F1
我希望:
241
ñ
注意:241 是 'ñ' 的 %d。
我打算这样做,例如将所有字符转换为 UTF-8,而不是错误的 unicode ("\uXXXX")。我需要想法。
编辑(4 月 28 日):我的最终目标是使用 JLex(示例代码):
import java.io.*;
import java.lang.*;
%%
%{
public static void main (String argv [])
throws java.io.IOException {
if (argv.length != 1) {
System.out.println("Usage:");
System.out.println("\tjava fileReader filename.txt");
return ;
} else {
String fInName = argv [0];
if (!fInName.endsWith(".txt")) fInName = fInName + ".txt";
FileInputStream input = new FileInputStream(fInName);
//Create lexical analyzer
fileReader yy = new fileReader (input);
//Process input file
while (yy.yylex()!=-1);
// Show stats
}
} //End main
%}
%class fileReader
%unicode
%line
%eof{
if ((yyline+1)<6){
System.out.println();
System.out.print((yyline)+" ís lèss thàn síx.");
}
%eof}
%integer
%state
break=[\r\n]
%%
<YYINITIAL>{break} { System.out.print(yytext()); }
<YYINITIAL>. { System.out.print(yytext()); }
在 OpenShift.com 我得到了这个:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /1x2/try_utf8.jsp.
Reason: Error reading from remote server
--------------------------------------------------------------------------------
Apache/2.2.15 (Red Hat) Server at jlex1x2-uocpfc.rhcloud.com Port 80
在 Linux 上运行正常。如何解决?
【问题讨论】:
-
您是否在 JSP 页面中添加了字符集 UTF-8?
-
@mushfek0001:是的。我已经编辑了我的问题。
标签: java jsp utf-8 openshift lex