0

I'm trying to learn a bit of Java's HTTP server, I have a simple setup, on a PHP server I have an input field where I upload a File with POST. I was wondering if it is possible to get that file in the Java server.
My current setup is the most simple one, on my main I have:

HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/test", new MyHandler()); server.start(); 

I can't find anything showing what to do from here on to get the File, I tried looking through everything given by HttpExchange.
I know that I can send a POST request but i don't know how to listen to one or even if it is possible to get the file from it.

4
  • Yes, it is possible to get that file. Now read: Why is “Is it possible to…” a poorly worded question? Commented Mar 8, 2020 at 1:01
  • Your MyHandler class needs to read the content of the POST request body, then process that content. To handle file uploads, the client would send content as multipart/form-data, so do some research and lookup and learn what that means for the POST content, e.g. What does enctype='multipart/form-data' mean?. Commented Mar 8, 2020 at 1:07
  • @Andreas I'm sorry I worded it badly, I meant to ask how to do it as most things I have searched only talks about sending POST requests, never receiving. I have read the content in the request body, however it only gives me the file name, nothing else. Commented Mar 8, 2020 at 1:13
  • If the web page only posted the file name for an <input type="file"> element, then you didn't tell the form to post as multipart/form-data. Check 2nd link up top: How does HTTP file upload work?. I.e. please do your research on how to do file uploads. Commented Mar 8, 2020 at 4:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.