l'ho risolta così:
codice:Client: 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); new DataOutputStream(socket.getOutputStream()).writeUTF(file.getName()); 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(); 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(); } }
codice:Server: public void run() { try { String filename=new DataInputStream(socket.getInputStream()).readUTF(); System.out.println ("inviato nome file: "+filename); BufferedInputStream in=new BufferedInputStream(socket.getInputStream()); String path_jar = (new File(".")).getCanonicalPath(); File saveFile = new File(path_jar + "/" + filename); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile)); int a; while((a=in.read())!=-1) out.write(a); out.flush(); in.close(); out.close(); System.out.println ("Server:invio completato"); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

Rispondi quotando