【发布时间】:2012-06-03 15:42:10
【问题描述】:
我有一个包含 823237 个字符的字符串。它实际上是一个 xml 文件,出于测试目的,我想作为响应形式返回一个 servlet。
我已经尝试了我能想到的一切
1) 用整个字符串创建一个常量...在这种情况下 Eclipse 抱怨(在 servlet 类名下有一条红线) -
The type generates a string that requires more than 65535 bytes to encode in Utf8 format in the constant pool
2) 将整个字符串分成 20 个字符串常量并直接写入out 对象
类似:
out.println( CONSTANT_STRING_PART_1 + CONSTANT_STRING_PART_2 +
CONSTANT_STRING_PART_3 + CONSTANT_STRING_PART_4 +
CONSTANT_STRING_PART_5 + CONSTANT_STRING_PART_6 +
// add all the string constants till .... CONSTANT_STRING_PART_20);
在这种情况下……构建失败……抱怨……
[javac] D:\xx\xxx\xxx.java:87: constant string too long
[javac] CONSTANT_STRING_PART_19 + CONSTANT_STRING_PART_20);
^
3) 将 xml 文件作为字符串读取并写入 out object .. 在这种情况下,我得到了
SEVERE: Allocate exception for servlet MyServlet
Caused by: org.apache.xmlbeans.XmlException: error: Content is not allowed in prolog.
最后我的问题是......我怎样才能从servlet 返回这么大的字符串(作为响应)???
【问题讨论】:
-
我用 StringBuffer 做了,但构建再次失败,我得到与 2 one 相同的错误
-
注意 String + String 创建一个新的 String ;-)
-
在 XML 文档中的 prolog
<?xml version="1.0"?>之前不应有空行或空格或 xml 元素。
标签: java string servlets file-io io