permaculture playing cards*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading File - Need to read as stream" Watch "Uploading File - Need to read as stream" New topic
Author

Uploading File - Need to read as stream

Karthik Krishnamurthy
Ranch Hand

Joined: Feb 04, 2005
Posts: 118
Hi,
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

Thanks
Karthik
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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


Steve
Karthik Krishnamurthy
Ranch Hand

Joined: Feb 04, 2005
Posts: 118
Hi,
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,

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

Servlet:
public void doService(HttpServletRequest request, HttpServletResponse response){
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
ServletFileUpload upload = new ServletFileUpload();
System.out.println("In SERVICE");
// Parse the request
try{
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
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){
doService(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response){
doService(request,response);
}

web.xml:

<servlet>
<servlet-name>tacdFileUploadServlet</servlet-name>
<servlet-class>com.servlet.FileUploadServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring/applicationContext-excel-servlet.xml
</param-value>
</init-param>
<load-on-startup> 1 </load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>fileUploadServlet</servlet-name>
<url-pattern>/FileUpload/ImportExcel/*</url-pattern>
</servlet-mapping>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
Greenhorn

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Uploading File - Need to read as stream