-2

I want to upload file in my doPost method in servlet which is extends HttpServlet. But I didn't find any example aboutn upload in override servlet method. How can I do this?

@WebServlet("/uploadPage")
    public class myServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String file = req.getParameter("fileUpload");
            System.out.println(file);
            byte[] myFile = // TODO????
        }
    }

Thank you for your help.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
clougioc
  • 33
  • 1
  • 5
  • I asked google - https://www.google.ca/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=servlet%20file%20upload%20example and this is much similar to your code (Servlet 3) http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api – Sundararaj Govindasamy Jul 23 '16 at 03:16

1 Answers1

0

There are few steps need to follow for File Upload functionality

   1. Declare form as MultiPartForm data
   2. Have commons dependencies for simplicity
   3. Have Servlet/Controller accept array with filename and data.

Each MVC has its own implementations for controller. This example explains Spring MVC: https://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/

Additionally you can consider security and access control to the directory where file upload is happening as well as Java API for antivirus to scan the uploaded files.

Happy file uploading :)

Amit Mahajan
  • 895
  • 6
  • 34