1

我正在SQL Server中进行文本文件批量上传。每当我尝试上传文件时,都会出现以下异常:

[Microsoft] [ODBC SQL Server Driver] [SQL Server] 无法批量插入,因为无法打开文件“C:/Form/Input_File/Form.txt”。操作系统错误代码 3(系统找不到指定的路径)。

请找到以下代码:

public void uploadFiles() 
    {
       File dir = new File(inputFilesPath);
                String[] children = dir.list();
                String completePathFileName = "";
                System.out.println(" Inside Upload ::");
                String saveFileNames = "";
                PreparedStatement prepStat;

                DBConnection  dbConnection=new DBConnection();
                Connection conHandler= dbConnection.getConnection();    
                if(null!=conHandler)        
                    System.out.println(" Clear ::"+conHandler);

                try
                {
                    if (children != null)
                    {
                        for (int i = 0; i < children.length; i++) 
                        {
                            String filename = children[i];
    System.out.println(" children[i]::"+children[i]);
                            // File is validated based on some business rules.
                            if (isValidFile(filename) == 1)
                            {
                                String[] fileSplit = filename.split("E");
                                String[] extnSplit = fileSplit[1].trim().split(".TXT");

                                completePathFileName += (completePathFileName.equals("")) 
                                ? extnSplit[0] : "^" + extnSplit[0];

                                saveFileNames += (saveFileNames.equals("")) 
                                    ? filename : "," + filename;
                                System.out.println(extnSplit[0]);   
                            }
                            else
                            {
                                inValidFileNames += (inValidFileNames.equals("")) 
                                ? filename : ";\n" + filename;
                            }
                        }

                        if (!completePathFileName.trim().equals(""))
                        {
                            System.out.println(completePathFileName);
                            prepStat = conHandler.prepareStatement("Exec StartFileImport ?");
                            prepStat.setString(1, completePathFileName);
                            prepStat.execute();
                            saveFileNameToDatabase(saveFileNames);
                        }

                        }
                }
                catch (SQLException sql)
                {
                    System.out.println(sql.getMessage());
                }
}

从以下代码获取连接对象:

public Connection getConnection()
    {
        System.out.println("In side DB Connection...");
      try{
            //  get a database connection
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
            System.out.println("Before Driver");
            conn= DriverManager.getConnection("jdbc:odbc:form26qa","form26","form26");
            System.out.println("After Driver");
            if(conn != null)
            {
                System.out.println("Connection established...");

            }//if
            else
            {
                System.out.println("Connection failed...");

            }//else
      }//try
        catch(Exception e)
        {
            System.out.println("Exception ocurred..."+e);
            e.printStackTrace();
        }//catch
        return conn;
    }

说明:我正在从输入路径读取文件并尝试获取文件名和文件路径并将文件上传到 SQL Server。

应用程序能够在指定路径中找到输入文件。上传时我收到上述异常

请检查并建议我解决此问题。

4

1 回答 1

0

该文件需要在服务器上可访问。文件路径是相对于服务器的,而不是您的 PC。此外,如果您尝试使用共享或映射驱动器,它将无法正常工作。您需要使用 UNC 路径。

UNC 名称示例

\\teela\admin$ (to reach C:\WINNT)
\\teela\admin$\system32 (to reach C:\WINNT\system32)
\\teela\temp (to reach C:\temp) 
于 2013-07-15T07:58:44.767 回答