Ver Mensaje Individual
  #8 (permalink)  
Antiguo 10/06/2009, 04:39
Jeibi
 
Fecha de Ingreso: noviembre-2007
Ubicación: Madrid
Mensajes: 96
Antigüedad: 16 años, 6 meses
Puntos: 2
Respuesta: ¿Cómo leo un elemento type="file" en un servlet?

Esque se ve que tu llevas mas años en esto.
Te agradezco tu ayuda.

Les pongo como lo solucioné por si a alguien le puede ayudar esta otra alternativa. Lo que hago es leer el archivo que envia el usuario desde su local y lo copio en el servidor para luego hacer lo que se necesite:

Código:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
		ServletContext context = (ServletContext) this.getServletContext();
		String ruta = context.getRealPath("/") + "xml\\";   //<--- aquí la ruta donde quiero guardarlo.
		MultipartParser mp = new MultipartParser(req, 10*1024*1024);
		Part part;
		
		while((part = mp.readNextPart())!=null){
			if (part.isFile()){//Es un fichero.
				FilePart filepart = (FilePart) part;
				InputStream is = filepart.getInputStream();
				StringWriter sw = new StringWriter();
	
				int tempo = is.read();
				while (tempo != -1 ) {
					sw.write(tempo);
					tempo = is.read();					
				}
				File dir = new File(ruta + filepart.getFileName());
				stringToFile(sw.toString(),dir);
			}
		}
	}
	
	/**
	 * Función que graba en un archivo el string que ha leido del fichero xml enviado por el usuario
	 * @param archivo 
	 * @param dir
	 */
	public void stringToFile(String archivo, File dir){
		FileWriter fwriter = null;
		BufferedWriter bwriter = null;
		try {
			fwriter = new FileWriter(dir);
			bwriter = new BufferedWriter(fwriter);
			bwriter.write(archivo);
			bwriter.close();
			fwriter.close();			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Espero les sirva a alguien.