File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes readLine() method return -1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "readLine() method return -1" Watch "readLine() method return -1" New topic
Author

readLine() method return -1

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hi Everybody,

I am submitting a form in jsp file to a servlet :-

Partial jsp file contents are :-

<form name="searchexcel" method="POST" ENCTYPE="multipart/form-data">
<input type=file name="filename" >
</form>

partial code in servlet as follows :-

ServletInputStream sis = request.getInputStream();
byte[] b = new byte[1024];
int x=0;
while((x=sis.readLine(b,0,1024))>-1) {

*****
}


readline method is returning -1 at first attempt itself. It will be a great help if Can any one inform what could be the possible reasons for it.

Regards,
Neeraj.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
With servlet response you have a choice - you can either read the body of the request with request.getInputStream() -OR- you can use the response getParameter method to get form parameters. This is because the servlet engine has to read the request body input stream in order to find and parse the form parameters for you. The return of -1 indicates that the InputStream is already at the end of data.
The JavaDocs for ServletRequest.getParameter() discusses this.
Bill
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
as your form type is multipart/form-data. you will get null, if use request.getParameter(String str).
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
You might be interested in the MultipartRequest and associated classes available in the O'Reilly Servlets package.

Jules
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: readLine() method return -1