错误Java.io.FileNotFoundException

时间:2014-05-08 06:38:05

标签: windows filenotfoundexception

在我的项目中,我阅读了excel文件。这个功能在本地主机上工作,但我在服务器上传这个项目,那时我得到如下错误

java.io.FileNotFoundException: D:/Java_Question.xls (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)

Controller.AdminExelSheetController.read(AdminExelSheetController.jav a:190) 为什么服务器上出现此错误?

阅读excel文件代码:这里需要进行哪些更改?

FileInputStream inputFile = new FileInputStream("D:/" + fileName);
POIFSFileSystem systemFile = new POIFSFileSystem(inputFile);
HSSFWorkbook wb = new HSSFWorkbook(systemFile);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
if (!"".equals(rows.toString())) {
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
Vector cellStore = new Vector();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
cellStore.addElement(cell);
}rowStore.addElement(cellStore);}

1 个答案:

答案 0 :(得分:0)

因为您的项目在服务器上有不同的路径。 你应该得到这样的文件路径......

String path = ServletContext.getRealPath("/FolderInYourProjectWhereExcelIsLocated");