Prova, temporaneamente, ad usare questo codice.
Lato server:
codice:
DataOutputStream output = null;
FileInputStream fis = null;
byte[] buffer = new byte[4096]; // Invio max 4 KB alla volta
int byteLetti = 0;
try {
output = new DataOutputStream( socket.getOutputStream() );
File f = new File( ... ); // Il tuo file da inviare
output.writeLong( f.getLength() );
output.flush();
fis = new FileInputStream( f );
while((byteLetti = fis.read(buffer)) >= 0) {
output.write(buffer, 0, byteLetti);
output.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (output != null) {
try { output.close(); } catch (Exception e) { }
}
if (fis != null) {
try { fis.close(); } catch (Exception e) { }
}
}
Lato client:
codice:
DataInputStream input = null;
FileOutputStream fos = null;
byte[] buffer = new byte[4096]; // Ricevo max 4 KB alla volta
int byteLetti = 0;
long fileLength = 0;
long crtlFileLength = 0;
boolean error = false;
try {
input = new DataInputStream( socket.getInputStream() );
fos = new FileOutputStream( ... ); // Il tuo file di output (ricevuto dal server)
fileLength = input.readLong();
while((ctrlFileLength < fileLength) && !error) {
byteLetti = input.read(buffer);
if (byteLetti >= 0) {
fos.write(buffer, 0, byteLetti);
fos.flush();
ctrlFileLength += byteLetti;
} else {
error = true;
}
}
if ( error ) System.out.println("Riscontrato errore in ricezione file");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
try { input.close(); } catch (Exception e) { }
}
if (fos != null) {
try { fos.close(); } catch (Exception e) { }
}
}
Verifica se con questo codice i file vengono effettivamente trasferiti o se vi sono errori/eccezioni.
Il codice chiude la socket dopo il trasferimento di un file, ma è solo per capire se il trasferimento del singolo file funziona oppure no.
Ciao.