我在index.jsp页面插入以下代码:
<FORM id="Form1" name="excel" action="ExcelToSQL.jsp" method="post">
<TABLE border="1">
<TR>
<TD>请选择你要导入的Excel表格:</TD>
<TD><input type="file" name="file"></TD>
<TD><input type="submit" value="确定"></TD>
</TR>
</TABLE>
</FORM>
在ExcelToSQL页面,通过以下代码:
String FilePath=new String(request.getParameter("file"));
InputStream is=new FileInputStream(FilePath);
rwb=Workbook.getWorkbook(is);
为何不能正确获得其路径名。我通过
InputStream is=new FileInputStream("D:/2007total.xls");可正常运行页面,请高手指点下~!
<FORM id="Form1" name="excel" action="ExcelToSQL.jsp" method="post">
<TABLE border="1">
<TR>
<TD>请选择你要导入的Excel表格:</TD>
<TD><input type="file" name="file"></TD>
<TD><input type="submit" value="确定"></TD>
</TR>
</TABLE>
</FORM>
在ExcelToSQL页面,通过以下代码:
String FilePath=new String(request.getParameter("file"));
InputStream is=new FileInputStream(FilePath);
rwb=Workbook.getWorkbook(is);
为何不能正确获得其路径名。我通过
InputStream is=new FileInputStream("D:/2007total.xls");可正常运行页面,请高手指点下~!
本文中约定:
文件路径(filePath)形如:C:\Windows\ecm.ini
文件目录(fileDir)形如:C:\Windows
文件名(fileName)形如:boot.ini在服务器端把二进制转换成文本,得到文件路径,如filePath = "D:\Games\ea.rar",那么如何得到文件名呢?以前常用如下代码:
pos = InStrRev(filePath, "\")
if pos > 0 then
fileName = Mid(filePath, pos+1)
end if初看无误,其实由于在Windows目录中"\"与"/"具有相同的意义,所以以上代码存在一定的安全隐患。客户端,假如要上传的文件位于D盘根目录下,文件名为index.asp。现在使用者上传时,把文件路径故意写成D:\abc\../index.asp,由于../表示上一级目录,那么它指向的实际位置就是D盘根目录下的index.asp文件,于是文件上传是成功的,可是它给服务器端报告的文件路径是D:\abc\../index.asp,根据以上代码,服务端程序在解析文件名时变成了"../index.asp",可是这并不是真正的文件名,并且若依此文件名来保存,便会保存到意想不到的目录,甚至覆盖你真正想要的文件。所以以上代码并不是良好的代码,使用以下方法能够正确获取文件名:
dim xgPos, fxgPos
xgPos = InStrRev(filename,"\")
fxgPos = InStrRev(filename,"/")
if xgPos > fxgPos then
fileName = Mid(filePath, xgPos + 1)
else
fileName = Mid(filePath, fxgPos + 1)
end if楼主参考下吧