当前位置: 首页 > 知识库问答 >
问题:

Java . io . file not found尝试在数据库中上传excel文件时显示异常

杭志泽
2023-03-14

我正在做一个任务,我想在oracle数据库中上传excel数据,但当我尝试运行代码并上传. xls文件后,它给我的文件找不到异常D:\GODBFILES\NETBEANS PROJECTS\NetBeansProjects\TestWebApplication\build\web\null(系统找不到指定的文件),我担心的是从任何位置上传文件应该在db中,我在fileinputstream中更新了代码,我写了硬编码的路径和文件名,但我不希望这个文件路径和文件名应该动态获取并存储在文件中

用于上传我在jar下面使用的文件

commons-fileupload-1.3.jarcommons-io-2.4.jarpoi-3.9.jarcos.jarpoi-ooxml-3.9.xml

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form id="f1" enctype="multipart/form-data" action="xlsUpload_01.jsp" method="post">
            <table align="center" border="1">
                <tr>
                    <td>Enter File name</td>
                    <td><input type="text" name="id"></td>

                </tr>
                <tr>
                    <td>Select File</td>
                     <td><input type="file"  name="xlsfile" />
                </tr>

            </table>
            <p>
            <center>
                <input align="center" type="submit" value="Upload File" name="btnsubmit"/>
            </center>

            </p>
        </form>
    </body>
</html>

xlsupload_01.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
            pageEncoding="UTF-8"%>
         <%@page import="java.sql.*" %>
        <%@page import ="java.util.Date" %>  
        <%@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="org.apache.poi.hssf.usermodel.*" %>
        <%@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 ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
        <%@page import="org.apache.poi.ss.usermodel.Cell" %>
        <%@page import ="org.apache.poi.ss.usermodel.Row"%>
        <%@page import="org.apache.poi.ss.usermodel.Sheet" %>
        <%@page import="org.apache.poi.ss.usermodel.Workbook" %>
        <%@page import="com.oreilly.servlet.MultipartRequest" %>
        <%@page import="org.apache.poi.xssf.usermodel.*"%>
        <%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>

         <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
      <%

     ArrayList CellArrayListHolder=new ArrayList();
     Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1821:xe","se","Spacess");


 **FileInputStream file=new FileInputStream(new File("D:\\GODBFILES\\NETBEANS PROJECTS\\upload\\hello.xlsx"));
XSSFWorkbook workbook=new XSSFWorkbook(file);**

    Sheet firstSheet=workbook.getSheetAt(0);
    Iterator<Row> iterator=firstSheet.iterator();
    int count=0;
    while(iterator.hasNext())
    {
        XSSFRow nextrow=(XSSFRow)iterator.next();
        ArrayList rowarraylist=new ArrayList();
        Iterator<Cell> cellIterator=nextrow.cellIterator();

        while(cellIterator.hasNext())
        {
            XSSFCell cell=(XSSFCell)cellIterator.next();
            rowarraylist.add(cell);
        }
        CellArrayListHolder.add(rowarraylist);
    }
           out.println(CellArrayListHolder);
           ArrayList rowarraylist=null;
           PreparedStatement st=con.prepareStatement("insert into DYNAMIC_INSERT values(?)");

    for(int i=1;i<CellArrayListHolder.size();i++)
    {
        rowarraylist=(ArrayList)CellArrayListHolder.get(i);
        st.setString(1, rowarraylist.get(0).toString());
        //st.executeUpdate();
        count=st.executeUpdate();

    }
    if(count>0)
    {
           out.println("<script type=\"text/javascript\">");
       out.println("alert('File added');");
       out.println("</script>");    
    }




      %>
        </body>
    </html>

共有1个答案

钱劲
2023-03-14

