0

I am trying to make a function in Java :

public static void upload_files(String us, String pw, String ip, String f){
 try
 {
    FTPClient client = new FTPClient();
    client.connect(ip);
    client.login(us,pw);
    client.upload(f);
 } catch(Exception e) {
    e.printStackTrace();
 }
}

I added apache libraries, but sadly I got an error on "upload" line I suppose I do not use correctly the method upload, but I don't know how to.

When I compile it with Netbeans, it is noticing me that there is an error line "client.upload(f);"

The error given in output is "java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: org.apache.commons.net.ftp.FTPClient.upload"

Thank you all in advance. nb : "f" is the direct path to my file


EDIT :

The problem is "quite" solved since now I upload some files on my ftp server BUT sadly they are all empty, as noticed on below.

oers
  • 18,436
  • 13
  • 66
  • 75
Dupond Durand
  • 31
  • 2
  • 7

1 Answers1

2

The Apache FTPClient does not have an upload method.

What it does have, is a method called storeFile .

It takes as it's parameters the name the file should have on the server, and an InputStream. The InputStream reads from your local file, so you need:

 InputStream is = new FileInputStream( f );
 client.storeFile( some_name, is );
  • now I got the same error with "storeFile", ie "cannot find symbol" thanks to netbeans. And when I run it, this error appear: "java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: storeFile". – Dupond Durand Aug 08 '12 at 20:06
  • I don't think the error comes from netbeans. Here is the error after the compilation : " error: cannot find symbol storeFile("test1", is ); symbol: method storeFile(String,InputStream) location: class main 1 error" – Dupond Durand Aug 08 '12 at 20:17
  • I see. Indeed, now some file is uploaded. Sadly the file is totally empty, but my main point was resolved. No I am wondering why my txt file is not uploaded. But I will have some "test1.txt" uploaded on my ftp server. – Dupond Durand Aug 08 '12 at 20:31
  • Should I open a new topic for such a question ? Or may we continue here ? Because the file which is uploaded is completely empty (0 B). – Dupond Durand Aug 08 '12 at 20:39
  • You may have to call `flush` or `close` on the InputStream. The API documentation is [here](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html). It may appear a bit intimidating if you're new to Java, but it's quite clear when you get used to it. – S.L. Barth is on codidact.com Aug 08 '12 at 20:40
  • I'm logging off now. I think you can find some answers by looking for code examples for FTPClient on the net, and in the FTPClient documentation. If that fails - "I get 0 bytes uploaded with FTPClient.storeFile" is, IMO, sufficiently different from your original question that you can post a new question. Good luck! – S.L. Barth is on codidact.com Aug 08 '12 at 20:43
  • I can imagine that it's clear when you're used to it, but as you noticed well, this is not my case, and I am a bit sorry about it. |||| After having checked I tried this code : InputStream is = new FileInputStream(f); client.storeFile("test1.txt", is ); is.close(); But sadly it didn't worked. Nothing was sent to the server now :/ – Dupond Durand Aug 08 '12 at 20:50
  • I just saw your answer about loggin off. Thank you for your help anyway. – Dupond Durand Aug 08 '12 at 20:54
  • In case you want to see it, here is the question you confirmed me to ask : http://stackoverflow.com/questions/11873353/empty-file-after-upload-with-java-on-my-ftp-server – Dupond Durand Aug 08 '12 at 21:26
  • You're welcome. I've deleted some of my comments to leave this page a bit cleaner. – S.L. Barth is on codidact.com Aug 09 '12 at 07:50