I'm importing Excel document for reading and displaying the content in the UI. I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.
-
Question not clear , please provide more details – Mukul Goel Oct 05 '12 at 14:53
3 Answers
I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.
There's a major thinking mistake here.
Imagine that I am the client who want to upload the file and that you are the server who need to get the file's contents. I give you the path c:\path\to\foo.xls
as sole information. How would you as being the server ever get its contents? Do you have an open TCP/IP connection to my local hard disk file system? Really? Is everyone else's local disk file system with all that sensitive information really so easily accessible by Internet?
That isn't how uploading files works. This would only ever work if the server runs at physically the same machine as the client, so that you can just use FileInputStream
with that path (which would only occur in local development environment).
You should instead be interested in the sole file contents which the client has sent to you along with the HTTP request body. In HTML terms, you can use <input type="file">
for this. But until the upcoming JSF 2.2 there is no equivalent standard JSF <h:xxx>
component for this. You need to look for JSF component libraries offering a component for this. In your case, with RichFaces (as tagged in your question), you can use <rich:fileUpload>
for this. It's unclear which RichFaces version you're using, but for RF 3.3.3 you can find a complete example in the 3.3.3 showcase site and for RF 4.0 in the 4.0 showcase site.
Whatever way you choose, you should in your JSF managed bean ultimately end up with a byte[]
or an InputStream
representing the file contents. Then you've all the freedom to store it wherever you want on the server's local disk file system using for example FileOutputStream
. You can even also just feed it directly to whatever Excel API you're using, most of them have just a method taking an InputStream
or byte[]
.

- 1,082,665
- 372
- 3,610
- 3,555
you better take a look at this article. The solution that uses Tomahawk 2.0 ( http://myfaces.apache.org/tomahawk-project/tomahawk20/index.html ) by BalusC is great
Everything you need is there
-
This is my code Workbook workbook = Workbook.getWorkbook(new File("D:/Docs/MNC/document.xls")); Sheet sheet = workbook.getSheet("Sheet"); ill add the excl file as shown but i want to get it wen i click browse n select Import buton..pls help me how to get this path when user click browse and import – user1654095 Sep 07 '12 at 12:02
-
you mean when you hit the browsers browse button to be taken directly to that file? – MaVRoSCy Sep 07 '12 at 14:52
-
ya instead of hardcoded path of the file name..i want the path name y from ui..how i can get the user selected file path? – user1654095 Sep 11 '12 at 05:28
-
every time user selects different excel files and my code has to getthe file path and read the particular excel data.. – user1654095 Sep 11 '12 at 06:18
-
try reading @BalusC answer. You DON'T need to know the path. The user's browser will supply you a request that includes an uploaded file. It's from the request that you need to extract the file uploaded. Not from the user's file system – MaVRoSCy Sep 11 '12 at 06:24
-
as i am new to this..i need a example code how it works..could u pls give one example code.. – user1654095 Sep 11 '12 at 09:24
-
here it is http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – MaVRoSCy Sep 11 '12 at 09:26
-
its in JSp/Servlets, But i have to do it in JSF AND I am using JBoss seam – user1654095 Sep 11 '12 at 09:36
-
the follow the link in my answer http://stackoverflow.com/questions/5418292/jsf-2-0-file-upload – MaVRoSCy Sep 11 '12 at 09:43
-
the link u have given for Tomahawk jar is showing error message not found..its not available..pls help me to get that jar – user1654095 Sep 11 '12 at 10:29
-
Please solve this In my UI, user will click on browse and select one excel file and on click of import it comes to Actio cLass where i have hardcoded the file location lik dis workbook = Workbook.getWorkbook(new File("D:/Docs/MNC/document.xls")); Sheet sheet = workbook.getSheet("Sheet"); I just want once the user browse and select the excel on click of import it should come to action class for reading and retrieving data from excelso pls help me how can i get the file for reading on click of import – user1654095 Sep 11 '12 at 11:22
String fileName = FilenameUtils.getName(uploadedFile.getName());
byte[] bytes = uploadedFile.getBytes();
FileOutputStream outputStream = null;
try {
String filePath = System.getProperty("java.io.tmpdir") + "" + fileName;
outputStream = new FileOutputStream(new File(filePath));
outputStream.write(bytes);
outputStream.close();
readExcelFile(filePath);
} catch (Exception ex) {
}
In the above code Iam uploading the file using tomahawk after uploading storing the file in a temporary location.And from there i will be reading using poi.
public static void readExcelFile(String fileName)
{
try{
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(myFileSystem);
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
for(Row row : sheet) {
//u can read the file contents by iterating.
}
} catch (Exception ex) {
}
}

- 287
- 6
- 22