[Solved] How to receive a file type parameter from html/jsp into a servlet


After painstaking efforts and google search I found a solution to my problem. A page from Stackoverflow helped very much. First I changed the get method of my form to post like this

<form action="Upload" method="post" enctype="multipart/form-data">
    Image<input type="file" name="image" id="image" accept="image/jpg">
    <input type="submit" value="submit">
</form>

Then I wrote the following servlet code. We accept the <input type="file">data as Part data in servlet. Then we convert it to input stream. The input stream then can be saved in database. Here is my Servlet:-

package controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import model.ConnectionManager;

@MultipartConfig(location="/tmp", fileSizeThreshold=1048576, maxFileSize=20848820, maxRequestSize=418018841)
public class Upload extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Part filePart=request.getPart("image");`// Retrieves <input type="file" name="image">`
        String filePath = filePart.getSubmittedFileName();//Retrieves complete file name with path and directories 
        Path p = Paths.get(filePath); //creates a Path object
        String fileName = p.getFileName().toString();//Retrieves file name from Path object
        InputStream fileContent = filePart.getInputStream();//converts Part data to input stream

        Connection conn=ConnectionManager.getConnection();
        int  len=(int) filePart.getSize();
        String query = ("insert into IMAGETABLE(ID,NAME,LENGTH,IMAGE) VALUES(?,?,?,?)");


        try {
            PreparedStatement pstmt = conn.prepareStatement(query);
            pstmt.setInt(1, 5);
            pstmt.setString(2, fileName);
            pstmt.setInt(3, len);
            pstmt.setBinaryStream(4, fileContent, len);
            success=pstmt.executeUpdate();
        } catch (SQLException ex) {
            System.out.println("Error : "+ex.getMessage());
        }finally{
            try{
                if(fileContent!=null)fileContent.close();
                if(conn!=null)conn.close();
            }catch(IOException | SQLException ex){
                System.out.println("Error : "+ex.getMessage());
            }
        }

    }

}

After execution, it does the job successfully. We accept the image from user and save it in database. Hope this solution will help all 🙂

solved How to receive a file type parameter from html/jsp into a servlet