Arquivos no FileUpload

2 respostas
V

Olá pessoal,

Estou usando o componente file upload do richfaces e gostaria de saber o seguinte,

Antes de redirecionar para uma próxima página, gostaria de pegar os nomes dos arquivos que estão armazenados no componente.

Alguém sabe ou ja viu coisa parecida de como fazer isso?

Obrigada.

2 Respostas

D

Bom dia,

No meu caso eu utilizo um um objeto “fileUpload” que contem uma lista de nomes de cada arquivo carregado.
Poderia explicar um pouco melhor o que deseja fazer em sua aplicação, assim poderei dar uma ajuda melhor.

[]

V

Olá .

To enviando os meus códigos pra vc dar uma olhada

Minha classe FileUpload

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package util;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

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

/**
*

  • @author t_828348
    
    */
    
    public class FileUploadBean {
    
    private ArrayList files = new ArrayList();
    
    private int uploadsAvailable = 5;
    
    private boolean autoUpload = true;
    
    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 getFiles() {
    
    return files;
    
    }
    
    public void setFiles(ArrayList 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;
}

Minha classe File
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package util;

/**
*

  • @author t_828348
    */
    public class File {

    private String Name;
    
    private String mime;
    
    private long length;
    
    private byte[] data;
    
    public byte[] getData() {
    
    return data;
    
    }
    
    public void setData(byte[] data) {
    
    this.data = data;
    
    }
    
    public String getName() {
    
    return Name;
    
    }
    
    public void setName(String name) {
    
    Name = name;
    
    int extDot = name.lastIndexOf(.);
    
    if (extDot > 0) {
    
    String extension = name.substring(extDot + 1);
    
    if (txt.equals(extension)) {
    
    mime = image/bmp;
    
    } else if (jpg.equals(extension)) {
    
    mime = image/jpeg;
    
    } else if (gif.equals(extension)) {
    
    mime = image/gif;
    
    } else if (png.equals(extension)) {
    
    mime = image/png;
    
    } else {
    
    mime = image/unknown;
    
    }
    
    }
    
    }
    
    public long getLength() {
    
    return length;
    
    }
    
    public void setLength(long length) {
    
    this.length = length;
    
    }
    
    public String getMime() {
    
    return mime;
    
    }
    
    }
    

Minha página

<?xml version="1.0" encoding="ISO-8859-1"?>

<jsp:root version=“2.1” xmlns:a4j=“http://richfaces.org/a4j
xmlns:f=“http://java.sun.com/jsf/core
xmlns:h=“http://java.sun.com/jsf/html
xmlns:jsp=“http://java.sun.com/JSP/Page
xmlns:rich=“http://richfaces.org/rich
xmlns:c=“http://java.sun.com/jsp/jstl/core
xmlns:webuijsf=“http://www.sun.com/webui/webuijsf”>
<jsp:directive.page contentType=“text/html;charset=ISO-8859-1” pageEncoding=“ISO-8859-1”/>
<f:view>
<webuijsf:page id=“page1”>
<webuijsf:html id=“html1”>
<webuijsf:head id=“head1”>
<webuijsf:link id=“link1” url="/resources/stylesheet.css"/>
</webuijsf:head>
<webuijsf:body id=“body1” style="-rave-layout: grid">
<webuijsf:form id=“form1”>


<jsp:directive.include file=“header.jspf”/>

<h:panelGrid columnClasses=“top,top” columns=“2” style=“left: 50px; top: 200px; position: absolute”>
<rich:fileUpload acceptedTypes="" allowFlash="#{testeUpload.fileUpBean.useFlash}"
fileUploadListener="#{testeUpload.fileUpBean.listener}"
id=“upload” immediateUpload="#{testeUpload.fileUpBean.autoUpload}"
maxFilesQuantity="#{testeUpload.fileUpBean.uploadsAvailable}">
<a4j:support event=“onuploadcomplete” reRender=“info”/>
</rich:fileUpload>
<h:panelGroup id=“info”>
<rich:dataGrid columns=“1” rowKeyVar=“row” value="#{testeUpload.fileUpBean.files}" var=“file”>
<rich:panel bodyClass=“rich-laguna-panel-no-header”>
<h:panelGrid columns=“2”>
<a4j:mediaOutput cacheable=“false” createContent="#{testeUpload.fileUpBean.paint}" element=“img”
mimeType="#{testeUpload.file.mime}" style=“width:100px; height:100px;” value="#{row}">
<f:param name=“time” value="#{testeUpload.fileUpBean.timeStamp}"/>
</a4j:mediaOutput>
<h:panelGrid columns=“2”>
<h:outputText value=“File Name:”/>
<h:outputText value="#{testeUpload.file.name}"/>
<h:outputText value=“File Length(bytes):”/>
<h:outputText value="#{testeUpload.file.length}"/>
</h:panelGrid>
</h:panelGrid>
</rich:panel>
</rich:dataGrid>
<rich:spacer height=“3”/>


<a4j:commandButton action="#{testeUpload.fileUpBean.clearUploadData}" reRender=“info, upload”
rendered="#{testeUpload.fileUpBean.size>0}" value=“Clear Uploaded Data”/>
</h:panelGroup>
</h:panelGrid>
<h:commandButton action="#{testeUpload.fileUpBean.getFiles}" style=“position: absolute; left: 576px; top: 552px” value=“Voltar”/>
</webuijsf:form>
</webuijsf:body>
</webuijsf:html>
</webuijsf:page>
</f:view>
</jsp:root>

Meu arquivo .xml

<?xml version="1.0" encoding="UTF-8"?> javax.faces.STATE_SAVING_METHOD server facelets.REFRESH_PERIOD 2 facelets.DEVELOPMENT true com.sun.faces.validateXml true com.sun.faces.verifyObjects true UploadFilter com.sun.webui.jsf.util.UploadFilter The maximum allowed upload size in bytes. If this is set to a negative value, there is no maximum. The default value is 1000000. maxSize 1000000 The size (in bytes) of an uploaded file which, if it is exceeded, will cause the file to be written directly to disk instead of stored in memory. Files smaller than or equal to this size will be stored in memory. The default value is 4096. sizeThreshold 4096
</filter>
    <filter>
        <display-name>RichFaces Filter</display-name>
        <filter-name>richfaces</filter-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
     <!--TESTE-->
        <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>false</param-value>
        </init-param>
    <!-- -->
        <init-param>
            <param-name>maxRequestSize</param-name>
            <param-value>80000000</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>UploadFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <filter-mapping>
        <filter-name>richfaces</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <init-param>
            <param-name>javax.faces.LIFECYCLE_ID</param-name>
            <param-value>com.sun.faces.lifecycle.PARTIAL</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>ExceptionHandlerServlet</servlet-name>
        <servlet-class>com.sun.errorhandler.ExceptionHandler</servlet-class>
        <init-param>
            <param-name>errorHost</param-name>
            <param-value>localhost</param-value>
        </init-param>
        <init-param>
            <param-name>errorPort</param-name>
            <param-value>24444</param-value>
        </init-param>
    </servlet>
    <servlet>
        <servlet-name>ThemeServlet</servlet-name>
        <servlet-class>com.sun.webui.theme.ThemeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ExceptionHandlerServlet</servlet-name>
        <url-pattern>/error/ExceptionHandler</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ThemeServlet</servlet-name>
        <url-pattern>/theme/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
        30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/testeUpload.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <exception-type>javax.servlet.ServletException</exception-type>
        <location>/error/ExceptionHandler</location>
    </error-page>
    <error-page>
        <exception-type>java.io.IOException</exception-type>
        <location>/error/ExceptionHandler</location>
    </error-page>
    <error-page>
        <exception-type>javax.faces.FacesException</exception-type>
        <location>/error/ExceptionHandler</location>
    </error-page>
    <error-page>
        <exception-type>com.sun.rave.web.ui.appbase.ApplicationException</exception-type>
        <location>/error/ExceptionHandler</location>
    </error-page>
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jspf</url-pattern>
            <is-xml>true</is-xml>
        </jsp-property-group>
    </jsp-config>
</web-app>

Não to conseguindo pegar o nome dos arquivos que estão sendo carregados no grid

To tentando dessa forma

System.out.println("-------");

Iterator i = fileUpBean.getFiles().iterator();

    while (i.hasNext()) {
        FileUploadBean arquivo = (FileUploadBean) i.next();
        System.out.println(arquivo.getFiles());
    }

Mas não consigo pegar nada.

Obrigada.

}

Criado 10 de agosto de 2009
Ultima resposta 11 de ago. de 2009
Respostas 2
Participantes 2