I am try uploading the image using the below servlet But after upload I am able save the file but I am not able to open I checked the file is corrupted.
Instead of using the annotation I have describe multipart-config in web.xml. I this code I am trying to get the image file I send the data using AJAX. Then I am redirected to Register servlet there I am using InputStream class to handle data. After this I creating the file and upload this Inputdata to file in some directory on server.
public class Register extends HttpServlet{
    @Override 
    protected void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String email=req.getParameter("email");
        Part part = req.getPart("image");
        String filename = part.getSubmittedFileName();
        InputStream is = req.getInputStream();
        byte[] data = new byte[is.available()];
        String path = "D:\\FullstackWeb\\images\\icon\\"+filename;
        System.out.println(path);
        FileOutputStream fos=new FileOutputStream(path);
        fos.write(data);
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://dns1.nishchay.com:3306/register","demouser","123Nbr@");
            String query = "Insert INTO register.signup(username,email,userpassword,filename) values(? ,?, ?,?)";
            PreparedStatement pstmt= conn.prepareStatement(query);
            pstmt.setString(1, username);
            pstmt.setString(2, email);
            pstmt.setString(3, password);
            pstmt.setString(4, path);       
            pstmt.executeUpdate();      
            conn.close();
        }catch(Exception e) {
            out.println("<h1>Issue is occured</h1>");       
        }       
    }
}```
 
    