Package org.kissweb

Class FileUtils

java.lang.Object
org.kissweb.FileUtils

public class FileUtils extends Object
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 Details

    • FileUtils

      public FileUtils()
  • Method Details

    • createReportFile

      public static File createReportFile(String prefix, String suffix)
      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 long
      suffix -
      Returns:
    • getHTTPPath

      public static String getHTTPPath(File f)
      Takes a previously obtained File and returns what path the front-end should use to access the file.
    • getHTTPPath

      public static String getHTTPPath(String fn)
      Takes a path to a file and returns what path the front-end should use to access the file.
    • write

      public static void write(String fileName, String str) throws IOException
      Write String to file fileName. File is created if it doesn't exist and truncated otherwise.
      Parameters:
      fileName -
      str -
      Throws:
      IOException
    • write

      public static void write(String fileName, byte[] vec) throws IOException
      Write byte array to file fileName. File is created if it doesn't exist and truncated otherwise.
      Parameters:
      fileName -
      vec -
      Throws:
      IOException
    • write

      public static void write(String fileName, Byte[] vec) throws IOException
      Write byte array to file fileName. File is created if it doesn't exist and truncated otherwise.
      Parameters:
      fileName -
      vec -
      Throws:
      IOException
    • append

      public static void append(String fileName, String str) throws IOException
      Append String to file fileName. File is created if it doesn't exist and appended otherwise.
      Parameters:
      fileName -
      str -
      Throws:
      IOException
    • append

      public static void append(String fileName, byte[] vec) throws IOException
      Append byte array to file fileName. File is created if it doesn't exist and appended otherwise.
      Parameters:
      fileName -
      vec -
      Throws:
      IOException
    • append

      public static void append(String fileName, Byte[] vec) throws IOException
      Append byte array to file fileName. File is created if it doesn't exist and appended otherwise.
      Parameters:
      fileName -
      vec -
      Throws:
      IOException
    • readFile

      public static String readFile(String fileName) throws IOException
      Read the entire contents of a file into a String.
      Parameters:
      fileName -
      Returns:
      Throws:
      IOException
    • readFileBytes

      public static byte[] readFileBytes(String fileName) throws IOException
      Read the entire contents of a file into a byte array.
      Parameters:
      fileName -
      Returns:
      Throws:
      IOException
    • readAllBytes

      public static byte[] readAllBytes(BufferedInputStream bis) throws IOException
      Read all bytes from a BufferedInputStream and return a byte array with the data. This is useful when processing an uploaded file.
      Parameters:
      bis -
      Returns:
      Throws:
      IOException
    • copy

      public static void copy(String source, String dest) throws IOException
      Copy file
      Parameters:
      source - file
      dest - file or directory
      Throws:
      IOException
    • getMimeType

      public static String getMimeType(String fname)
      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

      public static String getExtension(String fn)
      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 "."