java.io.FileNotFoundException:<;Excel文件名>;系统找不到指定的文件
我得到以下错误。我将把excel表格数据插入MYSQL数据库。当我上传Excel表格并按下submit按钮时,发生了以下异常java.io.FileNotFoundException:<;Excel文件名>;系统找不到指定的文件,java,jsp,Java,Jsp,我得到以下错误。我将把excel表格数据插入MYSQL数据库。当我上传Excel表格并按下submit按钮时,发生了以下异常 java.io.FileNotFoundException:studentsdetails1.xls(系统找不到指定的文件) 位于java.io.FileInputStream.open0(本机方法) 位于java.io.FileInputStream.open(未知源代码) 位于java.io.FileInputStream。(未知源代码) 这是我的密码。在本文中,我将
java.io.FileNotFoundException:studentsdetails1.xls(系统找不到指定的文件)
位于java.io.FileInputStream.open0(本机方法)
位于java.io.FileInputStream.open(未知源代码)
位于java.io.FileInputStream。(未知源代码)
这是我的密码。在本文中,我将把excel文件数据插入数据库
index.jsp-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action ="Excel.jsp" method="post" >
<input type="file" name= "excelfile" size="20" id="file" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
index.jsp-
在此处插入标题
Excel.jsp-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" `"http://www.w3.org/TR/html4/loose.dtd"> `
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import ="java.util.Date" %>
<%@ page import ="java.sql.*" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.io.FileNotFoundException" %>
<%@ page import ="java.io.IOException" %>
<%@ page import ="java.util.Iterator" %>
<%@ page import ="java.util.ArrayList" %>
<%@ page import ="javax.servlet.http.HttpServletRequest"%>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFCell" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFRow" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFSheet" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
<%@ page import ="akshay.Exceltest" %>
<%@ page import ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
<%!
Connection con;
PreparedStatement ps=null;
Statement stmt= null;
public static ArrayList readExcelFile(String fileName)
{
/** --Define a ArrayList
--Holds ArrayList Of Cells
*/
System.out.println(fileName);
ArrayList cellArrayLisstHolder = new ArrayList();
try{
/** Creating Input Stream**/
FileInputStream myInput = new FileInputStream(fileName);
/** Create a POIFSFileSystem object**/
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
/** Create a workbook using the File System**/
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
/** Get the first sheet from workbook**/
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
ArrayList cellStoreArrayList=new ArrayList();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreArrayList.add(myCell);
}
cellArrayLisstHolder.add(cellStoreArrayList);
}
}catch (Exception e){e.printStackTrace(); }
return cellArrayLisstHolder;
}%>
<%
String file = request.getParameter("excelfile");
String fileName=""+file+""; //testExcel.xls Excel File name
//Read an Excel File and Store in a ArrayList
ArrayList dataHolder=readExcelFile(fileName);
//Print the data read
//printCellDataToConsole(dataHolder);
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/<dtatabse>", "name", "password");
stmt =con.createStatement();
String query="insert into students12(id,name,age) values(?,?,?)";
ps=con.prepareStatement(query);
int count=0;
ArrayList cellStoreArrayList=null;
//For inserting into database
for(int i=1;i<dataHolder.size();i++) {
cellStoreArrayList=(ArrayList)dataHolder.get(i);
ps.setString(1,((HSSFCell)cellStoreArrayList.get(0)).toString());
ps.setString(2,((HSSFCell)cellStoreArrayList.get(1)).toString());
ps.setString(3,((HSSFCell)cellStoreArrayList.get(2)).toString());
count= ps.executeUpdate();
//out.print(((HSSFCell)cellStoreArrayList.get(2)).toString() + "\t");
}
//For checking data is inserted or not?
if(count>0)
{
%>
Following deatils from Excel file have been inserted in student table of database
<table>
<tr>
<th>Student's Name</th>
<th>Class</th>
<th>Age</th>
</tr>
<%
for (int j=1;j < dataHolder.size(); j++) {
cellStoreArrayList=(ArrayList)dataHolder.get(j);%>
<tr>
<td><%=((HSSFCell)cellStoreArrayList.get(0)).toString() %></td>
<td><%=((HSSFCell)cellStoreArrayList.get(1)).toString() %></td>
<td><%=((HSSFCell)cellStoreArrayList.get(2)).toString() %></td>
</tr>
<%}
}
else
{%>
<center> Details have not been inserted!!!!!!!!!</center>
<% }
}catch(Exception e)
{}%>
</table>
</body>
</html>
`
在此处插入标题
Excel文件中的以下数据已插入数据库的学生表中
学生姓名
班
年龄
详细信息尚未插入!!!!!!!!!
在表单中添加enctype=“多部分/表单数据”
<form action ="Excel.jsp" method="post" enctype="multipart/form-data" >
休息看起来不错。嗯。。。也许你可以解释一下,在错误消息“系统找不到指定的文件”中,你到底不明白什么?我已经创建了一个Excel工作表。我想将excel工作表数据插入数据库。但我犯了这个错误。当我调试代码时,我在下面一行中得到了相同的异常java.io.FileNotFoundException。FileInputStream myInput=新的FileInputStream(文件名);“系统找不到指定的文件”是否确实确定路径正确?我不这么认为。我必须把excel文件放在系统的什么地方?一些调试帮助:不要直接用文件名创建FileInputStream
,而是先创建一个file file=new file(fileName)
。然后,您可以通过一个简单的文件.getAbsolutePath()
找到Java希望该文件位于的位置。