简体   繁体   中英

Java File retrieving from FTP server

I am trying to retrieve a file from ftp server but I get error as below. Would you please help me

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream.GetField;

import java.io.OutputStream;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

public class FtpTest_V1 {

public static void main(String[] args) {

  String server = "192.168.200.8"; int port = 21; String user = "Test_user"; String pass = "123456**"; FileOutputStream fos = null; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); showServerReply(ftpClient); ftpClient.enterLocalPassiveMode(); int replyCode = ftpClient.getReplyCode(); System.out.println(replyCode); //isPositiveCompletion if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println(FTPReply.isPositiveCompletion(replyCode)); System.out.println(FTPReply.isNegativeTransient(replyCode)); System.out.println("Connect failed"); return; } boolean success = ftpClient.login(user, pass); //System.out.println(success); showServerReply(ftpClient); if (!success) { System.out.println("Could not login to the server"); return; } // Lists files and directories FTPFile[] files1 = ftpClient.listFiles("TEST2"); //printFileDetails(files1); // uses simpler methods String[] files2 = ftpClient.listNames("TEST2"); printNames(files2); String[] files = files2; for (String aFile: files) { String filename = aFile; fos = new FileOutputStream(filename); // Download file from FTP server ftpClient.retrieveFile("C://test//FTP_TEST//GET" + filename, >fos); } } catch (IOException ex) { System.out.println("An Error Occured"+ex.getMessage()); System.out.println("Warning! Something wrong happened"); ex.printStackTrace(); } finally { // logs out and disconnects from server try { if (fos != null) { fos.close(); } if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } private static void printNames(String files[]) { if (files != null && files.length > 0) { int i =0; for (String aFile: files) { i++; } System.out.println("Number of files = "+i); for (String aFile: files) { System.out.println(aFile); } } } private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER : " + aReply); } } 

}

}

Here is my Output :

SERVER : 220-FileZilla Server version 0.9.43 beta

SERVER : 220 Hello FTP SERVER

220

SERVER : 230 Logged on

number of files = 2

TEST2/testfile.csv

TEST2/tttt.csv

An Error OccuredTEST2\\testfile.csv (The system cannot find the path specified)

Warning! Something wrong happened

java.io.FileNotFoundException: TEST2\\testfile.csv (The system cannot find the path specified)

at java.io.FileOutputStream.open(Native Method)

at java.io.FileOutputStream.(Unknown Source)

at java.io.FileOutputStream.(Unknown Source)

at FtpTest_V1.main(FtpTest_V1.java:74)

The slash should be backwards like this: "C:\\ \\test\\ \\FTP_TEST\\ \\GET" (without the middle space, I put it that way because the html parser in this page change it to only one slash) intead of:

ftpClient.retrieveFile("C://test//FTP_TEST//GET" + filename, >fos); 

Or even better:

"C:" + File.separatorChar + "test" + File.separatorChar + "FTP_TEST" + File.separatorChar + "GET"

Hello I have solved the problem as changing the code. try catch block is as below

ftpClient.connect(server, port);

ftpClient.login(user, pass);

ftpClient.enterLocalPassiveMode();

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

String[] files = ftpClient.listNames("TEST_FILE");

  for (String aFile: files) { String remoteFile1 = aFile; File downloadFile1 = new File("C:/test/FTP_TEST 

/GET/"+remoteFile1.replace("TEST_FILE", ""));

  OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1)); boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1); outputStream1.close(); if (success) { System.out.println("File #1 has been downloaded successfully."); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM