Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 32695 invoked from network); 24 May 2004 16:34:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 24 May 2004 16:34:28 -0000 Received: (qmail 92096 invoked by uid 500); 24 May 2004 10:38:02 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 90439 invoked by uid 500); 24 May 2004 10:37:40 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 88638 invoked by uid 98); 24 May 2004 10:37:11 -0000 Received: from mailings@matthias-wessendorf.de by hermes.apache.org by uid 82 with qmail-scanner-1.20 (clamuko: 0.70. Clear:RC:0(212.227.126.188):. Processed in 0.21172 secs); 24 May 2004 10:37:11 -0000 X-Qmail-Scanner-Mail-From: mailings@matthias-wessendorf.de via hermes.apache.org X-Qmail-Scanner: 1.20 (Clear:RC:0(212.227.126.188):. Processed in 0.21172 secs) Received: from unknown (HELO moutng.kundenserver.de) (212.227.126.188) by hermes.apache.org with SMTP; 24 May 2004 10:37:10 -0000 Received: from [212.227.126.208] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1BSCpR-0002ul-00 for commons-dev@jakarta.apache.org; Mon, 24 May 2004 12:37:09 +0200 Received: from [217.255.2.191] (helo=fumakilla) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1BSCpR-0001MF-00 for commons-dev@jakarta.apache.org; Mon, 24 May 2004 12:37:09 +0200 From: "Matthias Wessendorf" To: Subject: [FileUpload] Utilclass for storing files in a specified directory Date: Mon, 24 May 2004 12:36:00 +0200 Message-ID: <004301c4417a$e869a630$6402a8c0@fumakilla> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2616 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Importance: Normal X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:157ed430dbf2887568e54eb61bfb58ed X-Spam-Rating: hermes.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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...) /** *=20 * Saves a Jakarta FormFile to a confiured place. *=20 * @param file - the FormFile to store * @return place of file */ public static String saveFile(FormFile file){ String retVal =3D null; =09 try { //retrieve the file data ByteArrayOutputStream baos =3D new ByteArrayOutputStream(); InputStream stream =3D file.getInputStream(); Properties props =3D new Properties(); =09 //properties must exist =09 props.load(Thread.currentThread().getContextClassLoader().getResourceAsS tream("fileupload.properties")); String place=3D props.getProperty("uploadPath"); if(!place.endsWith("/")) place =3D new StringBuffer(place).insert(place.length(),"/").toString(); retVal =3D place+file.getFileName(); =09 //write the file to the file specified OutputStream bos =3D new FileOutputStream(retVal); int bytesRead =3D 0; byte[] buffer =3D file.getFileData(); while ((bytesRead =3D stream.read(buffer)) !=3D -1) { bos.write(buffer, 0, bytesRead); } bos.close(); =09 //close the stream stream.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } =09 =09 =09 return retVal; } =09 /** *=20 * Saves a Jakarta FormFile to a desired place. *=20 * @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 =3D null; =09 try { //retrieve the file data ByteArrayOutputStream baos =3D new ByteArrayOutputStream(); InputStream stream =3D file.getInputStream(); Properties props =3D new Properties(); =09 //properties must exist =09 props.load(Thread.currentThread().getContextClassLoader().getResourceAsS tream("ecards.properties")); String tomcatVerzeichnis=3D props.getProperty("uploadPath"); =09 retVal =3D tomcatVerzeichnis+place; =09 System.out.println("FILE: "+retVal); =09 //write the file to the file specified OutputStream bos =3D new FileOutputStream(retVal); int bytesRead =3D 0; byte[] buffer =3D file.getFileData(); while ((bytesRead =3D stream.read(buffer)) !=3D -1) { bos.write(buffer, 0, bytesRead); } bos.close(); =09 //close the stream stream.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } =09 =09 =09 return retVal; } Cheers, Matze -- Matthias We=DFendorf 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