Ver Mensaje Individual
  #9 (permalink)  
Antiguo 11/08/2011, 01:15
Avatar de atapuerkasman
atapuerkasman
 
Fecha de Ingreso: febrero-2011
Mensajes: 76
Antigüedad: 13 años, 2 meses
Puntos: 2
Respuesta: Upload imagenes con struts2

He cambiado el código, me gustaría que las imagenes subidas las guardara en Miproyecto/WebContent/images/planos

Este es el código del archivo NuevaPlanta.java (el action de struts2 )

Código:
private File myfile;
	private String myfileFileName;
	private String myfileContentType;
	private String filePathToSaveInDB;
	
	
	
	
	public String openHome(){
		return "open";
	}
	
	public String execute(){
		
		try{
			ServletContext servletContext = ServletActionContext.getServletContext();
			String path= "/index.html";
			if(path==null)
				System.out.println("Error al especificar la ruta virtual de guardado");
			//aquí va la ruta donde se subiran las imagenes
			String filePath= servletContext.getRealPath(path);
			
			File uploadDir= new File(filePath);
			//si el directorio no existe, lo crea
			if(uploadDir.exists()==false){
				uploadDir.mkdirs();
			}
			setFilePathToSaveInDB(path+"/"+ myfileFileName);
			FileUtils.copyFile(myfile, new File(uploadDir, myfileFileName));
		}catch(Exception e){
			System.out.println("Exception: "+e);
			addActionError(e.getMessage());
			return "error";
		}
		
		return "success";
	}
Me devuelve todo correcto, además las validaciones de tamaño máximo de archivo y de tipo de archivo de struts.xml funcionan perfectamente.

me gustaría saber que valor tengo que introducir en:

Código:
String path= "/index.html";
String filePath= servletContext.getRealPath(path);
Para que las imagenes subidas correctamente se almacenen en la carpeta /images/planos de mi proyecto.

Última edición por atapuerkasman; 11/08/2011 a las 01:36