ftp in java, with jakarta commons net library file upload 1.4.1
Hello:
This is my code but it no runs..... i think that connect with the ftp but no transfer of files...
String server = "192.168.1.110";
String username = "joel";
String password = "123456";
String remote = "/privat/Joel/";
String local = "c:\\ftp.txt";
FTPClient ftp = new FTPClient();
//conectar al servidor and login
try
{
ftp.connect(server);
if(!ftp.login(username, password))
{
ftp.logout();
}
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
ftp.disconnect();
InputStream in = new FileInputStream(local);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
boolean Store = ftp.storeFile("/privat/public",in);
in.close();
ftp.logout();
ftp.disconnect();
ANY IDEA?
thanks
[856 byte] By [
joeleta] at [2007-10-3 2:11:44]

Based on your source I have just run this -
String server = "MY SERVER";
String username = "sabre";
String password = "my password";
String local = "/home/sabre/temp.txt";
FTPClient ftp = new FTPClient();
ftp.connect(server);
if(!ftp.login(username, password))
{
ftp.logout();
}
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
ftp.disconnect();
InputStream in = new FileInputStream(local);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
boolean Store = ftp.storeFile("fred.txt",in);
in.close();
ftp.logout();
ftp.disconnect();
and once I made sure my FTP server was running, my user name was correct, my password was correct and the source file existed then everything worked OK.
Thanks, i use windows and in the "local" variable i have to put c:\ftp.txt?and in storefile function, the first parameter is the name that the file will have in the ftp?thankssss
and...ftp.changeWorkingDirectory( folder )must i use this? for situate to the correct folder?
> Thanks, i use windows and in the "local" variable i
> have to put c:\ftp.txt?
OK - so are yoou sure that file c:\ftp.txt exists?
> and in storefile function, the first parameter is the
> name that the file will have in the ftp?
It is the name of the file that will be stored in the root of the directory associated with your ftp login on the ftp server.
Are you sure you have an FTP server running?
> and...> ftp.changeWorkingDirectory( folder )> must i use this? for situate to the correct folder?Probably - but get the basics working first.
sorry...but what common net library file upload do you use?I use common net library 1.4.1 but if you use source distribution, how do you install source distribution?
yes, the file exists.
When i open the backup manually this contains four folders, and i haven't got privileges in three folder. I have to enter in one folder and copy the file there.
Do you understand me?
connection.changeWorkingDirectory(ftp:\\backup\public);
or
connection.changeWorkingDirectory(\public);
backup is then name of the backup
thanks
> sorry...but what common net library file upload do
> you use?
I use the 1.4.1 binary distribution.
> I use common net library 1.4.1 but if you use source
> distribution, how do you install source distribution?
You don't need to install the source though the examples that come with the source are usefull as templates to se ehow to use the package.
ok... very good i understand you but the previous post... do you have any idea?thanks thanks thanks
> yes, the file exists.
> When i open the backup manually this contains four
> folders, and i haven't got privileges in three
> folder. I have to enter in one folder and copy the
> file there.
> Do you understand me?
>
> connection.changeWorkingDirectory(ftp:\\backup\public)
> ;
>
> or
>
> connection.changeWorkingDirectory(\public);
>
> backup is then name of the backup
>
> thanks
I think you probably need just
tp.changeWorkingDirectory("public"));
but make sure you test the return code e.g.
if (ftp.changeWorkingDirectory("public"))
{
// Changed OK
}
if (ftp.changeWorkingDirectory("public"))
{
JOptionPane.showConfirmDialog(null,"change");
}
I do this, and it confirmate that change the file, but don't store the file....
i don't know the problem.....
any idea?
MY CODE:
String server = "MYSERVER";
String username = "joel";
String password = "123456";
String local = "c:/ftp.txt";
FTPClient ftp = new FTPClient();
ftp.connect(server);
boolean login = ftp.login(username, password);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
ftp.disconnect();
ftp.setFileType(ftp.ASCII_FILE_TYPE);
boolean uno = ftp.changeToParentDirectory();
boolean dos = ftp.changeWorkingDirectory("public");
if (ftp.changeWorkingDirectory("public"))
{
JOptionPane.showConfirmDialog(null,"change");
}
boolean conectat = ftp.isConnected();
InputStream in = new FileInputStream(local);
//ftp.setFileType(ftp.BINARY_FILE_TYPE);
boolean Store = ftp.storeFile("ftp.txt",in);
in.close();
ftp.logout();
ftp.disconnect();
ALL THE BOOLEANS BE TRUE, FOR EXAMPLE UNO, DOS ARE TRUE.
can you speak spanish?
thankssss a lot
Ignore all this! I mis-read your post!Message was edited by: sabre150
What happens when you use a standard command line or graphical client to connect and transfer files to your FTP server? Do the files end up where you expect?
i don't know if i understand your question, sorry but i'm spanish amb my english....isn't very well.. jeje, but a lot of thanks for your help.
when I debug the code, the code stop to
boolean Store = ftp.storeFile("ftp.txt",in);
and don't continue.
I'm sure that the ftp is running, the password and the user correct.
Thanks sabre for your valous help
boolean dos = ftp.changeWorkingDirectory("public");
if (ftp.changeWorkingDirectory("public"))
You are CDing twice, did you mean to.
What does you catch clause look like?
mlka at 2007-7-21 9:50:26 >

