• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Uploading File - Need to read as stream

 
Karthik Krishnamurthy
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic