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.

  • Yes, it is possible to get that file. Now read: [Why is “Is it possible to…” a poorly worded question?](https://softwareengineering.meta.stackexchange.com/q/7273/202153) – Andreas Mar 08 '20 at 01: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?](https://stackoverflow.com/q/4526273/5221149). – Andreas Mar 08 '20 at 01: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. – User12322341232142 Mar 08 '20 at 01:13
  • If the web page only posted the file name for an `` 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?](https://stackoverflow.com/q/8659808/5221149). I.e. please do your **research** on how to do file uploads. – Andreas Mar 08 '20 at 04:37

0 Answers0