Sunday, 8 September 2013

Struts2 excel fileupload using org.apache.commons.fileupload

Struts2 excel fileupload using org.apache.commons.fileupload

I am implementing a file upload for excel i.e xls type document, after
selecting the file when I submit upload.parseRequest returns empty list.
List<FileItem> items = (List<FileItem>)upload.parseRequest(request)
Below is the upload.jsp for review -
<%@page import
="java.io.*,java.util.*,org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.DiskFileItemFactory,org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<% try { %>
<body>
<%! public File uniqueFile(File f,String pa) {
if(pa.equals("files/Recovery") || pa.equals("images/employee")
|| pa.equals("images/family")) {
return f;
} else if (f.exists()) {
String filename = f.getName();
int appender=1;
try{
appender = Integer.parseInt(filename.substring(0,1));
appender++;
filename =
appender+filename.substring(1,filename.length());
} catch(Exception e) {
filename = appender+filename;
}
String path = f.getParentFile().getPath();
return uniqueFile(new File(path+"\\"+filename),pa);
}
return f;
}
%>
<% File fullFile = null;
FileItem item = null;
int FILE_SIZE = 2097152;
try {
boolean isMultipart =
ServletFileUpload.isMultipartContent(request);
if(isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new
ServletFileUpload(factory);
List<FileItem> items =
(List<FileItem>)upload.parseRequest(request);
out.println("Number of fields: " + items.size() +
"<br/><br/>");
Iterator<FileItem> itr = items.iterator();
String msg = "";
while(itr.hasNext()) {
try {
item = (FileItem) itr.next();
if(item.isFormField()) {
String fieldName = item.getFieldName();
if(fieldName.equals("name")) {
request.setAttribute("msg", "Thank
You: " + item.getString());
}
} else {
String path = request.getParameter("path");
String field = request.getParameter("field");
out.println("path >>"+path+ "<br/><br/>");
fullFile = new File(item.getName());
String imageName = fullFile.getName();
File f = new File(path);
f.mkdirs();
File savedFile = new File(path + "/",
imageName);
fullFile = uniqueFile(savedFile,path);
item.write(fullFile);
msg = "file uploaded successfully";
%> <script>
<% if(!imageName.equals("")){ %>
window.opener.document.getElementById('sysForm').<%=field%>.value="<%=fullFile.getName()%>";
window.opener.document.getElementById('sysForm').target="main";
window.opener.document.getElementById('sysForm').action.value='addFile';
<% } else { %>
alert("Please select the file");
<%}%>
</script>
<% }
} catch (Exception e) {
e.printStackTrace();
msg = "Problem occured while uploading
file.<br>Please try again";
}
}
}else{
out.println("not a multipart request");
}
} catch (Exception e) {
e.printStackTrace();
}
%>
<script>
//window.close();
</script>
<% } catch(Exception e) {
e.printStackTrace();
}
%>
</body>
The dataMigration.jsp is as below -
<body>
<center>
<form action="upload.jsp?path=<%=path%>&field=<%=field%>"
name="form" method="POST" enctype="multipart/form-data">
<b><font face=verdana size=2>Upload File:<br><br></font></b>
<input type="file" name="myFile" id="myFile"/><br><br>
<input type="submit" name="Submit" value="Submit"
onclick="return callCheck();"/>
</form>
</center>
</body>
Please find the debugging analysis as below -
In debug mode under variables I can see request variable has files in
multi - files - table
But the items variable is empty i/e modcount = 0 & size = 0
I tried many possible alternatives but the parseRequest method returns the
empty list
Kindly help me in this regards as I am struggling with this issue the
whole day :( I do not want to use s:file i.e struts fileupload feature
Thanks pshinde31

No comments:

Post a Comment