Forums Register Login

Problem while uploading xls file through Servlet program.

+Pie Number of slices to send: Send
Salaam/Hi,

We are having following problem:
We have made one jsp page for taking input from user for xls file.(multipart/form-data enabled).
Once user upload the file and click submit,we call our servlet to do the needful.Upon reaching the following statement, it got hanged.
//We have used apache's common file upload jar files.
List items = upload.parseRequest(request);//where upload is object of DiskFileUpload.

No error message displayed on tomcat window as well as in our log file.

Please do suggest the needful action.

Thank you.

Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
Can you post the code from the servlet that processes the upload?
+Pie Number of slices to send: Send
We are calling following method after user clicks on Upload button:
--------------------------------------------------------
public String uploadxlsFile(HttpServletRequest request,HttpServletResponse response) throws Exception {
.....
.....
try {
boolean isMultipart = FileUpload.isMultipartContent(request);
// Create a new file upload handler
logger.info("isMultipart ::" + isMultipart);//this is true
DiskFileUpload upload = new DiskFileUpload();
logger.info("test1");
FileItemFactory factory = new DefaultFileItemFactory();
//ServletFileUpload upload = new ServletFileUpload(factory);
logger.info("test2");
// Parse the request
List items=null;
logger.info("upload object:"+upload);
//upload.setSizeMax(20000000);
try{
logger.info("test...");
/* FileItem */ items = upload.parseRequest(request);//this line is causing problem.
}catch(Exception e){logger.error("e:"+e);e.printStackTrace(); }
logger.info("test3");
Iterator iter = items.iterator();
while (iter.hasNext()) {
....
....

--------------------------------------------------------
We are getting the output line till test... in the log file.
After that the jsp page got hanged with no data.
No exception thrown and shown in the log and tomcat window.

Unable to understand the root cause.
common upload jar is set in the classpath and is available in lib folder.

Thank you.

Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
What does the client side see while the server is stuck at that statement?

If this was my problem I would be doing a "view source" on the HTML generated by your JSP. Perhaps there is something wrong with the submit side of this problem.

Bill
+Pie Number of slices to send: Send
Salaam/Hi,

The client is not seeing the confirmation message.
Instead it is showing blank(with only top menu).

Wondering why this is happening ?

FYI: We are uploading the xls file with complex data(mult-sheets) and also we are sending some query string data on submit of the page:

action="../servlet/ServletName?hidReq=XYZ2000&hidOpr=ABC&cmbpqr=1" method="post" enctype="MULTIPART/FORM-DATA"



Thank you.

Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
Salaam/Hi,

Is there any additional libraries required apart from:
commons-fileupload-1.2.jar for above mentioned problem.

One thing we found after clicking Upload button, it is jumping from that line(i.e items = upload.parseRequest(request)) to directly finally block (without sending any errors/exception).

Don't know why this is happening ?

We are still in stuck.



Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
 

action="../servlet/ServletName?hidReq=XYZ2000&hidOpr=ABC&cmbpqr=1" method="post" enctype="MULTIPART/FORM-DATA"



So you are both POSTing data and including parameters in the URL in the GET style!

I have never tried this - I wonder if your parameters "hidOpr" and "cmbpqr" are correctly getting extracted, have you verified this?

Bill
+Pie Number of slices to send: Send
Yes, verified.


We are still in stuck.


Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
I think I know what this is.

With servlets, you can either call getInputStream and parse everything yourself (which is what Apache's fileupload does) OR call one of the methods that causes the request to parse the input stream for you.
These methods are getParameter, getParameterMap, getParameterNames, and getParameterValues.

You can not do both with the same request.
Once you call one of the methods listed above, the input stream gets parsed and is no longer available for your upload library.

Instead of passing those values in the querystring, try making them regular form values and retrieve them with the methods provided by the fileupload library. Remove all calls to getParameter (or the others listed above) from your servlet.
+Pie Number of slices to send: Send
 


Originally posted by Ben:
Instead of passing those values in the querystring, try making them regular form values and retrieve them with the methods provided by the fileupload library. Remove all calls to getParameter


Ben,
Stil not working.


Some further clarification to be explained in this context:
We want to upload excel file using common upload utility and to read it using POIs api.
Flow:
=>Jsp to take input from user (through file type control)
=>Controller(Servlet) which will forward the request to one of our Model file(Java class file)
=>Java class file(Model) is using apaches common upload api to upload the file.
The code snippet is below:
try{
boolean isMultipart = FileUpload.isMultipartContent(request);
// isMultipart -> true
DiskFileUpload upload = new DiskFileUpload();
List /* FileItem */ items = upload.parseRequest(request);
...
Iterator iter = items.iterator();
...
catch(Exception e){...}
finally{
...
}


Now the problem is when we submit the request, it is reaching till this line :List /* FileItem */ items = upload.parseRequest(request);

After that it moved directly to finally block (without complaning any exception and without going to catch section.
And in the finally block we are returning to confirmation page where it is displaying following message:
java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key


Hope the problem is clear to all.
Please do let us know the action to be taken.


Originally posted by Me:
Is there any additional libraries required apart from:
commons-fileupload-1.2.jar for above mentioned problem



Revert awaited.



Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
Salaam/Hi,

Revert still awaited.

/*
while(true){
..
}
*/


Thank you.

Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
I think that Ben got it right - something in your code is causing the request input stream to be read before it gets to the parsing code.

I see:


What provision is made for handling the false condition? Can that be the way the finally statement is reached?

Bill
+Pie Number of slices to send: Send
Salaam/Hi,

Good news:
Finally we reached to the solution.


Originally posted by Me:
Is there any additional libraries required apart from:
commons-fileupload-1.2.jar for above mentioned problem.



The answer is yes. i.e commons-io-[1.4].jar


Originally posted by Ben:
With servlets, you can either call getInputStream and parse everything yourself (which is what Apache's fileupload does) OR call one of the methods that causes the request to parse the input stream for you.
These methods are getParameter, getParameterMap, getParameterNames, and getParameterValues.
You can not do both with the same request.



But in our case,when we forward the request to other java class,it works with same request(both getParameter & Apache's upload mechanism)
May be it won't work in the same servlet class.

Thank you to Ben & William for all help and info.

-----------------------------------------------
!Once again another Exception!
-----------------------------------------------
After going ahead of upload.parseRequest(request) line of statement.
It throws following exception:
org.apache.poi.hssf.record.RecordFormatException: Unable to construct record instance, the following exception occured: null

& In tomcat's catalina window:
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedConstructorAccessor28.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFactory.java:224)
at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:160)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:163)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>HSSFWorkbook.java:1


NB:The excel file is having four sheets with tabular formatted data.



Thank you.

Regards
Baseet Ahmed
******************
Say Salaam before starting communication(verbal+nonverbal),even before Hi/Hello.
Salaam : Assalam Alekum (Peace may upon you.)
******************
+Pie Number of slices to send: Send
Baseet,
This new question would probably do better in a thread of its own.
Most people will see a long thread like this and assume that you're already getting all the help that you need.

Ask in the "Other Open Source Projects" forum as this seems to be a POI issue and not a servlets issue.

Good-luck with your question.
-Ben
+Pie Number of slices to send: Send
Hi. i'm to portlet.
i'm working on a portlet to upload file. i got the sample code to to tat.i'm having the same problem as well.
it has problem when the code reach the parserequest line.

here is my code:

import javax.portlet.*;
import org.apache.commons.io.*;
import org.apache.commons.fileupload.portlet.*;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileItem;
import java.io.IOException;
import java.io.Writer;
import java.io.File;
import java.util.*;

public void processAction(ActionRequest request, ActionResponse response) throws PortletException,
IOException, UnavailableException
{
//System.out.println(request);
//System.out.println(request.getContentType());
// Check the request content type to see if it starts with multipart/

if (!PortletFileUpload.isMultipartContent(request))
{
//set an error message
response.setRenderParameter("error", ERROR_NO_FILE);
return;
}

PortletFileUpload dfu = new PortletFileUpload();
//maximum allowed file upload size (10 MB)
dfu.setSizeMax(10 * 1000 * 1000);

//maximum size in memory (vs disk) (100 KB)
//dfu.setSizeThreshold(100 * 1000);

try
{
//get the FileItems

//System.out.println("Our request Object" + request.toString()) ;
List fileItems = dfu.parseRequest(request); <-- the program throw exception when reach this line
System.out.println("xxxxxxxx");
Iterator iter = fileItems.iterator();

while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
//pass along to render request
String fieldName = item.getFieldName();
String value = item.getString();
response.setRenderParameter(fieldName, value);

}
else
{

//write the uploaded file to a new location
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
long size = item.getSize();
response.setRenderParameter("size", Long.toString(size));
response.setRenderParameter("contentType", contentType);
String tempDir = "D:\\"; //System.getProperty("java.io.tmpdir");
String serverFileName = fieldName + "-portlet.tmp";
File serverFile = new File(tempDir, serverFileName);
item.write(serverFile);
response.setRenderParameter("serverFileName",
serverFileName);
getPortletContext().log(
"serverFileName : " + tempDir + "/" + serverFileName);
}
}
}
catch (FileUploadException fue)
{
String msg = "File Upload Exception: " + fue.getMessage();
System.out.println(msg);
response.setRenderParameter("error", msg);
getPortletContext().log(msg, fue);
}
catch (Exception e)
{
String msg = "Exception: " + e.getMessage();
System.out.println(msg);
response.setRenderParameter("error", msg);
getPortletContext().log(msg, e);
}
}

i read the previous post saying that it may caused by the common.io library.
so i tried importing the common.io.jar but it seems no effect.. is there anything i miss out??

thanks
+Pie Number of slices to send: Send
Please paste the exact error message/stack trace, which you are getting at parserequest() line.




Regards
Baseet Ahmed
##############
Value of Parents
Jannat(Heaven) is under the feet of Mother.
Father is the middle gate of Jannat(Heaven).
[ ISLAM ]
##############
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4873 times.
Similar Threads
String cases:Lower,upper etc
sendRedirect fails to post data along with url if the data is too large
How about a Java coding competition in Java Ranch?
setting the value to a form of html
Compiling servlet [was: HTTP Status 404 - Servlet Login is not available]
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:49:35.