You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2004/05/24 14:32:48 UTC
DO NOT REPLY [Bug 29175] New: -
Utilclass for storing files in a specified directory
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=29175>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=29175
Utilclass for storing files in a specified directory
Summary: Utilclass for storing files in a specified directory
Product: Commons
Version: unspecified
Platform: All
OS/Version: All
Status: NEW
Severity: Enhancement
Priority: Other
Component: File Upload
AssignedTo: commons-dev@jakarta.apache.org
ReportedBy: mailings@matthias-wessendorf.de
I developed a (util)class, that contains two methods, that stores 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().getResourceAsStream("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().getResourceAsStream("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
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org