java - To upload a file using rich:fileUpload -
i'm facing problem while uploading file server, in code below can context.
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]; (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; } }
in 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; } }
the actual problem is, file created in "tempfolder/filename" leads nothing inside, it's txt size 0 bytes.
i'm not sure i'm doing wrong, welcome!
Comments
Post a Comment