对网上一段流行的上载疑惑

03-07-30 KillerMan

问题再这段:

ServletInputStream in = request.getInputStream();

		byte[] line = new byte[128];
		int i = in.readLine(line, 0, 128);
		if (i < 3)
			return;
		int boundaryLength = i - 2;

		String boundary = new String(line, 0, boundaryLength); //-2丢弃换行字符
		fields = new Hashtable();

		while (i != -1) {
<p>

一个包含文件域的request的流的内容如下:

-----------------------------7d31086b0762

Content-Disposition: form-data; name="f1"

1

-----------------------------7d31086b0762

Content-Disposition: form-data; name="f2"

2

-----------------------------7d31086b0762

Content-Disposition: form-data; name="f3"

3

-----------------------------7d31086b0762

Content-Disposition: form-data; name="f4"

4

-----------------------------7d31086b0762

Content-Disposition: form-data; name="f4"

5

-----------------------------7d31086b0762

Content-Disposition: form-data; name="file1"; filename=""

Content-Type: application/octet-stream

-----------------------------7d31086b0762

Content-Disposition: form-data; name="file2"; filename=""

Content-Type: application/octet-stream

-----------------------------7d31086b0762--

不明白他的程序还能执行,

int i = in.readLine(line, 0, 128); i的结果肯定是个-1

那么下边就直接return了,后边的代码根本执行不到啊。

他的这个例子根本就调不同,但这片文章网上还流传的很广,真是莫名其妙。

KillerMan
2003-07-30 15:46

这段代码作者说是再tomcat下调的,

我是在jrun4下边试的的。

包括smartUpload的upload方法中的代码

int i =0 ;

int j;

int conLength= request.getContentLength();

byte[] bt = new byte[conLength];

for(;i<conLength;i+=j){

request.getInputStream();

j = request.getInputStream().read(bt,i,conLength - i);

out.println(j+"aaaaaaa<br>");

break;

}

我这里读到的j总是-1,难道不同的服务器实现抽象类ServletInputStream时其read方法的返回值含义还不一样么?应该不会不遵守sun的标准吧?