0

I need to call a servlet via another Java file not via UI. I need to send a csv file along with the request. How can I send the file along with the request which I can access in the servlet.

        File myFile = new File("c:\\myfile.csv");
        String strFormURL = "http://localhost:8080/Forms/MyFacesServlet/";
        url = new URL(strFormURL);

        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                .openConnection();

        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");

Please advice how the file (myFile) can be passed along with the servlet call. So that I can access the file in Servlet and call a service to validate the file content.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Megan
  • 669
  • 2
  • 9
  • 19
  • Can you try this? https://stackoverflow.com/questions/11766878/sending-files-using-post-with-httpurlconnection – Shashanka Kalita Dec 08 '20 at 05:39
  • See "Uploading files" section of abovelinked duplicate. – BalusC Dec 09 '20 at 09:38
  • Thanks @BalusC. Could you please share how the file can be read in Servlet as well? I can't see the same as request.getParameter("file name"); – Megan Dec 10 '20 at 03:51
  • Just click the link behind *"Also see this answer for examples of both the FileUpload and the Servelt 3.0 approaches."* in the bottom of "Uploading files" section of abovelinked duplicate. – BalusC Dec 10 '20 at 08:25

0 Answers0