Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

readLine() method return -1

 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as your form type is multipart/form-data. you will get null, if use request.getParameter(String str).
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in the MultipartRequest and associated classes available in the O'Reilly Servlets package.

Jules
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic