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?

  • He says to you in Russian: File not found. Wrong way ..... and specifically new FileOutputStream("D://")); Where is the file to write to? - Alexey Shimansky
  • The path "D://" cannot be a file, so FileOutputStream cannot open it for writing - zRrr
  • In general, the server code is not working, starting with a client which nothing has been assigned, attempts to read from the output , to bring the BufferedInputStream to ObjectOutput . - zRrr
  • @VargSieg to write to the file - the file must exist. Those. you must at least write in D:/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 the D:// indication is incorrect - Alexey Shimansky
  • @zRrr Yes, the client is really empty, but what should it be assigned to? - Varg Sieg

1 answer 1

You must specify the name of the file, in fact, as he swears.

 output = new BufferedOutputStream(new FileOutputStream("D://your_file_name")); 

You are trying to write data to a directory. Of course, this is also a file, but it is special, it does not support the recording. Specify the required file name (or some temporary, or let the client additionally pass the file name)

  • 2
    Read the book on java. Any) - Stranger in the Q
  • one
    You need a file name anyway. If you just want the data from the file to be stored - just create a temporary file (for example, the system File.createTempFile) or generate a unique name in the folder and save there. In any case, you can not save a file without a name. What is your task in the end that such strange thoughts on implementation arise? - Chad
  • one
    If you need the file name to match the original, you need to transfer from the client past the file itself and its name. - Chad
  • one
    Well, the most clumsy way, without bringing in new knowledge, is that you form a new file in which you first add its name. and complete the name \ x00 for example. And on the server from the stream, first isolate the name, and write the rest to the file. You can not create a new file on the client, but simply shove the file name first \ x00 into the output and then the file itself - Chad
  • one
    those. you first output.write (fileNameBuf, 0, n) and therefore the file is already - Chad