permaculture playing cards*
The moose likes JSF and the fly likes jsf file upload working code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "jsf file upload working code" Watch "jsf file upload working code" New topic
Author

jsf file upload working code

ufuk ungor
Greenhorn

Joined: Mar 04, 2011
Posts: 2
I have a jsf project and I use richfaces so I get some codes from http://livedemo.exadel.com for fileupload but its does not work.
Can you give me working codes for fileupload. I will upload especially pdf files.

thank you
ufuk ungor
Greenhorn

Joined: Mar 04, 2011
Posts: 2
by the way , I find codes about file upload from richfaces.

These codes does not work but I don't get any error.
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">

<style>
.top {
vertical-align: top;
}

.info {
height: 202px;
overflow: auto;
}
</style>
<body>
<h:form>
<h:panelGrid columns="2" columnClasses="top,top">
<rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
maxFilesQuantity="#{fileUploadBean.uploadsAvailable}" id="upload"
immediateUpload="#{fileUploadBean.autoUpload}"
acceptedTypes="jpg, gif, png, bmp"
allowFlash="#{fileUploadBean.useFlash}">
<a4j:support event="onuploadcomplete" reRender="info" />
</rich:fileUpload>
<h:panelGroup id="info">
<rich:panel bodyClass="info">
<f:facet name="header">
<hutputText value="Uploaded Files Info" />
</f:facet>
<hutputText value="No files currently uploaded"
rendered="#{fileUploadBean.size==0}" />
<rich:dataGrid columns="1" value="#{fileUploadBean.files}"
var="file" rowKeyVar="row">
<rich:panel bodyClass="rich-laguna-panel-no-header">
<h:panelGrid columns="2">
<a4j:mediaOutput element="img" mimeType="#{file.mime}"
createContent="#{fileUploadBean.paint}" value="#{row}"
style="width:100px; height:100px;" cacheable="false">
<f:param value="#{fileUploadBean.timeStamp}" name="time" />
</a4j:mediaOutput>
<h:panelGrid columns="2">
<hutputText value="File Name:" />
<hutputText value="#{file.name}" />
<hutputText value="File Length(bytes):" />
<hutputText value="#{file.length}" />
</h:panelGrid>
</h:panelGrid>
</rich:panel>
</rich:dataGrid>
</rich:panel>
<rich:spacer height="3" />
<br />
<a4j:commandButton action="#{fileUploadBean.clearUploadData}"
reRender="info, upload" value="Clear Uploaded Data"
rendered="#{fileUploadBean.size>0}" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</body>
</ui:composition>


---------------------------

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

/**
* @author Ilya Shaikovsky
*
*/
public class FileUploadBean{

private ArrayList<File> files = new ArrayList<File>();
private int uploadsAvailable = 5;
private boolean autoUpload = false;
private boolean useFlash = false;
public int getSize() {
if (getFiles().size()>0){
return getFiles().size();
}else
{
return 0;
}
}

public FileUploadBean() {
}

public void paint(OutputStream stream, Object object) throws IOException {
stream.write(getFiles().get((Integer)object).getData());
}
public void listener(UploadEvent event) throws Exception{
UploadItem item = event.getUploadItem();
File file = new File();
file.setLength(item.getData().length);
file.setName(item.getFileName());
file.setData(item.getData());
files.add(file);
uploadsAvailable--;
}

public String clearUploadData() {
files.clear();
setUploadsAvailable(5);
return null;
}

public long getTimeStamp(){
return System.currentTimeMillis();
}

public ArrayList<File> getFiles() {
return files;
}

public void setFiles(ArrayList<File> files) {
this.files = files;
}

public int getUploadsAvailable() {
return uploadsAvailable;
}

public void setUploadsAvailable(int uploadsAvailable) {
this.uploadsAvailable = uploadsAvailable;
}

public boolean isAutoUpload() {
return autoUpload;
}

public void setAutoUpload(boolean autoUpload) {
this.autoUpload = autoUpload;
}

public boolean isUseFlash() {
return useFlash;
}

public void setUseFlash(boolean useFlash) {
this.useFlash = useFlash;
}

}



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jsf file upload working code
 
Similar Threads
Problem in Retriving Value in Action class
problem in uploading a file
error during uploading files - no parametres in request
Mail with attachment using jsp
org.apache.struts.upload.FormFile question