I'm working in an simple File Transfer program but I really get a point where is impossible to continue, cause the compiler give the following error:
Cliente_FTP.java:60: error: cannot find symbol conexion.enviar_archivo(nombre_archivo); // THIS IS THE LINE GENERATING THE ERROR !!! ^ symbol: method enviar_archivo(String) location: variable conexion of type Misocket 1 error
I'm just trying to create a new method (enviar_archivo and recibir_archivo) in a package (Misocket) but it seams that is not reachable when this is called.. I will try to explain me better showing the code, firts I have a Client side with interchange the log and password with the server, and until here everythng goes well, but when I try to call in method enviar_archivo in the class Misocket and compile, I got the error above. Please if some of you see what I'm doing bad please let me know.
This is the Client whos call the clases Misocket that is a Socket extention.
import java.io.*;
import java.net.*;
import Misocket.*;
public class Cliente_FTP {
static String log;
static String clave;
static String nomb;
static String direccion;
Socket servidor;
int numCliente=0;
static String mensaje;
public static void main(String args[]) {
direccion = "localhost";
int puerto = 5972;
String operacion = null;
String nombre_archivo;
try {
Misocket conexion = new Misocket(direccion, puerto);
System.out.println(conexion.recibir_mensaje());
boolean bandera_operacion;
bandera_operacion = true;
while(bandera_operacion){
operacion = System.console().readLine("Ingrese (1) autenticar usuario o (2) para registar usuario: ");
if(operacion.equals("1")){
System.out.println("autenticar usuario");
bandera_operacion = false;
}
else if (operacion.equals("2")) {
System.out.println("registrar usuario");
bandera_operacion= false;
} else {
System.out.println("error");
bandera_operacion = true;
}
}
log = System.console().readLine("usuario: ");
clave = System.console().readLine("clave: ");
mensaje = operacion + " " + log + " " + clave;
conexion.enviar_mensaje(mensaje);
mensaje = conexion.recibir_mensaje();
System.out.println(mensaje);
if(!"Usuario no existente".equals(mensaje)){ // si el usuario si existe ejecutar codigo para transferir archivos
System.out.println("listo para transmitir archivos");
nombre_archivo = "ENVIAR+ARCHIVO" + "./FTP/ArchivoServidor.txt";
conexion.enviar_archivo(nombre_archivo); // THIS IS THE LINE GENERATING THE ERROR !!!
}
conexion.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
the class Misocket that contain the method to create socket, streams , send an receive messages, everything goes well but the methos that should send and receive Files: package Misocket;
import java.io.*;
import java.net.*;
public class Misocket extends Socket{
private Socket socket;
private BufferedReader entrada;
private PrintStream salida;
private InputStream is;
private OutputStream os;
private String nombre_archivo;
public Misocket(String host_servidor, int puerto) throws SocketException, IOException{
socket = new Socket(host_servidor, puerto);
crear_flujos();
}
public Misocket(Socket socket) throws IOException{
this.socket = socket;
crear_flujos();
}
private void crear_flujos() throws IOException {
InputStream flujoentrada = socket.getInputStream();
entrada = new BufferedReader(new InputStreamReader(flujoentrada));
OutputStream flujosalida = socket.getOutputStream();
salida = new PrintStream (flujosalida);
} // FIN CREAR_FLUJOS
public void enviar_mensaje(String mensaje) throws IOException {
if(mensaje.contains("ENVIAR+ARCHIVO")){
nombre_archivo = mensaje.replaceAll("ENVIAR+ARCHIVO", "");
System.out.println("enviando " + nombre_archivo);
enviar_archivo(nombre_archivo);
} else {
System.out.println("estoy como mensaje" + nombre_archivo);
salida.println(mensaje);
salida.flush();
}
} // FIN ENVIAR MENSAJE
public String recibir_mensaje() throws IOException {
String mensaje = entrada.readLine();
return mensaje;
} // FIN RECIBIR_MENSAJE
public void enviar_archivo(String nombre_archivo) throws IOException {
File archivo = new File(nombre_archivo);
byte[] mybytearray = new byte[(int) archivo.length() + 1];
FileInputStream fis = new FileInputStream(archivo);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
System.out.println("Sending " + "(" + mybytearray.length + " bytes)");
salida.write(mybytearray,0,mybytearray.length);
salida.flush();
fis.close();
bis.close();
} // FIN ENVIAR ARCHIVO
public void recibir_archivo() throws IOException{
int filesize = 6022386;
int bytesRead;
int current = 0;
byte[] mybytearray = new byte[filesize];
FileOutputStream fos = new FileOutputStream("def");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead = is.read(mybytearray, current,
(mybytearray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
bos.close();
}
public void cerrar() throws IOException {
socket.close();
} // FIN CERRAR SOCKET
} // FIN DE LA CLASE
Regards /Russo