0

I have a form with an input type file.

<form id="upload" action="fileUpload.jsp" method="post">
        <input type="file" id="qns" name="qns" accept=".xls,.xlsx"><br><br>
        <input type="submit" id="uploadFile" value="Upload File">
</form>

The user should be able to browse to an excel file and on submit, I want the excel data to be uploaded to Oracle XE database(I am using apache POI).

However, in the fileUpload.jsp page, I am unable to access the browsed file since it gives only the file name and not the entire path.

The code in fileUpload.jsp is:

String filename = request.getParameter("qns");
InputStream input = new FileInputStream(filename);    
POIFSFileSystem fs = new POIFSFileSystem(input);

Error:

java.io.FileNotFoundException: Abc.xls (The system cannot find the file specified).

Do I need to upload the file to server via servlet and then access it? Please help.

1 Answer 1

1

You're following it wrong. We don't get access to user's disk. FileInputStream is to read file from the disk that you have access to. In this case your user and your server are on different machines, may be even world apart.

For that you need a multipart form and a Java utility which can read Multipart file stream for you.

Here's an Apache Utility that does exactly that.

If you prefer to do it in plain Java EE style then here's the link from the oracle documentation for file uploading.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.