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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to