如何处理823237个字符的字符串 [英] How to process a string with 823237 characters

查看:785
本文介绍了如何处理823237个字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含823237个字符。它实际上是一个xml文件,出于测试目的,我希望作为servlet的响应返回。

I have a string that has 823237 characters in it. its actually an xml file and for testing purpose I want to return as a response form a servlet.

我已经尝试了所有我能想到的东西

I have tried everything I can possible think of

1)用整个字符串创建一个常量...在这种情况下Eclipse抱怨(在servlet类名下有一个红线) -

1) creating a constant with the whole string... in this case Eclipse complains (with a red line under servlet class name) -

 The type generates a string that requires more than 65535 bytes to encode in Utf8 format in the constant pool

2)将整个字符串分成20个字符串常量并直接写入 out 对象
类似于:

2) breaking the whole string into 20 string constants and writing to the out object directly something like :

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); 

在这种情况下......构建失败..抱怨..

in this case ... the build fails .. complaining..

   [javac] D:\xx\xxx\xxx.java:87: constant string too long
   [javac]      CONSTANT_STRING_PART_19 + CONSTANT_STRING_PART_20); 
                                                    ^

3)将xml文件作为字符串读取并写入 out object ..在这种情况下我得到

3) reading the xml file as a string and writing to out object .. in this case I get

SEVERE: Allocate exception for servlet MyServlet
Caused by: org.apache.xmlbeans.XmlException: error: Content is not allowed in prolog.

最后我的问题是......我怎么能返回这么大的字符串(作为回复) )来自 servlet ???

Finally my question is ... how can I return such a big string (as response) from the servlet ???

推荐答案

第二种方法可能有以下方式:

The second approach might work the following way:

out.print(CONSTANT_STRING_PART_1);
out.print(CONSTANT_STRING_PART_2);
out.print(CONSTANT_STRING_PART_3);
out.print(CONSTANT_STRING_PART_4);
// ...
out.print(CONSTANT_STRING_PART_N);
out.println();

您可以在循环中执行此操作(强烈建议;))。

You can do this in a loop of course (which is highly recommended ;)).

你这样做,你只是临时创建大字符串,然后将其传递给 println(),这是与第一个问题相同。

The way you do it, you just temporarely create the large string again to then pass it to println(), which is the same problem as the first one.

这篇关于如何处理823237个字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