Package org.kissweb
Class FileUtils
java.lang.Object
org.kissweb.FileUtils
Utilities to deal with files on the back-end. These are usually PDF or CSV files that
will be sent to the front-end.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAppend byte array to file fileName.static voidAppend byte array to file fileName.static voidAppend String to file fileName.static voidCopy filestatic FilecreateReportFile(String prefix, String suffix) Create a new file in a server accessible temp directory.static StringgetExtension(String fn) Obtain the file name extension.static StringgetHTTPPath(File f) Takes a previously obtained File and returns what path the front-end should use to access the file.static StringgetHTTPPath(String fn) Takes a path to a file and returns what path the front-end should use to access the file.static StringgetMimeType(String fname) Returns the MIME type based on the file name's extension.static byte[]Read all bytes from aBufferedInputStreamand return a byte array with the data.static StringRead the entire contents of a file into a String.static byte[]readFileBytes(String fileName) Read the entire contents of a file into a byte array.static voidWrite byte array to file fileName.static voidWrite byte array to file fileName.static voidWrite String to file fileName.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
createReportFile
Create a new file in a server accessible temp directory. The file returned is guaranteed to be new. The name of the file uses the supplied prefix and suffix but with a random center. The file is deleted when it is more than a day old. This provides plenty of time for the user to download a report or data file.- Parameters:
prefix- at least 3 characters longsuffix-- Returns:
-
getHTTPPath
Takes a previously obtained File and returns what path the front-end should use to access the file. -
getHTTPPath
Takes a path to a file and returns what path the front-end should use to access the file. -
write
Write String to file fileName. File is created if it doesn't exist and truncated otherwise.- Parameters:
fileName-str-- Throws:
IOException
-
write
Write byte array to file fileName. File is created if it doesn't exist and truncated otherwise.- Parameters:
fileName-vec-- Throws:
IOException
-
write
Write byte array to file fileName. File is created if it doesn't exist and truncated otherwise.- Parameters:
fileName-vec-- Throws:
IOException
-
append
Append String to file fileName. File is created if it doesn't exist and appended otherwise.- Parameters:
fileName-str-- Throws:
IOException
-
append
Append byte array to file fileName. File is created if it doesn't exist and appended otherwise.- Parameters:
fileName-vec-- Throws:
IOException
-
append
Append byte array to file fileName. File is created if it doesn't exist and appended otherwise.- Parameters:
fileName-vec-- Throws:
IOException
-
readFile
Read the entire contents of a file into a String.- Parameters:
fileName-- Returns:
- Throws:
IOException
-
readFileBytes
Read the entire contents of a file into a byte array.- Parameters:
fileName-- Returns:
- Throws:
IOException
-
readAllBytes
Read all bytes from aBufferedInputStreamand return a byte array with the data. This is useful when processing an uploaded file.- Parameters:
bis-- Returns:
- Throws:
IOException
-
copy
Copy file- Parameters:
source- filedest- file or directory- Throws:
IOException
-
getMimeType
Returns the MIME type based on the file name's extension.- Parameters:
fname- the complete file name or the extension- Returns:
- the MIME type corresponding to the file extension, or "application/octet-stream" if not found
-
getExtension
Obtain the file name extension. For example "file.pdf" will return "pdf". "" is returned if no extension is found.- Parameters:
fn- the file name- Returns:
- the extension portion of the file name without the "."
-