• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

readLine() method return -1

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as your form type is multipart/form-data. you will get null, if use request.getParameter(String str).
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might be interested in the MultipartRequest and associated classes available in the O'Reilly Servlets package.

Jules
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic