2015-09-04 174 views
-1

下面是我從FTP服務器csv文件是空白

public class CsvFileDownload { 
    static DBAdapter dbAdapter; 
    static int inverterType; 
    static String TAG="CsvFileDownload"; 
    static FileOutputStream fos; 

/*method to connect to ftp server and retrieve file from ftp server to local device*/ 

    public static void listDirectory(FTPClient ftpClient, Context context, String parentDir, 
           String currentDir, int level, int inverterId) throws IOException { 
     dbAdapter=new DBAdapter(context); 
     String dirToList = parentDir; 
     inverterType=inverterId; 
     if (!currentDir.equals("")) { 
      dirToList += "/" + currentDir; 
     } 
     FTPFile[] subFiles = ftpClient.listFiles(dirToList); 
     if (subFiles != null && subFiles.length > 0) { 
      for (FTPFile aFile : subFiles) { 
       String currentFileName = aFile.getName(); 
       if (currentFileName.equals(".") 
         || currentFileName.equals("..")) { 
        // skip parent directory and directory itself 
        continue; 
       } 
       for (int i = 0; i < level; i++) { 
        System.out.print("\t"); 
       } 
       if (aFile.isDirectory()) { 
        System.out.println("[" + currentFileName + "]"); 
        listDirectory(ftpClient, context, dirToList, currentFileName, level + 1,inverterId); 
       } else { 
        System.out.println(currentFileName); 

        fos = new FileOutputStream(context.getExternalFilesDir(null).getAbsolutePath()+"example.csv"); 
        String filename = context.getExternalFilesDir(null).getAbsolutePath() + "example.csv"; 

        // Download file from FTP server 
        ftpClient.retrieveFile(currentFileName, fos); 

        System.out.println("Starting to parse CSV file using opencsv:"+currentFileName); 
        parseEcoUsingOpenCSV(filename, context); 

       } 
      } 
     } 
    } 



/* Method to parse csv file */ 
    private static void parseEcoUsingOpenCSV(String filename, Context context) throws IOException { 
     dbAdapter=new DBAdapter(context); 


      ICsvBeanReader beanReader = null; 
      try { 
       beanReader = new CsvBeanReader(new FileReader(filename), CsvPreference.STANDARD_PREFERENCE); 

       // the header elements are used to map the values to the bean (names must match) 
       final String[] header = beanReader.getHeader(true); 
       int amountOfColumns=beanReader.length(); 
       final CellProcessor[] processors = new CellProcessor[amountOfColumns]; 
       System.out.println("Starting to parse CSV file using opencsv:"+filename+" header:"+header); 
       EcogridDetail ecogridDetail; 
       while((ecogridDetail = beanReader.read(EcogridDetail.class, header, processors)) != null) { 
        System.out.println(String.format("lineNo=%s, rowNo=%s, customer=%s", beanReader.getLineNumber(), 
          beanReader.getRowNumber(), ecogridDetail)); 
       } 

      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       if(beanReader != null) { 
        beanReader.close(); 
       } 
      } 



    } 

} 

的問題是,下載的文件是空白的下載csv文件的代碼。我得到以下異常:

Exception:java.lang.NullPointerException: nameMapping should not be null 

回答

0

了代碼調試後,我才知道,我只是在FTP服務器上通過遠程文件的文件名進行下載。由於我得到getReplyCode() 550和getReplyString()作爲「沒有這樣的文件或目錄」。

代替文件名,我們需要在ftp服務器上傳遞文件的整個路徑作爲retrieveFile()方法中的參數。