Strumenti Utente

Strumenti Sito


lpr-b:serveruploadbuffered
package trasferimentoFile;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerDue {
 
	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();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
 
				String filename = br.readLine();
				System.out.println("Mi preparo a ricevere il file <"+filename+">");
 
				BufferedWriter bw = new BufferedWriter(new FileWriter(destDir+"/"+filename)); 
 
				char [] buffer = new char[MAXBUFFER];
				int letti = br.read(buffer, 0, MAXBUFFER);
				while(letti>0) {
					bw.write(buffer, 0, letti);
					letti = br.read(buffer, 0, MAXBUFFER);
				}
				br.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/serveruploadbuffered.txt · Ultima modifica: 12/10/2007 alle 13:28 (12 anni fa) da Marco Danelutto