lpr-b-2007-2008:serverupload
package trasferimentoFile; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Writer; import java.net.ServerSocket; import java.net.Socket; public class Server { public static final int PORTA = 12345; private static final int MAXBUFFER = 1024; private static final int MAXNAMELEN = 1024; /** * @param args */ public static void main(String[] args) { try { String destDir = args[0]; ServerSocket ss = new ServerSocket(PORTA); while(true) { Socket s = ss.accept(); InputStream is = s.getInputStream(); byte [] nameBuffer = new byte[MAXNAMELEN]; int i = 0; int letti = is.read(nameBuffer,i++,1); while(nameBuffer[i-1] != ((byte) '\n')) { letti = is.read(nameBuffer,i++,1); } String filename = new String(nameBuffer, 0, i-1); System.out.println("Mi preparo a ricevere il file <"+filename+">"); FileOutputStream bw = new FileOutputStream(destDir+"/"+filename); byte [] buffer = new byte[MAXBUFFER]; letti = is.read(buffer, 0, MAXBUFFER); while(letti>0) { bw.write(buffer, 0, letti); letti = is.read(buffer, 0, MAXBUFFER); } is.close(); } } catch (FileNotFoundException e) { System.out.println("Errore nella creazione del file di output"); } catch (IOException e) { e.printStackTrace(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Usage is:\njava Server directoryDestinazione"); } } }
lpr-b-2007-2008/serverupload.txt · Ultima modifica: 19/09/2008 alle 14:08 (16 anni fa) da 127.0.0.1