我有这些文件:
在2.txt中:
Hello
ñuñííòúçç ùùù
uíúgfl正确的输出(在Eclipse (Linux)中获得):
Hello
ñuñííòúçç ùùù
uíúgfl
4 ís lèss thàn síx.请注意:
在JSP文件中,我希望获得正确的输出(在OpenShift.com),其中包含一个进程。因此,我需要改进我的文件(JAVA和JSP)。因此,JSP文件应该显示正确的输出(如果我将流程重定向到out2.txt)。现在我明白了?或者另一个奇怪的角色。我也尝试过,但没有成功:
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.print(content);编辑:的文件:
<%@ 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.请注意:
我的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/独立/配置’,包含:
...
</extensions>
-<system-properties>
<property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION" value="on"/>
</system-properties>
...我在这个XML文件中添加了两个新的属性,但是暂时什么也没有发生。我没有找到文件domain.xml (或者是domain.xml
编辑(24 APR): I创建了一个新的类文件,其中包含这个未来的字符串(Java ),或者作为一个示例:
String s= "\u00F1ñ"
... // Code 这个未来主义字符串有7个字符。我希望在JSP (对类文件的进程调用)中看到这个输出。正如我告诉过您的,我已经创建了一个新的类文件,只有一个char (‘new’)。在我的JSP文件中,我获得:
241
\u00F1我希望:
241
ñ注: 241是“i”的%d。
我打算这样做,例如将所有字符转换为UTF-8,而不是假unicode ("\uXXXX")。我需要主意。
编辑(28 APR): --我的最终目标是使用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下,它运行正常。如何解决这个问题?
发布于 2015-04-22 13:10:00
请在jsp上添加utf-8编码。
<%@ page contentType="text/html; charset=UTF-8"%>发布于 2015-04-22 16:31:23
看起来好像在bash中没有得到UTF-8 (正确的地区),甚至fileReader2也不能正常工作。您可以通过编写JSP源\u00F1 (即ñ )来测试UTF-8的输出。
你需要Linux nerds。或立即这样做:
File file = File.createTempFile("fR2-", null);
PrintStream oldOut = System.out;
try (PrintStream capture = new PrintStream(file, "UTF-8")) {
System.setOut(capture);
fileReader2.main(new String[0]);
System.out.flush();
} finally {
System.setOut(oldOut);
} // closes capture.这将创建一个写入(临时)文件的PrintStream。回忆会更好。它使用UTF-8,所以可以使用所有Unicode。
然后,System.setOut将输出设置为捕获。
String str = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
file.delete();https://stackoverflow.com/questions/29795092
复制相似问题