0

I am trying to upload a file to an FTP server

As I've found here How do you upload a file to an FTP server?, I have this code:

FTPClient client = new FTPClient();
FileInputStream fis = null;

try {
    client.connect("IP");
    client.login("user", "pwd");
    client.changeWorkingDirectory("/a/b/c/");

    // Create an InputStream of the file to be uploaded
    String filePath = file.getPath();
    fis = new FileInputStream(filePath);
    String fileName = file.getName();                               

    // Store file to server
    client.storeFile(fileName, fis);
    client.logout();

} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
    if (fis != null) {
        fis.close();
    }
    client.disconnect();
    } catch (Exception e) {
    e.printStackTrace();
    }
}

When I run it, the file is created where expected, but it is empty (0 kb)

The writing process also takes quite a lot of time...

What am I doing wrong?

Community
  • 1
  • 1
Maik
  • 811
  • 3
  • 22
  • 35
  • Find if this piece of code is good: String filePath = file.getPath(); fis = new FileInputStream(filePath); how do you create file object? It's may be slow because your internet connection is slow or your ftp server is slow... may be – thepaulo Dec 15 '16 at 11:21
  • Any error/exception? – Martin Prikryl Dec 15 '16 at 11:25
  • Can you upload the same file to the same server and directory using a standalone FTP client? – Martin Prikryl Dec 15 '16 at 11:26
  • I have added System.out.println(file.length()); and the file i'm trying to transfer isn't empty (991b). It's a simple .txt file. No errors or exceptions. I can move the file to that location with FileZilla witohout problems – Maik Dec 15 '16 at 13:31
  • As I hav found here java FTP upload creates empty file (the answer by JK Patel), adding client.enterLocalPassiveMode(); resolved my problem – Maik Dec 19 '16 at 09:04

1 Answers1

-1

Look at this post Apache Commons FTP problems You need to set file type and tranfor mode to make this work.

Community
  • 1
  • 1
Ran Koretzki
  • 464
  • 3
  • 15