Hi
David , Thanks for your response !!!
<code>
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
FileInputStream fstream;
File fileTowrite, fileToCreate;
String currenttime, filepath,filepath1;
DataAccess dao = new DataAccess();
totalCnt = 0;
successCnt = 0;
errorCnt = 0;
UploadForm myForm = (UploadForm)form;
UploadDataDB uploadData = new UploadDataDB();
ActionErrors errors = new ActionErrors();
ActionMessages am = new ActionMessages();
String contentType = myForm.getUploadContentType();
String fileName = myForm.getUploadFileName();
System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
try {
filepath = servlet.getServletContext().getRealPath("/")+ "upload";
// trying to copy the file using copyFile option but always I am getting the error here only please suggest me
fileToCreate = new File(filepath,myForm.getUploadFileName());
FileUtils.copyFile(myForm.getUplaod(),fileToCreate);
fstream = new FileInputStream(fileToCreate);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
stringAppend = new StringBuilder();
StringBuilder stringAppend1 = new StringBuilder();
stringAppend1.append("\n***********************************************************************\n\n");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
currenttime = dateFormat.format(date);
stringAppend1 = stringAppend1.append(currenttime + " Skipped Lines are : " + "\n\n");
logger.debug("------Before getting the connection from database-----");
logger.debug("------After getting the connection from database----");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileToCreate));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
int rows; // No of rows
rows = sheet.getPhysicalNumberOfRows();
int cols = 0; // No of columns
int tmp = 0;
// This trick ensures that we get the data properly even if it doesn't start from first few rows
for (int i = 0; i < 10 || i < rows; i++) {
row = sheet.getRow(i);
if (row != null) {
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if (tmp > cols) {
cols = tmp;
}
System.out.println("ROWS>>>>>>>>"+tmp);
}
}
errorCnt = uploadData.setColumns(rows, cols, sheet);
stringAppend1 = stringAppend1.append(stringAppend);
if (totalCnt != successCnt) {
fileTowrite = new File(filepath, "uploadfile.log");
FileWriter logfile = new FileWriter(fileTowrite, true);
Writer wr = new BufferedWriter(logfile);
wr.write(stringAppend1 + "\n");
wr.close();
}
if (totalCnt == successCnt) {
am.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("Uploaded Successfully"));
//addActionMessage("Uploaded Successfully");
} else if (errorCnt == totalCnt) {
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("Unable to upload the file because of invalid data.<br/>View the <a target='_new' href='../file?fileName=uploadfile'>log file</a> for more details."));
} else {
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("Some of the records in the file are not uploaded because of invalid data. <br/>View the <a target='_new' href='../file?fileName=uploadfile'>log file</a> for more details."));
}
return mapping.findForward("success");
} catch (Exception e) {
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("Please upload file with .xls extension only."));
logger.error("------Exception---" + e.getMessage());
e.printStackTrace();
//return upload();
return mapping.findForward("failure");
} finally {
try {
if (dao != null) {
dao.releaseResources();
}
} catch (Exception e) {
logger.error("------Exception in finally---" + e.getMessage());
return mapping.findForward("failure");
}
}
}
</code>
Hi david I putting my code snippet that would be helpful to you.
Thanks