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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package clientftp;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author mathieu
*/
public class Upload implements Runnable{
private int port;
private String IP;
private String nomFichier;
private String dir;
FileInputStream streamIn;
OutputStream streamOut;
private LigneTransfert ligne;
//private IHM ihm;
public Upload(String dir, String nomFichier, LigneTransfert ligne){
this.nomFichier = nomFichier;
this.dir = dir;
this.ligne = ligne;
}
public synchronized void run() {
Socket sock = null;
StringBuffer reponse = new StringBuffer(4096);
StringBuffer IP_tmp = new StringBuffer(20);
try {
//ClientFTP.getInstance().cmd("TYPE I");
//ClientFTP.getInstance().rep();
if((port = ClientFTP.getInstance().passive(IP_tmp,reponse)) == 0){
ClientFTP.getInstance().deconnecter();
}
IP = IP_tmp.toString();
System.out.println("IP = "+IP+" | port : "+port);
IHM.getInstance().ajoutLog(reponse.toString()+"\n");
ClientFTP.getInstance().cmd("STOR "+nomFichier);
System.out.println("Debut");
sock = new Socket(IP.toString(), port);
IHM.getInstance().ajoutLog(ClientFTP.getInstance().rep());
streamIn = new FileInputStream (dir+"/"+nomFichier);
streamOut = sock.getOutputStream();
ecrireSocket();
streamIn.close();
streamOut.close();
IHM.getInstance().ajoutLog(ClientFTP.getInstance().rep());
System.out.println("Fin");
} catch (UnknownHostException ex) {
Logger.getLogger(Download.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Download.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void ecrireSocket() throws IOException{
byte b[] = new byte[16384];
int nb;
while ((nb = streamIn.read(b)) > 0)
{
streamOut.write(b, 0, nb);
System.out.println("ecrit");
ligne.addNbOctetsEnvoyes(nb);
}
}
} |
Partager