I don't understand when you tell me CDing twice, what is the meaning?I haven't got catch, my catch iscatch (Exception e){;} thanksss
okokok, now i understand.
boolean dos = ftp.changeWorkingDirectory("public");
if (ftp.changeWorkingDirectory("public"))
{
JOptionPane.showConfirmDialog(null,"change");
}
this isn't correct,
this yes
boolean dos = ftp.changeWorkingDirectory("public");
> I don't understand when you tell me CDing twice, what
> is the meaning?
>
> I haven't got catch, my catch is
>
> catch (Exception e)
> {
>;
>}
> thanksss
Swallowing exceptions like this is a sure way to waste time and is generally regarded as criminal. You almost certainly have an exception being throwns but unless you print a stack trace you will never know which one.
thanks tomorrow i will prove it, good night!
this is the errorCannot assign requested address: bindthanks
Post the full text of the stack trace and indicate which line of your source throws the exception.
[#|2006-08-08T10:04:19.796+0200|WARNING|sun-appserver-pe8.2|com.sun.rave.web.ui.renderer.ImageRenderer|_ThreadID=19;|WEBUI0002: No se ha encontrado el siguiente ID de mensaje entre los recursos de com.sun.rave.web.ui.resources.LogMessages:URL was not specified and generally should be|#]
[#|2006-08-08T10:04:20.031+0200|INFO|sun-appserver-pe8.2|org.apache.shale.remoting.faces.RemotingPhaseListener|_ThreadID=21;|Checking view identifier '/Page1.jsp'|#]
[#|2006-08-08T10:04:20.078+0200|WARNING|sun-appserver-pe8.2|com.sun.rave.web.ui.renderer.ImageRenderer|_ThreadID=21;|WEBUI0002: No se ha encontrado el siguiente ID de mensaje entre los recursos de com.sun.rave.web.ui.resources.LogMessages:URL was not specified and generally should be|#]
[#|2006-08-08T10:04:22.781+0200|INFO|sun-appserver-pe8.2|org.apache.shale.remoting.faces.RemotingPhaseListener|_ThreadID=16;|Checking view identifier '/Page1.jsp'|#]
[#|2006-08-08T10:04:22.796+0200|INFO|sun-appserver-pe8.2|javax.enterprise.system.stream.out|_ThreadID=16;|
com.sun.rave.web.ui.renderer.UploadRenderer::decode()|#]
[#|2006-08-08T10:04:22.796+0200|INFO|sun-appserver-pe8.2|javax.enterprise.system.stream.out|_ThreadID=16;|
com.sun.rave.web.ui.renderer.UploadRenderer::Looking for id form1:fileUpload1_com.sun.rave.web.ui.upload|#]
[#|2006-08-08T10:04:22.796+0200|INFO|sun-appserver-pe8.2|javax.enterprise.system.stream.out|_ThreadID=16;|
com.sun.rave.web.ui.renderer.UploadRenderer::Found id form1:fileUpload1_com.sun.rave.web.ui.upload|#]
[#|2006-08-08T10:04:23.000+0200|SEVERE|sun-appserver-pe8.2|javax.enterprise.system.container.web|_ThreadID=16;|WebModule[/codiftp]Exception occurred!!
java.net.BindException: Cannot assign requested address: bind
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at com.sun.enterprise.server.ss.ASServerSocket.bind(ASServerSocket.java:244)
at com.sun.enterprise.server.ss.ASServerSocketImpl.listen(ASServerSocketImpl.java:121)
at java.net.ServerSocket.bind(ServerSocket.java:320)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at org.apache.commons.net.DefaultSocketFactory.createServerSocket(DefaultSocketFactory.java:155)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:475)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:388)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1388)
at codiftp.Page1.button2_action(Page1.java:372)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at com.sun.rave.web.ui.appbase.faces.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:221)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:257)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.sun.rave.web.ui.util.UploadFilter.doFilter(UploadFilter.java:184)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:263)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:132)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:933)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:189)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.doProcess(ProcessorTask.java:604)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:475)
at com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:371)
at com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:264)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:281)
at com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:83)
|#]
So what is line Page1.java:372 ?
boolean Store = ftp.storeFile("ftp.txt",in);
> boolean Store = ftp.storeFile("ftp.txt",in);That does not look to be the right line!You need to create a simple stand alone example to work with. You will find it difficult to diagnose the problem from within an application server.
yes it is the correct line, i'm surereally thanks
I agree with Sabre, you should use a smaller application to test.
Say:
import org.apache.commons.net.ftp.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Throwable {
String server = "192.168.1.110";
String username = "joel";
String password = "123456";
String remote = "/privat/Joel/";
String local = "c:\\ftp.txt";
FTPClient ftp = new FTPClient();
//conectar al servidor and login
try
{
System.out.println( "Connecting" );
ftp.connect(server);
if(!ftp.login(username, password))
{
System.out.println( "Login failed" );
ftp.logout();
return;
}
int reply = ftp.getReplyCode();
System.out.println( "Connect returned: " + reply );
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println( "Connection failed" );
return;
}
InputStream in = new FileInputStream(local);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
System.out.println( "Downloading File" );
boolean store = ftp.storeFile("/privat/public",in);
in.close();
ftp.logout();
ftp.disconnect();
}
}
}
Message was edited by:
mlk
mlka at 2007-7-21 9:50:26 >