在这里我得到了解决方案

 <%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.omg.PortableServer.Servant"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.nio.file.Paths"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
     <%@page import="java.sql.*" %>
    <%@page import ="java.util.Date" %>  
    <%@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="org.apache.poi.hssf.usermodel.*" %>
    <%@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 ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
    <%@page import="org.apache.poi.ss.usermodel.Cell" %>
    <%@page import ="org.apache.poi.ss.usermodel.Row"%>
    <%@page import="org.apache.poi.ss.usermodel.Sheet" %>
    <%@page import="org.apache.poi.ss.usermodel.Workbook" %>
    <%@page import="com.oreilly.servlet.MultipartRequest" %>
    <%@page import="org.apache.poi.xssf.usermodel.*"%>
    <%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
    <%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>


     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
  <%

    try
    {
 ArrayList CellArrayListHolder=new ArrayList();
 Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1871:godb","xe","se");
      String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
out.println(saveFile);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

%>
<%

PreparedStatement psmnt = null;
FileInputStream fis;
//try {
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//connection = DriverManager.getConnection(connectionURL, "root", "root");
File file1 = new File(saveFile);
FileInputStream file_inut=new FileInputStream(file1);
XSSFWorkbook workbook=new XSSFWorkbook(file_inut);
Sheet firstsheet=workbook.getSheetAt(0);
Iterator<Row> iterator=firstsheet.iterator();
int count=0;
while(iterator.hasNext())
{
   XSSFRow nextrow=(XSSFRow)iterator.next();
    ArrayList rowarraylist=new ArrayList();
    Iterator<Cell> cellIterator=nextrow.cellIterator();

    while(cellIterator.hasNext())
    {
        XSSFCell cell=(XSSFCell)cellIterator.next();
        rowarraylist.add(cell);
    }
    CellArrayListHolder.add(rowarraylist);
}
       out.println(CellArrayListHolder);
       ArrayList rowarraylist=null;
       PreparedStatement st=con.prepareStatement("insert into DYNAMIC_INSERT values(?)");

for(int i=1;i<CellArrayListHolder.size();i++)
{
    rowarraylist=(ArrayList)CellArrayListHolder.get(i);
     //st.setString(1, file_name);
    st.setString(1, rowarraylist.get(0).toString());
    //st.executeUpdate();
    count=st.executeUpdate();

}
if(count>0)
{
       out.println("<script type=\"text/javascript\">");
   out.println("alert('File added');");
      out.println("location='xlsUpload.html';");
   out.println("</script>");    
}
}
}
catch(Exception ex)
{
 out.println("<script type=\"text/javascript\">");
   out.println("alert('File not found');");
      out.println("location='xlsUpload.html';");
   out.println("</script>");    

}


  %>
    </body>
</html>
 类似资料:
  • 我正在尝试将文件上传到 Azure Blob 存储,但在将文件推送到存储中时收到错误。 我使用java 11和Quarkus进行开发。在POM上,我添加了工件azure-storage-blob和azure-sdk-bom 法典: 恢复错误 io.net.cha.DefaultChannelPipeline] (vert.x-eventloop-thread-2) 一个 exceptionCaug

  • 我正在OpenAPI 3.0中使用Swagger编辑器。我需要文件的路线,其中包括上传图像。当尝试“试用”时,我不会让文件浏览器选择要在请求正文中上载的图像,我得到的只是一个带有参数名称和类型的JSON字符串。 这是路线的YAML描述: 我错过了什么?

  • 最近我卸载了Wamp服务器并重新安装了较新版本的Wamp服务器,即3.1.3 注意:我用的是Windows 10 以下是Wamp Server的所有详细信息: 当我在phpMyAdmin中单击MYSQL数据库,然后单击导出时,phpMyAdmin显示空白页面。然而,当我单击导入时,phpMyAdmin根本不会显示空白页面。但奇怪的是,当单击Export时,phpMyAdmin显示空白页面。 由于我

  • 我正在尝试读取文本文件,然后对其应用一些数据验证规则。添加规则后,我将数据写回Excel文件。 但是当尝试将其写回excel文件时,我收到了这个错误: 线程“主”org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:保存失败:保存包时发生错误:部分 /docProps/app.xml无法保存在流中,编组器org.apache.p

  • 我试图通过在控制器中执行以下操作在数据库中上载多个图像: 公共函数存储(请求$request){ 它采用图像表单,并将所选图像存储在public/image文件夹中,但在数据库中,它存储具有相同图像名称的所有图像。在显示的同时,它会多次显示相同的图像。 我知道这里的人有解决这个问题的办法,也许会有更好的主意。所以请帮我解决这个问题。提前谢谢。 上面的“完成”方法不适合我,因此我在控制器中执行了此操

  • 我对Windows Azure完全陌生。 我目前有一个本地db用于我的。NET应用程序。我已经创建了一个azure帐户以及相关的SQL azure数据库。 我正在添加C代码,以便将数据从本地应用程序传递到云。为了测试此功能,我添加了一个随机用户,我想将其插入Azure DB。 我使用了以下教程:http://www.windowsazure.com/en-us/develop/net/how-to