问题描述:

Below is my code for downloading csv file from ftp server

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();

}

}

}

}

The problem is that the downloaded file is blank. And I get below exception:

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

网友答案:

After debugging my code I came to know that I was just passing the file name of the remote file on ftp server to be downloaded. Due to which I was getting getReplyCode() as 550 and getReplyString() as "No such file or directory".

Instead of filename we need to pass the whole path of the file on ftp server as argument in retrieveFile() method.

相关阅读:
Top