I am receving the following error:
java.lang.NoSuchMethodError: org/apache/commons/fileupload/servlet/ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;
The below is my code.
InputStream inputstream = null;
String gridDataStr ="";
String errorMsg = "";
DTObject resultDTO = new DTObject();
FileItem fileitem = null;
//PrintWriter to send the JSON response back
PrintWriter out = response.getWriter();
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Sets the size threshold beyond which files are written directly to
// disk.
factory.setSizeThreshold(MAX_MEMORY_SIZE);
// Sets the directory used to temporarily store files that are larger
// than the configured size threshold. We use temporary directory for
// java
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
// constructs the folder where uploaded file will be stored
String uploadFolder = getServletContext().getRealPath("")
+ File.separator + destinationDir;
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(MAX_REQUEST_SIZE);
try{
// Parse the request
List items = upload.parseRequest(request); //error occurs here
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadFolder + File.separator + fileName;
File uploadedFile = new File(filePath);
System.out.println(filePath);
// saves the file to upload directory
item.write(uploadedFile);
}
}
// displays done.jsp page after upload finished
getServletContext().getRequestDispatcher("/blahblah.jsp").forward(
request, response);
}catch (Exception e){
System.out.println("Exception occured "+e);
e.printStackTrace ();
errorMsg="Error uploading file";
}
System.out.println(">> In ExcelUpload::doPost()::Exit # errorMsg is - "+errorMsg);
if(errorMsg!=null && errorMsg!=""){
request.setAttribute("errorMsg", errorMsg);
}
I am using commons.fileupload1.2.2.jar
. Previously I was using common.fileupload.1.2.1.jar
and there was the same error, so I changed the version still the error persists. Can anyone please spot the error?