Foros del Web » Programación para mayores de 30 ;) » Java »

Ayuda.

Estas en el tema de Ayuda. en el foro de Java en Foros del Web. HOLA a todos.....por favor necesito ayuda.... Estoy haciendo un jsp para subir archivos a un fichero en mi disco C, lo que quiero saber es ...
  #1 (permalink)  
Antiguo 28/06/2010, 10:29
Avatar de chicocuas  
Fecha de Ingreso: septiembre-2009
Mensajes: 4
Antigüedad: 14 años, 7 meses
Puntos: 0
Ayuda.

HOLA a todos.....por favor necesito ayuda....

Estoy haciendo un jsp para subir archivos a un fichero en mi disco C, lo que quiero saber es como puedo hacer para al momento de que estos archivos se adjuntan, se guarde un registro en la base de datos.

lo otro es que como hago para mediante un link o algo pueda e el mismo jsp poder bajarme el/los archivos que subí al fichero en el disco C.

Estoy utilizando el componente de richfaces para subir archivos:

<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<head>
<title>adjunto</title>
<style>
.top {
vertical-align: top;
}
.info {
height: 202px;
overflow: auto;
}</style>
</head>

<f:view>
<center>
<h:form>
<h:panelGrid columns="2" columnClasses="top,top">
<rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
maxFilesQuantity="#{fileUploadBean.uploadsAvailabl e}"
immediateUpload="true"
id="upload" allowFlash="false" >
<a4j:support event="onuploadcomplete" reRender="info" />
</rich:fileUpload>

<h:panelGroup id="info">
<rich:panel id="infos" header="Subir archivos">
<h:outputText 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">
<h:outputText value="File Name:" />
<h:outputText value="#{file.name}" />
<h:outputText value="File Length(bytes):" />
<h:outputText 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="Guardar los Archivos"
rendered="#{fileUploadBean.size>0}" />
</h:panelGroup>
</h:panelGrid>

<a>Descargar archivo</a>
<br></br>
<a4j:commandLink action="#{redirect.volverA}">Volver</a4j:commandLink>
</h:form>
</center>
</f:view>

La clase file:
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;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}

public String getMime(){
return mime;
}
}

el backingBean:

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

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

public class FileUploadBean{

private ArrayList<File> files = new ArrayList<File>();
private int uploadsAvailable = 5;

private String parametro;
String carpetaInfTec = "c:/subidos"; //directorio a guardar archivos

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).getDa ta());
}
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() throws Exception{
for (int i=0; i < files.size();i++){
java.io.File archivo = new java.io.File(this.carpetaInfTec, files.get(i).getName());
FileOutputStream fileOutStream = new FileOutputStream(archivo);
System.out.println("archivo guardado:"+files.get(i).getName());
fileOutStream.write(files.get(i).getData());
fileOutStream.flush();
fileOutStream.close();
}
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 String getParametro() {
return parametro;
}

public void setParametro(String parametro) {
this.parametro = parametro;
}

}


Con todo esto quiero que lo que se ponga en el componente para subir el archivo se me guarde en la base de datos.....y que me ayuden con una posibilidad de como hacer para que con alguna cosa (ejemplo un link) pueda descargarme ese archivo que lo subi con el componente......Por fa ayudenme. Gracias!

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:23.