and nothing attched:

so see below

Greetings matthias

-----------------------------------
package net.wessendorf.utils.file;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.struts.upload.FormFile;

/**
 * Hilfsklasse, mit welcher aus einer Web-Anwendung
 * Daten zum Web-Server geladen werden k�nnen.
 *
 * @author Matthias Wessendorf
 */
public final class UploadFile {
        
        /**
         * 
         * Private Constructor, prevents the class for beeing
         * instanced.
         * 
         *
         */
        private UploadFile() {
                
        }
        
        /**
         * 
         * Save a Jakarta FormFile to a preconfigured place.
         * 
         * @param file
         * @return
         */
        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("ecards.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) {
                        System.out.println("Datei nicht vorhanden");
                        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();

                        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) {
                        System.out.println("File not found");
                        fnfe.printStackTrace();
                }
                catch (IOException ioe) {
                        ioe.printStackTrace();
                }
                
                        
                
                return retVal;
        }
}



-----Original Message-----
From: David Erickson [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, December 30, 2003 5:37 PM
To: Struts Users Mailing List
Subject: Re: Still having problems with File upload (multipart-formdata)


construktor ;P

----- Original Message ----- 
From: "Matthias Wessendorf" <[EMAIL PROTECTED]>
To: "'Struts Users Mailing List'" <[EMAIL PROTECTED]>
Sent: Tuesday, December 30, 2003 9:29 AM
Subject: RE: Still having problems with File upload (multipart-formdata)


Hi Patrick,
okay attched is my class

it offers only two methods.
soon more... perhaps ;-)

one stores a form-file to a place, defined in a PROPERTIES-File under
uploadPath-KEY

the other stores the file to a place which is the second-parameter ;-)

both methods are static. The class has only a private Construktor, so it
is final... :-)



With

String placeString =
getServlet().getServletContext().getRealPath("/");

you get the Path of your Struts-App
like: C:\Tomcat\webapps\myStrutsApp


so you could use the second like:

UploadFile.saveFile(myFormFile, placeString);

your file is then in root.

hope this is what you needed.

greetings

matthias

-----Original Message-----
From: Patrick Scheuerer [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 30, 2003 5:15 PM
To: Struts Users Mailing List
Subject: Re: Still having problems with File upload (multipart-formdata)


Matthias Wessendorf wrote:

> Hi Patrick,
>
> you want to get the path of /myapp
> like C:\Tomcat\webapps\myapp
> or?

That's exactly what i was trying to do!

> in an actionClass
> you now want to store
> in placeString\uploaddir
> isn�t it?

Right again :-)

> i wrote an Util-Class which managed the
> storage in my action.
> one parameter is the formfile the other the placeToStore.
> you can get it, if you want...

That would be wonderful! Thank you very much for your help!

Patrick


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




------------------------------------------------------------------------
----
----


> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to