0

I am simulating client-server behavior on my laptop. the scenario is where the client uploads a file to the server and then is able to download the same file from the server. I was wondering though how I would exactly save it? Do i need to designate a specific location on my pc? i am using java to program my server and client.

So far, I have gotten to the thought of using the FileInputStream class and FileOutputStream class, but I am lost after this point.

Any advice would be appreciated.

user2910237
  • 133
  • 1
  • 2
  • 7

1 Answers1

1

Yes, you need to designate a specific location on your machine. One way to do it is to send the contents of the file in a POST request from the client to your server.

An example with many things omitted:

PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileToUpload), "UTF-8"));
for (String line; (line = reader.readLine()) != null;)
{
    writer.println(line);
}

Check out this answer for more info.

Community
  • 1
  • 1
jhn
  • 371
  • 2
  • 8