1

I have a JSP/Servlet application that has a form containing multiple parts.

Client side:

        out.println("<form action='cbehindRegister_submit.php' method='post'enctype=\"multipart/form-data\">");
        out.println("<table>");
        out.println("<tbody>");
        out.println("<tr>");
        out.println("<td><label>Username: </label></td>");
        out.println("<td><input type='text' required id='input_username' name='input_username'/></td>");
        out.println("<td><label id='valusername' class='val'> </label></td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td><label>Country: </label></td>");
        out.println("<td><input type='text' required id='input_country' name='input_country'/></td>");
        out.println("<td><label id='valcountry' class='val'> </label></td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td><label>Profile picture: </label></td>");
        out.println("<td><input type='file' required id='input_profile_picture' name='input_profile_picture' accept='image/x-png,image/gif,image/jpeg' /></td>");
        out.println("<td><label id='valprofile_picture' class='val'> </label></td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td><label>Birth date: </label></td>");
        out.println("<td><input type='date' required id='input_birth_date' name='input_birth_date' /></td>");
        out.println("<td><label id='valbrith_date' class='val'> </label></td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td><label>Password: </label></td>");
        out.println("<td><input type='password' required id='input_password' name='input_password' /></td>");
        out.println("<td><label id='valpassword' class='val'> </label></td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td><label>Verify password: </label></td>");
        out.println("<td><input type='password' required id='input_verifypassword' name='input_verifypassword' /></td>");
        out.println("<td><label id='valverifypassword' class='val' > </label></td>");
        out.println("</tr>");

        //request
        out.println("<tr>");
        out.println("<td height='45px' colspan='3'><input type='submit' id='submit' value='Registreer' id='regsubmit' /></td>");
        out.println("</tr>");

        out.println("</tbody>");
        out.println("</table>");
        out.println("</form>");

The question is how can I get the file uploaded as an byte array so that it can be stored as blob in a database?

Is there anyway of getting the file on a similar way as

File file =  request.getParameter("nameOfFormParameter");

or

byte[] image =  request.getParameter("nameOfFormParameter");

Most guides on the internet are for storing the file as is on the server.

EDIT 1: Most questions relating file upload give a solution that stores the files on disk and dont handle the files in memory. The purpose of this question is to find a solution that handles uploaded file in memory without writing the files directly to disk , but stores them in a database.

AXANO
  • 163
  • 1
  • 12

1 Answers1

1

After searching for around 2 days I finally found the answer:

Firstly

add enctype=\"multipart/form-data\ in the form tag so that the result is :

<form action='cbehindRegister_submit.php' method='post'enctype=\"multipart/form-data\">

Secondly

use the following line of code to receive the file (be sure that the name of the input tag of the form is the same as the string in the getPart function):

Part filePart = request.getPart("input_profile_picture");

Thirdly

create an inputstream from the filePart and convert the inputstream to a byte array

InputStream filecontent = filePart.getInputStream();
byte[] fileAsByteArray = IOUtils.toByteArray(filecontent);

Now , the image is ready to be blobbed!

AXANO
  • 163
  • 1
  • 12