ciao ragazzi!
Sono un paio di anni che mi cimento con questo (fastidioso) linguaggio, fino ad ora ho sempre risolto i miei problemi sbattendo la testa sulla tastiera e con il grande potere dell'oracolo (google), questa volta però ho un problemino da cui però non ne sto uscendo.

Ho sviluppato un'applicazione, che fra le altre cose deve gestire un invio di file tra server e clients, questi possono inviare e spedire file anche in parallelo sfruttando una socket diversa per ogni file e un thread diverso per ogni socket.

un po di codice così mi spiego meglio:

THREAD SUL CLIENT:

codice:

private class ParallelClientSendFile implements Runnable {
	File file;
	public ParallelClientSendFile (File file){
		this.file=file;
	}
		
		public void run() {
			
			Socket socket = null;
		    try {
		    	
		    	System.out.println(" --invio file " + file.getName());
		        System.out.println(" --dimensione file " + file.length());
			socket = new Socket(SERVER_HOST, SERVER_FILE_PORT);
				
                               // /*
				//con bufferd stream, FUNZIONA
				BufferedInputStream in=new BufferedInputStream(new  FileInputStream(file));
				BufferedOutputStream out=new BufferedOutputStream(socket.getOutputStream());
				
				int a;
				while((a=in.read())!=-1)
				out.write(a);
				
				out.flush();
				in.close();
				//*/
				
				//CON BUFFERED STREAM E OBJECT STREAM NON FUNZIONA
				/*
				ObjectInputStream in=new ObjectInputStream (new BufferedInputStream(new FileInputStream(file)));
				ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
				
				out.reset();
				out.writeObject(in.readObject());
				out.flush();
				in.close();
				
				 */
				
				out.close();
			    socket.close();
			      
			      System.out.println ("client:invio completato");
			      
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		
	}


THREAD SUL SERVER:

codice:
private class ParallelServerReceiveFile implements Runnable{

	Socket socket;


	
	//metodo costruttore
	private ParallelServerReceiveFile(Socket socket) {
			
			   this.socket = socket;
			    ip=socket.getInetAddress().getHostAddress();
	
	}
	
	
	
	public void run() {
		
		 try {
			 
			
			 
			 
			 
			 ///*
			 //buffered stream,  salva FUNZIONA
			 BufferedInputStream in=new BufferedInputStream(socket.getInputStream());
			 
				String path_jar = (new File(".")).getCanonicalPath();
			         //NON SO IL NOME DEL FILE, DEVO CHIAMARLI TUTTI "test.txt"??! XD
				File saveFile = new File(path_jar + "/" + "test.txt");
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
				
				int a;
				while((a=in.read())!=-1)
				out.write(a);
				
				out.flush();
				//*/
		
			
			 
			/*
				//object stream per prendere il nome, NON FUNZIONA
				 
		 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			 File inFile = (File) in.readObject();
				String path_jar = (new File(".")).getCanonicalPath();
			 
				File saveFile = new File(path_jar + "/" + inFile.getName());
				ObjectOutputStream out=new ObjectOutputStream ( new BufferedOutputStream(new FileOutputStream(saveFile)));
				
				 out.writeObject(inFile);
				    
				    out.flush();;
		    
		    
		    
		
		   */
		    
		    
		    	in.close();
		    	out.close();

				System.out.println ("Server:invio completato");
				socket.close();
				
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

inizialmente provai semplicemente a scrivere e leggere usando solo ObjectStream sia dal client che dal server, e tutto funzionava, sul server così potevo fare:

ObjectInputStream in=new ObjectInputStream(socket.getInputStream());
File file=(File) in.readObject();

e da qui estrapolarmi il nome del file e procedere al salvataggio.

Notai però che con più thread e con file grandi le operazini di I\O diventavano veramente costose, così decisi di inserire tutto in un bufferdStream.

Utilizzando solo buffered stream funziona tutto, però non posso estrapolarmi il nome del file, effettivamente potrei passarlo dal client al server però vorrei evitare..

unendo i due stream, come puoi vedere nel codice postato, dove ho messi */ il compilatore mi da lato client:

java.io.StreamCorruptedException: invalid stream header: 686E6768
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Client$ParallelClientSendFile.run(Client.java:292)
at java.lang.Thread.run(Unknown Source)

sulla riga: ObjectInputStream in=new ObjectInputStream (new BufferedInputStream(new FileInputStream(file)));

Qualche idea??
Vi ringrazio in anticipo.