jueves, 28 de mayo de 2009

ICEfaces 1.8.0 + <ice:outputResource/> (english version)

Hi

I’m going to try to explain how to show a list of file from a folder so you can download using the new <ice:outputResource/> of ICEfaces 1.8.0

In the component-showcase example you can download just one file, and I wanted to show a list of files from a folder and download it all. After thinking on it a lot and with the help of lmaciass from the ICEfaces forum I devoloped a working example, and I’m going to try to explain.

Step 1:

Create a class with the information of the file and the resoruce to create, Resource (import com.icesoft.faces.context.Resource;):

public class ResourceBean {

Resource resource;
String fileName;
String fileDescription;
/**
* @param resource
* @param fileName
* @param fileDescription
*/
public ResourceBean(Resource resource, String fileName,
String fileDescription) {

this.resource = resource;
this.fileName = fileName;
this.fileDescription = fileDescription;
}
Añadir los getters/setters que faltan y listo
........
}


Step 2:

Create a class to read from the folder and create a list (to show in datatable) of ResourceBean objects. Each of this object will contain the information to download.


public class RecursoDescarga {

// Lista con cada uno de los recursos
private List resourceBeanList = null;
// Recurso independiente para añadir a la lista de recursos
private ResourceBean resourceBean = null;


public RecursoDescarga() {
init();
}

private void init() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
//UPLOAD_PATH2 es algo así como "upload/";
//Cada uno tendrá que modificarlo donde lo tengo, en mi caso justo bajo el WebContent
String basePathFile = session.getServletContext().getRealPath(
Rutas.UPLOAD_PATH2);

File dir = new File(basePathFile);
List list = getFileListing(dir);

ArrayList items = new ArrayList();
for (int i = 0; i < list.size(); i++) {
String pdfFileName = ((File) list.get(i)).getName();
try {

resourceBean = downloadFile(basePathFile, pdfFileName, "");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
items.add(resourceBean);
}
resourceBeanList = items;

Step 3:

The downloadFile method:

public ResourceBean downloadFile(String path, String fileName,
String fileDescription) throws IOException,
IllegalArgumentException {

if (path == null || path.equals("") || fileName == null
|| fileName.equals("")) {
throw new IllegalArgumentException("Argumento Ilegal");
}
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();

Resource res = new ByteArrayResource(toByteArray(ec
.getResourceAsStream(Rutas.UPLOAD_PATH4 + "/" + fileName)));

return new ResourceBean(res, fileName, fileDescription);

}

public static byte[] toByteArray(InputStream input) throws IOException {

ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int len = 0;
while ((len = input.read(buf)) > -1)
output.write(buf, 0, len);
return output.toByteArray();
}

Step 4:

Finally the two method to read the files from the folder:

public List getFileListing(File dir) {
List list = getFileListingNoSort(dir);
Collections.sort(list);

return list;

}

private List getFileListingNoSort(File dir) {
List list = new ArrayList();
File[] filesAndDirs = dir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);

for (File file : filesDirs) {
if (file.isFile()) {
list.add(file);
}
}

return list;
}


Step 5:

jspx page

<dataTable id="downloadFiles"
value="#{recursoDescarga.resourceBeanList}" var="resources">
<ice:column>
<f::facet name="header">
<ice:outputText style="text-align:left;"
value="#{msgs['nombre.de.archivo']}" />
</f::facet>
<ice:outputText value="#{resources.fileName}" />
</ice:column>
<ice:column style="text-align:center;">
<f::facet name="header">
<ice:outputText value="#{msgs['descargar']}" />
</f::facet>

<ice:outputResource label="#{msgs['descargar']}"
resource="#{resources.resource}"
image="./xmlhttp/images/LogoPDF.gif" mimeType="application/pdf"
fileName="#{resources.fileName}" attachment="true"
shared="false" style="width:150px" />
</ice:column>

<ice:column>
< f::facet name="header">
<ice:outputText value="#{msgs['descripcion']}" />
</f::facet>
<ice:outputText value="#{resources.fileDescription }" />
</ice:column>
</ice:dataTable>


Step 6:

Declare RecursoDescarga in faces-config-bean.xml


I hope it Works

Thanks

10 comentarios:

  1. A little question, how would you do if you want to do "something else", when user downloads a file, i.e., download the resource and enable another button...

    Thanks

    ResponderEliminar
  2. Hi.

    I don't understand exactly what you need to do, could you give me more details

    thanks

    ResponderEliminar
  3. hola david, sabes, he estado intentando de hacer esto en icefaces hace mucho tiempo y no lo logro hacer, es lo útimo que me falta par mi proyecto, y he revisado en todos lados y no puedo resolverlo, puedes enviarme ese proyecto de inputfile??(he bajado tutoriales de todos lados y no me resulta, bueno tampocoo soy una máquina programadora)

    saludos

    matias (villalon.matias@gmail.com)

    ResponderEliminar
  4. Hola matias

    Cuando tenga un ratito busco un proyectito que tengo por ahí y te lo paso. Si me despisto, me lo recuerdas.

    Salu2

    ResponderEliminar
  5. hola david:

    te lo agradecería mucho, ya que llevo aproximadamente 2 meses intentando sacarlo y ya no se que más hacer. hablamos

    saludos
    matias

    ResponderEliminar
  6. Hola, tienes código donde salga esto, ya q a mi tampoco me sale :S

    gracias

    ResponderEliminar
  7. Hola Ricardo.

    Dime exactamente que necesitas hacer y si te puedo ayudar te lo paso sin problemas..

    Salu2

    ResponderEliminar
  8. necesito hacer esto mismo que sale en el tutorial, tener una datatable con archivos de un ficheroy poder descargarlo, he seguido todos los pasos (como 6 veces) y me sale el datatable en blanco :S, si puedieras ayudarme david plz :)

    ResponderEliminar
  9. este es mi mail david ricardoulloac@gmail.com
    ojalá puedas ayudarme, saludos.

    ResponderEliminar