导入本地Excel文件到数据库成功,但是导入远程客户机上的Excel文件提示“java.io.FileNotFoundException: C:
.txt”(系统找不到指定的文件。)” 导入本地Excel文件到数据库成功,但是导入远程客户机上的Excel文件提示“java.io.
导入本地Excel文件到数据库成功,但是导入远程客户机上的Excel文件提示“java.io.FileNotFoundException: C:\1.txt”(系统找不到指定的文件。)”
com.jspsmart.upload.File importFile=myUpload.getFiles().getFile(0);
if(!importFile.isMissing()){
...
...
...
java.io.File file=new java.io.File(importFile.getFilePathName());
/*错误出现在下面一行,提示“java.io.FileNotFoundException: C:\1.txt”(系统找不到指定的文件。)*/
POIFSFileSystem poiFS=new POIFSFileSystem(new FileInputStream(file));
wk=new HSSFWorkbook(poiFS);
sheetCount=wk.getNumberOfSheets();
...
...
...
}
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(servletconfig, request, response);
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(myFile.
m_parent.m_binArray, myFile.m_startData, myFile.m_size));
jxl.Workbook wk = Workbook.getWorkbook(is);
………………………………
com.jspsmart.upload.File importFile=myUpload.getFiles().getFile(0);
if(!importFile.isMissing()){
...
...
...
java.io.File file=new java.io.File(importFile.getFilePathName());
/*错误出现在下面一行,提示“java.io.FileNotFoundException: C:\1.txt”(系统找不到指定的文件。)*/
POIFSFileSystem poiFS=new POIFSFileSystem(new FileInputStream(file));
wk=new HSSFWorkbook(poiFS);
sheetCount=wk.getNumberOfSheets();
...
...
...
}
8 个解决方案
#1
关注
#2
up
#3
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(servletconfig, request, response);
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(myFile.
m_parent.m_binArray, myFile.m_startData, myFile.m_size));
jxl.Workbook wk = Workbook.getWorkbook(is);
………………………………
#4
请问楼上的能不能写得完整一些呀?
另外,m_parent.m_binArray、m_startData,m_size是从哪里来的呀?好像com.jspsmart.upload.File
这个类没这些属性呀?
我指的是有poi而不是jexcel
另外,m_parent.m_binArray、m_startData,m_size是从哪里来的呀?好像com.jspsmart.upload.File
这个类没这些属性呀?
我指的是有poi而不是jexcel
#5
先传到server上,在读取。
#6
SmartUpload mySmartUpload=new SmartUpload();
String realpath="";
realpath = "";
int count;
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(Integer.MAX_VALUE);
mySmartUpload.upload();
try {
count = mySmartUpload.save(realpath);
} catch (Exception e) {
out.println(e.toString());
}
----------------------------------------
mySmartUpload.upload(); 处报错
其引用的类中方法:
public void upload()
throws SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();
String fileName = new String();
String fileExt = new String();
String filePathName = new String();
String contentType = new String();
String contentDisp = new String();
String typeMIME = new String();
String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
System.out.println("m_totalBytes="+m_totalBytes);
for(; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
throw new SmartUploadException("Unable to upload.");
}
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
System.out.println(dataHeader);
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if(isFile)
{
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if(isFile && fileName.length() > 0)
{
if(m_deniedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
totalFileSize += (m_endData - m_startData) + 1;
if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if(isFile)
{
wujingsys.xls.upload.File newFile = new wujingsys.xls.upload.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
newFile.setFileName(fileName);
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if(contentType.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
newFile.setSize((m_endData - m_startData) + 1);
newFile.setStartData(m_startData);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);//此行报错!!!!!!!
m_formRequest.putParameter(fieldName, value);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while(true);
}
哪位高手用过啊?
String realpath="";
realpath = "";
int count;
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(Integer.MAX_VALUE);
mySmartUpload.upload();
try {
count = mySmartUpload.save(realpath);
} catch (Exception e) {
out.println(e.toString());
}
----------------------------------------
mySmartUpload.upload(); 处报错
其引用的类中方法:
public void upload()
throws SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();
String fileName = new String();
String fileExt = new String();
String filePathName = new String();
String contentType = new String();
String contentDisp = new String();
String typeMIME = new String();
String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
System.out.println("m_totalBytes="+m_totalBytes);
for(; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
throw new SmartUploadException("Unable to upload.");
}
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
System.out.println(dataHeader);
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if(isFile)
{
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if(isFile && fileName.length() > 0)
{
if(m_deniedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
totalFileSize += (m_endData - m_startData) + 1;
if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if(isFile)
{
wujingsys.xls.upload.File newFile = new wujingsys.xls.upload.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
newFile.setFileName(fileName);
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if(contentType.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
newFile.setSize((m_endData - m_startData) + 1);
newFile.setStartData(m_startData);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);//此行报错!!!!!!!
m_formRequest.putParameter(fieldName, value);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while(true);
}
哪位高手用过啊?
#7
我试过,分两步走,先上传到服务器,再读取
上传:
try
{
publicSmartUpload.initialize(pageContext);
//publicSmartUpload.service(request,response);
publicSmartUpload.setAllowedFilesList("xls,XLS");
publicSmartUpload.setDeniedFilesList("bat,exe,");
//publicSmartUpload.setMaxFileSize(-1);
//publicSmartUpload.setTotalMaxFileSize(-1);
publicSmartUpload.upload();
//String fn=publicSmartUpload.getFiles().getFile(0).getFileName();
out.println("您的文件已经成功上传到服务器!<a href='/go.html?url=success.jsp'>请点这里进入下一步!</a><br>");
int count=publicSmartUpload.save("/upload/excel");
for(int i=0; i<publicSmartUpload.getFiles().getCount(); i++)
{
com.jspsmart.upload.File file=publicSmartUpload.getFiles().getFile(i);
out.println("<TABLE BORDER=1>");
out.print("<tr bgcolor=#cccccc><td>:文件名:</td></tr>" + "<tr><td>" +file.getFileName() + "</td></tr>");
out.print("<tr bgcolor=#cccccc><td>:文件长度:</td></tr>" + "<tr><td>" +file.getSize() + "</td></tr>");
out.println("</table><br>");
String name=file.getFileName();
int size=file.getSize();
//out.println(name);//测试读取数据是否正常
//out.println(ResourceType);//测试读取数据是否正常
//out.println(size);//测试读取数据是否正常
//out.println(ResourceName);//测试读取数据是否正常
//out.println(Introduce);//测试读取数据是否正常
}
}
catch(Exception e)
{
e.printStackTrace();
out.println("对不起,您上传的文件因发生错误而无法上传到服务器!<a href='/go.html?url=success_upload.jsp'>请点这里返回</a><br>");
}
%>
读取到数据库:
<%
String path= request.getParameter("path").trim();//Excel文件名
String dir = (String)request.getRealPath("/upload/excel/"+path);//获取你上传文件的绝对路径
out.println(dir);
InputStream is = new FileInputStream(dir);//写入到FileInputStream
jxl.Workbook wb = Workbook.getWorkbook(is); //得到工作薄
jxl.Sheet st = wb.getSheet(0);//得到工作薄中的第一个工作表
Collection col = new ArrayList();
for(int y=1;y<st.getRows();y++)
{
Score score = new Score();
// Collection a = new ArrayList();
for(int x=0;x<st.getColumns();x++)
{
Cell cell=st.getCell(x,y);//得到工作表的第一个单元格,即A1
String content=cell.getContents();//getContents()将Cell中的字符转为字符串
if(x==0){
score.setId(content);
//out.println("获取学号");
}
else
if(x==1){
score.setUsername(content);
//out.println("获取姓名");
}
else
if(x==2){
score.setCourseName(content);
//out.println("获取密码");
}
else
if(x==3){
score.setScore(Integer.parseInt(content));
// out.println("获取分数");
}else
if(x==4){
score.setSemester(Integer.parseInt(content));
// out.println("获取学期");
}
}
col.add(score);
//out.println("添加到新对象!");
}
try{
insert.InsertScores(col);//写数据进数据库
out.print("<center>写进数据成功!5秒钟后自动返回!<center>");
}catch(Exception e){
e.printStackTrace();
}
wb.close();//关闭工作薄
is.close();//关闭输入流
%>
上传:
try
{
publicSmartUpload.initialize(pageContext);
//publicSmartUpload.service(request,response);
publicSmartUpload.setAllowedFilesList("xls,XLS");
publicSmartUpload.setDeniedFilesList("bat,exe,");
//publicSmartUpload.setMaxFileSize(-1);
//publicSmartUpload.setTotalMaxFileSize(-1);
publicSmartUpload.upload();
//String fn=publicSmartUpload.getFiles().getFile(0).getFileName();
out.println("您的文件已经成功上传到服务器!<a href='/go.html?url=success.jsp'>请点这里进入下一步!</a><br>");
int count=publicSmartUpload.save("/upload/excel");
for(int i=0; i<publicSmartUpload.getFiles().getCount(); i++)
{
com.jspsmart.upload.File file=publicSmartUpload.getFiles().getFile(i);
out.println("<TABLE BORDER=1>");
out.print("<tr bgcolor=#cccccc><td>:文件名:</td></tr>" + "<tr><td>" +file.getFileName() + "</td></tr>");
out.print("<tr bgcolor=#cccccc><td>:文件长度:</td></tr>" + "<tr><td>" +file.getSize() + "</td></tr>");
out.println("</table><br>");
String name=file.getFileName();
int size=file.getSize();
//out.println(name);//测试读取数据是否正常
//out.println(ResourceType);//测试读取数据是否正常
//out.println(size);//测试读取数据是否正常
//out.println(ResourceName);//测试读取数据是否正常
//out.println(Introduce);//测试读取数据是否正常
}
}
catch(Exception e)
{
e.printStackTrace();
out.println("对不起,您上传的文件因发生错误而无法上传到服务器!<a href='/go.html?url=success_upload.jsp'>请点这里返回</a><br>");
}
%>
读取到数据库:
<%
String path= request.getParameter("path").trim();//Excel文件名
String dir = (String)request.getRealPath("/upload/excel/"+path);//获取你上传文件的绝对路径
out.println(dir);
InputStream is = new FileInputStream(dir);//写入到FileInputStream
jxl.Workbook wb = Workbook.getWorkbook(is); //得到工作薄
jxl.Sheet st = wb.getSheet(0);//得到工作薄中的第一个工作表
Collection col = new ArrayList();
for(int y=1;y<st.getRows();y++)
{
Score score = new Score();
// Collection a = new ArrayList();
for(int x=0;x<st.getColumns();x++)
{
Cell cell=st.getCell(x,y);//得到工作表的第一个单元格,即A1
String content=cell.getContents();//getContents()将Cell中的字符转为字符串
if(x==0){
score.setId(content);
//out.println("获取学号");
}
else
if(x==1){
score.setUsername(content);
//out.println("获取姓名");
}
else
if(x==2){
score.setCourseName(content);
//out.println("获取密码");
}
else
if(x==3){
score.setScore(Integer.parseInt(content));
// out.println("获取分数");
}else
if(x==4){
score.setSemester(Integer.parseInt(content));
// out.println("获取学期");
}
}
col.add(score);
//out.println("添加到新对象!");
}
try{
insert.InsertScores(col);//写数据进数据库
out.print("<center>写进数据成功!5秒钟后自动返回!<center>");
}catch(Exception e){
e.printStackTrace();
}
wb.close();//关闭工作薄
is.close();//关闭输入流
%>
#8
直接获取文件流到poi的HSSFWorkbook
不必通过先获取File对象和path
HSSFWorkbook workbook = new HSSFWorkbook(InputStream input)
不必通过先获取File对象和path
HSSFWorkbook workbook = new HSSFWorkbook(InputStream input)
.txt”(系统找不到指定的文件。)” 导入本地Excel文件到数据库成功,但是导入远程客户机上的Excel文件提示“java.io.