简体   繁体   中英

JSP get the name of the uploaded file

I need to upload a Blob file into a MySQL database using JSP. I was able to do that, but I just can't store the name of the actual file. It's very important because the next step would be a list of the uploaded files. This is what I got so far:

@MultipartConfig
public class FileUploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        DBConnection DBC = new DBConnection();
// this is the database connection method
        Connection con = DBC.connection();

        InputStream inputFile = null;
        Part file = request.getPart("file");
        if(file != null){
            inputFile = file.getInputStream();
        }
        Date utilDate = new Date();
        Date sqlDate = new java.sql.Date(utilDate.getTime());

        try{
            String fileUpload = "insert into uploads(file,uploaded_date) values('"+inputFile+"','"+sqlDate+"')";
            Statement st = con.createStatement();
            int insertFile = st.executeUpdate(fileUpload);
            response.sendRedirect("ok.jsp");
        }catch(SQLException e){

        }
    }

So, if I upload a file called picture.jpg, I'd like to store picture.jpg in a String, not the streams java.io.FileInputStream@55c3ece1 or something like this. Thanks in advance!

According to Java EE Tutorial it is possible to retrieve the file name from Content-Disposition Header. The linked article shows how to do exactly that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM