im making a client server application in java using sockets
I’ve implemented the basic client and server, the client can send pictures to the server and the server stores them but i don’t know how to store multiple files. At the moment im using the following code to store the file received from the client
while(true)
{
Socket connectionSocket = serverSocket.accept();
try (DataInputStream receivedFromClient = new DataInputStream(connectionSocket.getInputStream());
FileOutputStream saveToServer = new FileOutputStream("files/file.jpg"))
{
int i;
while ((i = receivedFromClient.read()) > -1)
{
saveToServer.write(i);
}
}
}
This is how im saving one file, and the file gets overwritten by any new file that the server receives. I want to store multiple files that the client sends, how would i do that? Also how would i set the names of the files that get received?
Add a file name argument to the method in which this code block exists that will allow you pass the filename. Or autoincrement the file name using a static counter, if that is appropriate.
The problem is you are using the same file name every time, as it is hardcoded here:
Instead, the string you are passing to FileOutputStream should be variable, ex.