This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes Problem with Jakarta Commons Fileupload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Problem with Jakarta Commons Fileupload" Watch "Problem with Jakarta Commons Fileupload" New topic
Author

Problem with Jakarta Commons Fileupload

sabbir kazi
Ranch Hand

Joined: Feb 03, 2001
Posts: 62
Hi,

I am getting a NullPointerException when trying to execute:

List fileList = fileUpload.parseRequest(request); in my Servlet.

Please help me find out why I am getting this exception.

Thanks.

Sabbir

Here is the full code:

public void doPost( HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// JDBC Connection
if (conn == null){
prepareConnection();
try {
stmt = conn.prepareStatement(
"INSERT into Template (customer_id, template_name, template_size,"+
"template) VALUES"+
"(123456,?,?,?)");

// parse the incoming request
ServletFileUpload fileUpload = new ServletFileUpload();


try {
System.out.println("Calling ServletFileUpload's parseRequest method...");
List fileList = fileUpload.parseRequest(request);
System.out.println("Successful");
Iterator fileItemItr = fileList.iterator();
while (fileItemItr.hasNext()){
if (!fileItem.isFormField()){
fileItem = (FileItem) fileItemItr.next();
fileName = fileItem.getName();
fileInputStream = fileItem.getInputStream();
fileSize = fileItem.getSize();
} else {
// must be other fields
}
// writing to database
stmt.setString(1, fileName);
stmt.setLong(2, fileSize);
stmt.setBinaryStream(3, fileInputStream, 10);
stmt.execute();
}
} catch (FileUploadException e1) {
// TODO Auto-generated catch block
System.out.println("Un-Successful "+ e1.getMessage());
e1.printStackTrace();
}

releaseConnection();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It would help to see the stack trace instead of just the class of the exception that's being thrown.
sabbir kazi
Ranch Hand

Joined: Feb 03, 2001
Posts: 62
Hi Paul,

Thanks for your reply. Here is the exception stack trace:

Angel J Gama
Ranch Hand

Joined: Jun 28, 2007
Posts: 36
Hi,
I think you need to create a FileItemFactory and use it to create the ServletFileUpload, and you have to read all elements even when the element is/isn't a form field... Here's you code modified:

// parse the incoming request
FileItemFactory factory = new DiskFileItemFactory(); //MODIFIED
ServletFileUpload fileUpload = new ServletFileUpload(factory); //MODIFIED
try {
System.out.println("Calling ServletFileUpload's parseRequest method...");
List fileList = fileUpload.parseRequest(request);
System.out.println("Successful");
Iterator fileItemItr = fileList.iterator();
while (fileItemItr.hasNext()){
fileItem = (FileItem) fileItemItr.next(); //MODIFIED (used to be within if, you have to read the element even when it's not a form field element, then you ask if it is a formfield element)
if (!fileItem.isFormField()){
fileName = fileItem.getName();
fileInputStream = fileItem.getInputStream();
fileSize = fileItem.getSize();
} else {
// must be other fields
}
}

Hope it helps.
 
wood burning stoves
 
subject: Problem with Jakarta Commons Fileupload
 
Similar Threads
problem reading output of a subprocess
File upload
Getting Form value when file uploading by one Form
Uploading file with special characters in name
Can I get stream for uploaded file in struts 2?