Reputation: 151
Scenario: I am trying to read an excel file using JExcel and dump the data into Oracle database. I ask the user to browse and select a file to read from their system and then I read the same and dump the data (without saving the physical file) into the Oracle database.
<form id='uploadform'>
<table class='detailTable'>
<tr>
<td class='detailTableTD'>Select File to Upload:</td>
<td class='detailTableTD'>
<input type='file' id='fileUpload'>
</td>
</tr>
<tr>
<td class='detailTableTD'></td>
<td class='detailTableTD'>
<button id='uploadBtn' onclick="upload();">Upload</button>
<button id='backBtn'>Back</button>
</td>
</tr>
</table>
</form>
Java:
public int[][] readFromExcel(final String excelFilePath) throws ApplicationException {
String excelPath = excelFilePath.trim();
File inputWorkbook = new File(excelPath);
Workbook workbook;
MyObj drawBk;
List drBkLst = new ArrayList();
String tmpStr = "";
try {
workbook = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = workbook.getSheet(0);
for (int j = 1; j < sheet.getRows(); j++) {
drawBk = new MyObj();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell = sheet.getCell(i, j);
switch (i) {
case 0:
drawBk.setSerialNumber(cell.getContents());
break;
case 1:
drawBk.setMediaNumber(cell.getContents());
break;
case 2:
tmpStr = cell.getContents();
if (!(tmpStr.trim().equalsIgnoreCase("Y") || tmpStr.trim().equalsIgnoreCase("N")))
continue;
else
drawBk.setMnProcessInd(tmpStr);
drawBk.setSnProcessInd(tmpStr);
break;
}
}
drawBk.setLastChangedUser(userId);
drBkLst.add(drawBk);
}
} catch (Exception readExcelEx) {}
return insertExcelDataToDatabase(drBkLst);
}
Here I capture the path and pass as a parameter to a Java method for processing the cells and create a ArrayList of Objects to be inserted into DB. All simple code here. It all works fine when run from a local deployment but when I deploy the EAR to server I start getting FileNotFound Exception when I browse for a file and select a file. I feel its all due to some relative/absolute path issue but can't possibly debug and I need it fast. Any precise help help will be appreciated?
Thanks.
Upvotes: 0
Views: 1363
Reputation: 3183
You will have to write the file on some temp directory on your server. You can not use your system file path from server (ear).
You will have to change your form to multipart and handle the request. Check out this link (http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html).
You can avoid writing the file to temp as well (it will be written in tmp by java anyways, but you won't have to provide the path). use
FileInputStream fis = new FileInputStream(new File()); //after reading it using file io.
Workbook workbook = WorkbookFactory.create(fis);
Upvotes: 1
Reputation: 1816
I believe when you run locally that file resides on your local system hence you don't get any exception. But when you deploy it on server the file browse by user is still on his machine and not reached till server. You need to create a temp file or read data in java objects from the request parameter binary data of file.
Upvotes: 1