I think I have worked out your problem. The default is to send your commands on port 21 and your data on port 22 but many systems (and firewalls) won't allow you to send data using this port which means you are not allowed to bind to port 22. It is normal to place the server PASV mode. This is achieved by using
ftp.pasv();
Add this just before you send the file and check the return code.
thanks for your help sabre, but it no runs, i'm so desesperated........really thanks
> thanks for your help sabre, but it no runs, Does the standalone version run?Have you tried putting it into PASV mode?> i'm so> desesperated........Don't let the problem get you down. Take your girl out and have a nice glass of wine.
The error is the same adding PASV mode
[#|2006-08-09T09:46:31.578+0200|SEVERE|sun-appserver-pe8.2|javax.enterprise.system.container.web|_ThreadID=18;|WebModule[/codiftp]Exception occurred!!
java.net.BindException: Cannot assign requested address: bind
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at com.sun.enterprise.server.ss.ASServerSocket.bind(ASServerSocket.java:244)
at com.sun.enterprise.server.ss.ASServerSocketImpl.listen(ASServerSocketImpl.java:121)
at java.net.ServerSocket.bind(ServerSocket.java:320)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at org.apache.commons.net.DefaultSocketFactory.createServerSocket(DefaultSocketFactory.java:155)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:475)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:388)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1388)
at codiftp.Page1.button2_action(Page1.java:383)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at com.sun.rave.web.ui.appbase.faces.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:221)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:257)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.sun.rave.web.ui.util.UploadFilter.doFilter(UploadFilter.java:184)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:263)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:132)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:933)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:189)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.doProcess(ProcessorTask.java:604)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:475)
at com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:371)
at com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:264)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:281)
at com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:83)
|#]
Until you use a standalone test harness you are unlikely to find what the problem is. One possible interpretation of the stack trace is that you don't have permisison to setup the connection (bind) to the data port. It could be that you application server needs configuring to allow this. It could be that a firewall has block this. It could be many things BUT you need to create the most simple test harness you can.
ftp.enterLocalPassiveMode(); now it runs!!!!! thank you!
> ftp.enterLocalPassiveMode(); > > now it runs!!!!! thank you!:-) Nice to know - time for a coffee then!
thank you very much sabre"!!!!!
not able to store a file in the serveri userd stoe filebut getting erroe file transfer fails
i used storefile() to store a file on the serverbut file is not geting transfered from my local directory to the server.
Joe,How are you sure that the FTP server is running? can you connect to FTP at the command line? (windows-R ftp)If so... when you just login to FTP and put a file where does it go? that's your FTP home directory.Keith.
> Joe,
>
> How are you sure that the FTP server is running? can
> you connect to FTP at the command line? (windows-R
> ftp)
>
> If so... when you just login to FTP and put a file
> where does it go? that's your FTP home directory.
>
> Keith.
This is an old post that was resolved. An unrelated question has been added.
the above code is correct when you wish to upload a .txt (TEXT) filebut in case of binary file uploads (Images, Videos), make sure to addftp.setFileType(ftp.BINARY_FILE_TYPE);just beforeftp.storeFile(destFname,sourceFileStream);