某天,要做几个WebService,供第三方调用,一些是查询会返回多条数据,建议用分页,请求的时候带了条数,返回去的时候也带了条数,第三方不同意,让做最大条数限制,所以需要估算返回的最大条数。
1.读入XML计算一条数据所事的字节数据
public static void main(String args[]) {
String FileName = "d:/aa.xml";
File myFile = new File(FileName);
StringBuffer result = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new FileReader(myFile));
String str;
while ((str = in.readLine()) != null) {
result.append(str);
}
in.close();
} catch (IOException e) {
e.getStackTrace();
}
String re=result.toString();
re=re.replaceAll("\\s*", "");
byte[] byteS=re.getBytes();
System.out.println(byteS.length);
}
结果4500B,即5K,由于这个XML的数据用XSD自动生成的,现实的数据肯定比这个复杂,由于有几个字段是1024B的,所以每条数据最大算10K
2. 有经验的人建议,Webservice每次传递的数据不要超过1M,因为0.5M相对于一个大的网页,再大就不能保证即时响应了,和带宽都有关系的。所以最终确定最大限制为50条数据,因为50条*10K=500K=0.5M。