This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSF and the fly likes Uploading files with JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Uploading files with JSF" Watch "Uploading files with JSF" New topic
Author

Uploading files with JSF

Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
Hi,

How would I receive a file from a JSF component? Normally, I use to get a String object. Is there something similar I can do to get my hands on files passed in a from multipart request?

Thanks!
Garann
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

I'd suggest using something like MyFaces and using their file upload component. Much easier than implementing it yourself.

If that isn't an options. Use the FacesContext to get to the HttpServletRequest object and then use common-fileupload to get the file(s).


GenRocket - Experts at Building Test Data
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
Wow. Is there nothing in JSF or J2EE to handle file uploads? (That's nuts!) Or is this FileUpload just a better solution? Either way, that's what I'll use. Thanks for the link, and the quick response!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Originally posted by Garann Rose Means:
Wow. Is there nothing in JSF or J2EE to handle file uploads? (That's nuts!) Or is this FileUpload just a better solution? Either way, that's what I'll use. Thanks for the link, and the quick response!


Well, yes, there is. There is the difficult way of parsing the HttpServletRequest for the files. There is the not as hard way of using common-fileupload to do the work for you. And then there is the easiest way and that is using MyFaces fileupload component.
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455


If that isn't an options. Use the FacesContext to get to the HttpServletRequest object and then use common-fileupload to get the file(s).



Could you tell me how to get HttpServletRequest object from FacesContext. I looked at the API but couldn't find any direct method.

Thanks,
Rajeev.


SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Teddy Leung
Greenhorn

Joined: Nov 07, 2005
Posts: 1
http://myfaces.apache.org/tomahawk/fileUpload.html

In JSP file:
<h:form id="uploadForm" enctype="multipart/form-data">
<t:inputFileUpload id="fileupload" value="#{<SessionBean>.uploadedFile}" storage="memory" required="true"/>

In your session bean <SessionBean>:
private UploadedFile uploadedFile;
set&get...

Then you can use your uploaded file.
[ November 07, 2005: Message edited by: Teddy Leung ]
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi,

this is the code i used in my servlet for uploading files. i make use of apache commongs fileupload.



if works well in a servlet but the same is not working in the JSF , i mean if i copy and paste the above code inside doProcessButtonAction() [ this is the event invoked on clicking the button] Could anyone tell me where i went wrong ? The List size is printed as 0 in doProcessButtonAction().

Thanks,
Rajeev.
shree lk
Greenhorn

Joined: Mar 16, 2011
Posts: 14

Hello All...

I have problem with JSF file uploading.

I have uploaded excel file with ' inputFileUpload' tag of JSF and given value using BEANS as #{myBeans.myFile} to the tag. Now in backbean java file I can't able to get that file so that I can process or parse excel file. I declared it as follows
String inputFile= " #{myBeans.myFile}". But its not working. I don't know whether its right??. I also given getters & setters for 'inputFile' . But when i give direct path of the file like- String inputFile="c:/upload/test.xls" I am able to process that excel file file successfully. I have used POI to parse excel file.

Also I am able to display contents of xls from JSf form in the browser. Although I am getting it on the tomact console. I used following code to parse and display xls file contents.


Can anybody suggest solution to this. Also how to display xls file contents to browser with JSF form.

public String fileSubmit() throws IOException
{

inputFile= "c:/upload/test.xls";


//inputFile="#{attend.inputFile}";

List cellDataList = new ArrayList();



try
{
/*
* Create a new instance for FileInputStream class
*/
FileInputStream fileInputStream = new FileInputStream(inputFile);

/*
* Create a new instance for POIFSFileSystem class
*/

POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);

/*
* Create a new instance for HSSFWorkBook Class
*/

HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
HSSFSheet hssfSheet = workBook.getSheetAt(0);

/*
* Iterate the rows and cells of the spreadsheet to get all the data.
*/

Iterator rowIterator = hssfSheet.rowIterator();

while (rowIterator.hasNext())
{
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
Iterator iterator = hssfRow.cellIterator();
List cellTempList = new ArrayList();

while (iterator.hasNext())
{
HSSFCell hssfCell = (HSSFCell) iterator.next();
cellTempList.add(hssfCell);
}
cellDataList.add(cellTempList);
}
}
catch (Exception e)
{
e.printStackTrace();
}


printToConsole(cellDataList);
// List retList=printToConsole(cellDataList);

return "upLoaded";
}

private List printToConsole(List cellDataList)
{
for (int i = 0; i < cellDataList.size(); i++)
{
List cellTempList = (List) cellDataList.get(i);
for (int j = 0; j < cellTempList.size(); j++)
{
HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);

String stringCellValue = hssfCell.toString();
System.out.print(stringCellValue + "\t");




}
System.out.println();


}


return cellDataList;



}





Thanks...
pmr marreddy
Greenhorn

Joined: Jun 30, 2011
Posts: 1
I am using JSF to upload the excel file. just sharing to have a look .

1)you can use jsf fileupload control to upload any file like as
---------- your requirment will go here----

i hope this will give some input for upload excel and reading excel sheet.

thanks
pmr
 
jQuery in Action, 2nd edition
 
subject: Uploading files with JSF
 
Similar Threads
jsp (jsf) & UML Class Diagram
Can we Access FacesContext outside FacesServlet
Caching issue
Download files component
Jar files for JSF 2.0 + Primefaces