Conexion ftp con Java - java.io.IOException: Connection is not open
Publicado por Charly (118 intervenciones) el 23/01/2018 20:23:26
Hola, tengo que crear un programa Java para conectar a localhost mediante FTP.
Utilizo Mozilla, tanto Server como Client, y he logrado conectarlos. Hasta ahí, bien.
Ahora tengo que crear un programa Java para poder manejar archivos entre el servidor y el cliente (subida, descarga, modificación,...).
He creado el siguiente código:
Incluso habiéndolos conectado antes, me da este error:
Conectandose a: http://localhost:8081
java.io.IOException: Connection is not open
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:514)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:648)
at org.apache.commons.net.ftp.FTP.user(FTP.java:793)
at org.apache.commons.net.ftp.FTPClient.login(FTPClient.java:1073)
at ClienteFTP.main(ClienteFTP.java:11)
Nunca había hecho esto y ya se que hay muchos errores, pero me gustaría que me los comentáseis para arreglarlos.
Necesito saber cómo se indica el localhost con el puerto, o por qué me da error el usuario y contraseña.
Muchas gracias por adelantado.
Utilizo Mozilla, tanto Server como Client, y he logrado conectarlos. Hasta ahí, bien.
Ahora tengo que crear un programa Java para poder manejar archivos entre el servidor y el cliente (subida, descarga, modificación,...).
He creado el siguiente código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import org.apache.commons.net.ftp.*;
public class ClienteFTP{
public static void main(String[] args){
FTPClient cliente=new FTPClient();//cliente
String servidor="http://localhost:8081";//servidor
String user="alu";
String pasw="123";
try{
System.out.println("Conectandose a: "+servidor);
boolean login=cliente.login(user,pasw);
String direc="/NUEVODIREC";
if(login){
//subir fichero
cliente.changeWorkingDirectory(direc);
cliente.setFileType(FTP.BINARY_FILE_TYPE);
//stream de entrada con el fichero a subir
BufferedInputStream in=new BufferedInputStream(new FileInputStream("C:\\Users\\cdum7\\Documents\\texto.txt"));
cliente.storeFile("texto.txt",in);
//renombrar fichero
direc="/htcdocs/NUEVODIREC/NUEVO";
cliente.changeWorkingDirectory(direc);
if(cliente.rename("texto.txt","texto1.txt")){
System.out.println("Fichero renombrado...");
}else{
System.out.println("No se ha podido renombrar el fichero...");
}
//eliminar fichero
direc="/htcdocs/NUEVODIREC/NUEVO/texto.txt";
if(cliente.deleteFile(direc)){
System.out.println("Fichero eliminado...");
}else{
System.out.println("No se ha podido eliminar el fichero...");
}
//descargar fichero
direc="/htcdocs/NUEVODIREC/NUEVO";
cliente.changeWorkingDirectory(direc);
//stream de salida para recibir el fichero descargado
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("C:\\texto2.txt"));
if(cliente.retrieveFile("texto2.txt",out)){
System.out.println("Recuperado correctamente...");
}else{
System.out.println("No se ha podido descargar...");
}
in.close();//cerrar flujo entrada
out.close();//cerrar flujo salida
cliente.logout();//logout del usuario
cliente.disconnect();//desconexion del servidor
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
Conectandose a: http://localhost:8081
java.io.IOException: Connection is not open
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:514)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:648)
at org.apache.commons.net.ftp.FTP.user(FTP.java:793)
at org.apache.commons.net.ftp.FTPClient.login(FTPClient.java:1073)
at ClienteFTP.main(ClienteFTP.java:11)
Nunca había hecho esto y ya se que hay muchos errores, pero me gustaría que me los comentáseis para arreglarlos.
Necesito saber cómo se indica el localhost con el puerto, o por qué me da error el usuario y contraseña.
Muchas gracias por adelantado.
Valora esta pregunta


0