使用rich:fileUpload上传文件

时间:2015-08-07 18:09:35

标签: java file-upload richfaces

我在将文件上传到服务器时遇到了一些问题,在下面的代码中您可以获得一些上下文。

view.xhtml

<h:outputText value="Subir archivo tráfico:" styleClass="label" />
<h:panelGrid >
    <rich:fileUpload
        disabled="#{not administrarFormTraficoBean.edicionHabilitada}"
        fileUploadListener="#{fileUploadBean.listener}"
        listHeight="60px"
        id="upload"
        acceptedTypes="txt"
        ontyperejected="alert('Únicamente se pueden cargar archivos en formato .txt');"
        autoclear="true"
        maxFilesQuantity="1"
        lang="es"
        doneLabel="Listo" 
        deleteLabel="Eliminar"
        serverErrorLabel="Se produjo un error al subir el archivo"
        sizeExceededLabel="El tamaño de archivo supera el tamaño permitido!"
        clearLabel="Limpiar"
        clearAllLabel="Limpiar"
        addLabel="Agregar..."
        uploadLabel="Cargar Archivo">
        <a4j:ajax event="uploadcomplete" execute="@none" render="info" />
    </rich:fileUpload>
    <h:panelGrid id="info" layout="block" >
        <rich:panel bodyClass="info">
            <f:facet name="header">
                <h:outputText value="Archivo subido" />
            </f:facet>
            <h:outputText value="No hay archivos subidos."
                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>
                        <h:panelGrid columns="2">
                            <h:outputText value="Nombre archivo:" />
                            <h:outputText value="#{file.name}" />
                            <h:outputText value="Tamaño (bytes):" />
                            <h:outputText value="#{file.length}" />
                        </h:panelGrid>
                    </h:panelGrid>
                </rich:panel>
            </rich:dataGrid>
        </rich:panel>
        <br />
    </h:panelGrid>
</h:panelGrid>

fileUploadBean.java

package uy.com.xxx.utiles.uploadTrafico;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

import uy.com.xxx.ejb.ManagerSysParm;
import uy.com.xxx.utiles.Constantes;

@ManagedBean(name = "fileUploadBean")
@SessionScoped
public class FileUploadBean {

    private ArrayList<FileTrafico> files = new ArrayList<FileTrafico>();
    @EJB
    private ManagerSysParm managerSysParm;

    public int getSize() {
        if (getFiles().size() > 0) {
            return getFiles().size();
        } else {
            return 0;
        }
    }

    public FileUploadBean() {
    }

    public void listener(FileUploadEvent event) throws Exception {
        UploadedFile item = event.getUploadedFile();
        FileTrafico file = new FileTrafico();

        file.setData(item.getData());
        file.setLength(item.getData().length);
        file.setName(item.getName());
        files.clear();
        files.add(file);

        subirArchivo(event);
    }

    public void subirArchivo(FileUploadEvent event) throws IOException {
        byte[] bytes = null;
        UploadedFile item = event.getUploadedFile();
        File file = new File(managerSysParm.buscarValorParametro(Constantes.DIRECTORIO_TEMPORAL) + item.getName());
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(managerSysParm.buscarValorParametro(Constantes.DIRECTORIO_TEMPORAL)+ item.getName());
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];

            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }

            fis.close();
            bytes = bos.toByteArray();
            fos.write(bytes);
            fos.close();

        } catch (FileNotFoundException e) {
            System.out.println("El archivo que está subiendo presenta un error: " + e);
            e.printStackTrace();
        }

    }

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

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

}
FileTrafico.java中的

public class FileTrafico {

    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="text/plain";
            }else {
                mime = "text/unknown";
            }
        }
    }
    public long getLength() {
        return length;
    }
    public void setLength(long length) {
        this.length = length;
    }

    public String getMime(){
        return mime;
    }

}

实际问题是,我得到了&#34; tempFolder / filename&#34;但是内部没有任何内容,它是一个大小为0字节的文本。

我不确定我做错了什么,欢迎任何帮助!

0 个答案:

没有答案