Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/06/2006, 12:47
Avatar de dogduck
dogduck
 
Fecha de Ingreso: enero-2006
Ubicación: ¿Atlantida, Hesperides, Islas afortunadas?
Mensajes: 2.231
Antigüedad: 18 años, 4 meses
Puntos: 19
Mirate este ejemplo , donde se trabaja con el protocolo file:// de windows ( en realidad seria el protocolo smb )
Cita:
http://club.idecnet.com/~ccastano/femepa/131205.htm
Práctica: Hacer una especie de FTP por medio de URL y el protocolo file://



/*
* FTPURL.java
*
* Created on 12 de diciembre de 2005, 11:55
*/

//package urlftp;
import java.net.*;
import java.lang.*;
import java.io.*;


/**
*
* @author [email protected]
*/
public class FTPURL extends javax.swing.JFrame {

/** Creates new form FTPURL */
public FTPURL() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();

getContentPane().setLayout(null);

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jLabel1.setText(" FTP a traves de URL: protocolo file://");
jLabel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
jLabel1.setHorizontalTextPosition(javax.swing.Swin gConstants.CENTER);
getContentPane().add(jLabel1);
jLabel1.setBounds(60, 10, 240, 20);

jTextField1.setText("carpeta/fichero.ext");
getContentPane().add(jTextField1);
jTextField1.setBounds(180, 40, 170, 20);

jLabel2.setText("Origen : file://");
getContentPane().add(jLabel2);
jLabel2.setBounds(30, 40, 80, 20);

jLabel3.setText("Destino :");
getContentPane().add(jLabel3);
jLabel3.setBounds(30, 70, 60, 20);

jTextField2.setText("C:/Directorio/fichero.ext");
getContentPane().add(jTextField2);
jTextField2.setBounds(80, 70, 270, 20);

jButton1.setText("Copia fichero");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

getContentPane().add(jButton1);
jButton1.setBounds(70, 100, 140, 23);

jLabel4.setText("Estado :");
jLabel4.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
getContentPane().add(jLabel4);
jLabel4.setBounds(30, 140, 240, 20);

jTextField3.setText("host");
getContentPane().add(jTextField3);
jTextField3.setBounds(110, 40, 50, 20);

jLabel5.setText("/");
getContentPane().add(jLabel5);
jLabel5.setBounds(170, 40, 10, 20);

pack();
}
// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//ante el evento clikar boton
String texto=null;
File ficheronuevo = null;

int ncaracter;
try{

String cadena = "file://"+jTextField3.getText()+"/"+jTextField1.getText();
URL ficheroremoto = new URL(cadena);
BufferedReader servidor = new BufferedReader(new InputStreamReader(ficheroremoto.openStream()));
ficheronuevo = new File(jTextField2.getText());
BufferedWriter cliente = new BufferedWriter(new FileWriter(ficheronuevo));

//leemos caracter a caracter

while ((ncaracter=servidor.read())!=-1){

cliente.write(ncaracter);
//Depura : jLabel4.setText(ncaracter.toString());


}//fin mientras --
servidor.close();
cliente.flush();
cliente.close();
jLabel4.setText("Copiado. OK");
}
catch(Exception e){jLabel4.setText(e.toString());}


}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FTPURL().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration

}