commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matthias Wessendorf" <maili...@matthias-wessendorf.de>
Subject [FileUpload] Utilclass for storing files in a specified directory
Date Mon, 24 May 2004 10:36:00 GMT
I developed a class, that contains two methods, that store files to a
specified  dir.

one methode takes only FormFile as argument, it loads that path for
storing form a properties-file.

the second takes FormFile and a String, which contains the desired path
for storing,
if not a properties-file is given ...

would it be usefull, to have such a util-class directly in
FileUpload-Project ?
(i didn't figured out a methode like this...)

<snip>

	/**
	 * 
	 * Saves a Jakarta FormFile to a confiured place.
	 * 
	 * @param file - the FormFile to store
	 * @return place of file
	 */	public static String saveFile(FormFile file){
		String retVal = null;
		
		try {
		//retrieve the file data
			ByteArrayOutputStream baos = new
ByteArrayOutputStream();
			InputStream stream = file.getInputStream();
			Properties props = new Properties();
			
			//properties must exist
	
props.load(Thread.currentThread().getContextClassLoader().getResourceAsS
tream("fileupload.properties"));
			String place= props.getProperty("uploadPath");

			if(!place.endsWith("/"))
				place = new
StringBuffer(place).insert(place.length(),"/").toString();
			retVal = place+file.getFileName();
			
		//write the file to the file specified
			OutputStream bos = new FileOutputStream(retVal);
			int bytesRead = 0;
			byte[] buffer = file.getFileData();
			while ((bytesRead = stream.read(buffer)) != -1)
{
				bos.write(buffer, 0, bytesRead);
			}
			bos.close();
		
		//close the stream
			stream.close();
		}
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
			
		
		return retVal;
	}
	
	/**
	 * 
	 * Saves a Jakarta FormFile to a desired place.
	 * 
	 * @param file - the FormFile to store
	 * @param place - the desired place for the file
	 * @return place of file
	 */
	public static String saveFile(FormFile file, String place) {
		String retVal = null;
		
		try {
		//retrieve the file data
			ByteArrayOutputStream baos = new
ByteArrayOutputStream();
			InputStream stream = file.getInputStream();

			Properties props = new Properties();
			
			//properties must exist
	
props.load(Thread.currentThread().getContextClassLoader().getResourceAsS
tream("ecards.properties"));
			String tomcatVerzeichnis=
props.getProperty("uploadPath");

			

			retVal = tomcatVerzeichnis+place;
			
			System.out.println("FILE: "+retVal);
			
		//write the file to the file specified
			OutputStream bos = new FileOutputStream(retVal);
			int bytesRead = 0;
			byte[] buffer = file.getFileData();
			while ((bytesRead = stream.read(buffer)) != -1)
{
				bos.write(buffer, 0, bytesRead);
			}
			bos.close();
		
		//close the stream
			stream.close();
		}
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
			
		
		return retVal;
	}


</snip>

Cheers, Matze
--
Matthias We├čendorf
Aechterhoek 18
D-48282 Emsdetten
Germany
Email: matthias AT wessendorf DOT net
URL: http://www.wessendorf.net


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message