-1

I'm trying to upload a file from an .jsp. This is the .jsp part

    <form class="user" action="./messaggio" method="POST" enctype="multipart/form-data">
    //other input
    <input type="file" name="allegato" id="allegato">
    //other things

The file should be converted in a Base64 String and stored into the DB. I'm using Tomcat 8.5 and 3.1 Servlet and i have to do it server side without using JS. I really don't know how to continue. I've looking on stackoverflow without understandig well what's the exact process to follow. I'm not looking for the code, but the flow of the event. Sorry for my english. Thanks.

  • Does this answer your question? [How to convert an Image to base64 string in java?](https://stackoverflow.com/questions/36492084/how-to-convert-an-image-to-base64-string-in-java) – Onkar Musale Dec 20 '19 at 07:05

1 Answers1

0

Found the solution:

        Part filePart = request.getPart("file");
        InputStream fileContent = filePart.getInputStream();
        File f = new File(getServletContext() + "temp");
        OutputStream outputStream = null;

        if (isMultipart) {
            try {
                outputStream = new FileOutputStream(f);

                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = fileContent.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
                allegato = //criptMethodHere
            } finally {
                if (outputStream != null) {
                    outputStream.close();
                    f.delete();
                }
            }