Good afternoon, tell me, I wrote a simple application, the client and the server, the task was to transfer files
CUSTOMER
public class Client_2 implements Runnable { static private ServerSocket server;//ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ ΠΎΡ Socket connection static private Socket client;//ΡΠΎΠ·Π΄Π°ΡΡ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ ServerSocket ΡΠ°ΠΊ ΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΠΎΠ΅Π΄Π΅Π½ΠΈΡΡΡΡ Ρ channel socket /*****************STREAM*****************/ static private BufferedOutputStream output; static private BufferedInputStream input; /***************************************/ public static void main(String[] args) { new Thread (new Client_2()).start(); new Thread (new Server_2()).start(); } @Override public void run() { // TODO Auto-generated method stub byte[] byteArray = new byte[8192]; int in; try { client = new Socket("127.0.0.1", 60000); input = new BufferedInputStream(new FileInputStream("C:/tmp.txt")); output = new BufferedOutputStream(client.getOutputStream()); while ((in = input.read(byteArray)) != -1){ output.write(byteArray,0,in); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } SERVER
public class Server_2 implements Runnable { static private ServerSocket server;//ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ ΠΎΡ Socket connection static private Socket client;//ΡΠΎΠ·Π΄Π°ΡΡ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ ServerSocket ΡΠ°ΠΊ ΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΠΎΠ΅Π΄Π΅Π½ΠΈΡΡΡΡ Ρ channel socket /*****************STREAM*****************/ static private BufferedOutputStream output; static private BufferedInputStream input; /***************************************/ @Override public void run() { // TODO Auto-generated method stub byte[] byteArray = new byte[8192]; int in; try { server= new ServerSocket(60000/*Π½ΠΎΠΌΠ΅Ρ ΠΏΠΎΡΡΠ°*/, 10/*ΠΊΠΎΠ»Π»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΠΉ*/); output = new BufferedOutputStream(new FileOutputStream("D://")); input = new BufferedInputStream(client.getInputStream()); while ((in = ((ObjectInput) output).read(byteArray)) != -1){ ((ObjectOutput) input).write(byteArray,0,in); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } but when you transfer the file gives an error
java.io.FileNotFoundException: D: \ (The system cannot find the specified path) at java.io.FileOutputStream.open0 (Native Method) at java.io.FileOutputStream.open (Unknown Source) at java.io.FileOutputStream. (Unknown Source) at java.io.FileOutputStream. (Unknown Source) at ru.cod.Server_2.run (Server_2.java:37) at java.lang.Thread.run (Unknown Source) Exception in thread "Thread-1" java. lang.NullPointerException at ru.cod.Server_2.run (Server_2.java:50) at java.lang.Thread.run (Unknown Source)
What could be a mistake?
new FileOutputStream("D://"));Where is the file to write to? - Alexey Shimansky"D://"cannot be a file, soFileOutputStreamcannot open it for writing - zRrrclientwhich nothing has been assigned, attempts to read from theoutput, to bring theBufferedInputStreamtoObjectOutput. - zRrrD:/file.txt. And if it is not there, then create a file with the desired name. You donβt transfer the physical file itself, but only the content, so theD://indication is incorrect - Alexey Shimansky