0

So, basically what I want to do is, on a jsp page give option to browse and select an excel file. Then when the user clicks on Upload button, the data from excel is saved into database table.

I am able to insert into database table without the UI i.e., when there is only servlet and database. In that case, I am doing this:

    InputStream fis = new FileInputStream(new File("C:\\Users\\RAJYAWARDHAN\\Desktop\\Book1.xlsx"));

And after this I use fis to one by one enter data into Database table using Apache POI. But when JSP page is included I am using form:

<input type="file" name="file_uploaded" />

So, when not using JSP page, I have the address of file, that is on my machine only. But that's for test purpose. When user will upload, then I will not have the address of the file because file is on users machine. Hence, I cannot use new File("address_of_file") as file is not on my machine(server basically).

Also, I don't want to first upload file from users machine to my machine and then pick it up from that particular path. I just want to import data from the selected file by user and not actually upload and save that file.

What should I do? Please explain in simple terms. Thank You.

Please ask if question is not clear. English is not my first language.

2
  • 1
    Please be aware that JSP is a server side process (the same as a servlet). It sounds like you will have to POST, the file to a servlet and then in that servlet process the xls and insert into the DB Commented Mar 6, 2018 at 2:53
  • @ScaryWombat Yes I am doing it in POST only. Should I upload the whole code? Commented Mar 6, 2018 at 12:34

1 Answer 1

1

I think what you want to use is getPart method in servlets.

Just like request.getParameter you can use request.getPart("file_uploaded"). It gets file for the given name from front end(jsp/HTML).

This returns Part object which can be converted to InputStream. You don't need to save file to some location in this case.

Do not forget to forget to set enctype= multipart/form-data in your form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.