File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Uploading File - Need to read as stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading File - Need to read as stream" Watch "Uploading File - Need to read as stream" New topic

Uploading File - Need to read as stream

Karthik Krishnamurthy
Ranch Hand

Joined: Feb 04, 2005
Posts: 118
I had posted a request regarding JExcel earlier. THis is a follow up, regarding the actual servlet.

Is there some way to incorporate File Upload feature itself, so that instead of saving the file to disk, we can read it as an InputStream.

Any help greatly appreciated

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I don't know about JExcel, perhaps your other thread answered that, but Apache Commons FileUpload does Have a Streaming API (follow this link).

Karthik Krishnamurthy
Ranch Hand

Joined: Feb 04, 2005
Posts: 118
Thanks for the reply.
I used the api and tried it out. But there is some issue that I am unable to figure out.
Am attaching my code here. Please help.
Only the sentence "In SERVICE" is getting printed.
Thank You,

<form enctype="multipart/form-data">
<input type="file" name="fileImport.xls"/>
<a href = "./FileUpload/ImportExcel">Import Workbook</a>

public void doService(HttpServletRequest request, HttpServletResponse response){
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
ServletFileUpload upload = new ServletFileUpload();
System.out.println("In SERVICE");
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item =;
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
// Process the input stream

}catch(IOException ioe){

}catch(FileUploadException fue){

}catch (ValidationException ve){


public void doGet(HttpServletRequest request, HttpServletResponse response){

public void doPost(HttpServletRequest request, HttpServletResponse response){


<load-on-startup> 1 </load-on-startup>


Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .

[Asking smart questions] [About Bear] [Books by Bear]
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Try logging your exceptions, seeing which one is being thrown, and work from there. At very least wrap the exceptions in a ServletException and re-throw it so the container can handle the reporting for you.

Right now it looks like you catch and eat your exceptions without ever reporting them. This is bad for the very reason you are seeing - something goes wrong and it is impossible to tell.
Vinod Yadav

Joined: Sep 18, 2006
Posts: 2
It's working fine at my end too..

I think you miss something, could you please post the Exception that you get, it will help us to find the root cause.
I agree. Here's the link:
subject: Uploading File - Need to read as stream
It's not a secret anymore!