Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/02/2011, 04:01
mariaanie
 
Fecha de Ingreso: septiembre-2008
Mensajes: 137
Antigüedad: 15 años, 6 meses
Puntos: 0
Cargar archivo Commons File Upload JSP

Buenos días,

Tengo que cargar dos archivos desde una jsp, con la libreria Commons FileUpload:

1- Cargar un archivo, desde un formulario, a traves de un <input name="file" type="file" />, donde le indico la ruta del archivo en mi disco.

2- Cargar un .zip, entiendo que el mismo metodo que empleo para cargar un fichero, es valido para este, y luego lo descomprimo mediante java, no?

Alguien me puede explicar el metodo a seguir, he podido ver algunos ejemplos, en la web de Apache, pero no entiendo mucho, ya que vengo de PHP y me estoy iniciando en J2EE.

Ejemplo:

1-++++++++++++++++++++++++++++++++++++++++++++++++++ +++JSP

<form name="myform" action="fileuploaddemo.jsp"
method="post" enctype="multipart/form-data">
Specify your name:<br />
<input type="text" name="name" size="15"/><br />
Specify your Image:<br />
<input type="file" name="myimage"><br/>
Specify your File:<br />
<input type="file" name="myfile"><br /><br />
<input type="submit" name="Submit" value="Submit your files"/>

2- ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++

boolean isMultipart = FileUpload.isMultipartContent(request);

DiskFileUpload upload = new DiskFileUpload();

+++Pregunta: el parametro request, entiendo que contiene la variable myfile no???
List items = upload.parseRequest(request);

Iterator itr = items.iterator();

while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();

// check if the current item is a form field or an uploaded file
if(item.isFormField()) {

// get the name of the field
String fieldName = item.getFieldName();

// if it is name, we can set it in request to thank the user
if(fieldName.equals("name"))
request.setAttribute("msg", "Thank You: " + item.getString());

} else {

// the item must be an uploaded file save it to disk. Note that there
// seems to be a bug in item.getName() as it returns the full path on
// the client's machine for the uploaded file name, instead of the file
// name only. To overcome that, I have used a workaround using
// fullFile.getName().
File fullFile = new File(item.getName());
File savedFile = new File(getServletContext().getRealPath("/"),
fullFile.getName());
item.write(savedFile);
}
}


Bueno espero que me podais hechar un cable.