2010年1月6日 星期三

Connection reset in servlet when upload

OSGi的環境中,在實做HttpServlet的時候,因為板子配備很低,在接受upload大檔案時一直發生Connection reset的問題。利用Wireshark查看,發現預定送出的錯誤訊息有送出,但是client上傳似乎被強制中斷。

猜測與uploading強制中斷有關,後來利用了等待上傳數據完全傳輸完畢再進行其他動作,才避免了這樣的情形發生。

解法如下:

在doPost的方法中,假設HttpServletRequest變數為request。

接著在方法的一開始,就進行忽略上傳數據動作到結尾。

ServletInputStream input=request.getInputStream();
input.skip(request.getContentLength());
input.close();

這樣一來就能確保client端上傳完畢。或許有想過skip這行不要可以嗎?答案是不行的。好像任意停掉client端就會導致connection reset。那如果我不小心上傳了G級檔案豈不是要等很久嗎?網路上Krzysztof Osiecki網友說"

That's the way HTTP works. When sending data to server you are forming request, and after successfully recieving your data server responds.

The solution is to use flash or applet to upload files, that way you can force client side not to send too much data to server.

就認命吧!

參考資料:
[1] Large file upload does not stop after FileUploadLimitExceededException

沒有留言: