Generate the Full URL for Uploaded Files on a Server

Generate the Full URL for Uploaded Files on a Server

Most commonly when you save a file in your server you will be using a context name to define the physical location in which the files are stored and also you may be using the same context name for the file paths in your database records, see the following images as examples:





This configuration allow you to shorten the URLs and also hides the important information about physical locations in your server but sometimes you may need retrieve the real URL for the files in order to process specific actions like download a file, to achieve this you can create a Source code objet in Plex that will return you the real path of a specific file, to do this please follow this instructions:

  1. In Plex creates a field called “FilePath” with this configuration
  2. Creates a source code object called “GenerateFileURL” with this configuration
  3. Add the following code to your “GenerateFileURL” source code:

    {

                    com.adcaustin.webclient.IWebApp webApp = (com.adcaustin.webclient.IWebApp)(Object)this.getApp() ;

        javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest)webApp.getFromUserStorage("javax.servlet.http.HttpServletRequest") ;

                    String fileName = &(1:).toString() ;  

        String path = fileName.replace( "\\", "/") ;

        path = path.replace( "%", "%25" ).replace( "#", "%23" ) ;

        String fileUrl = "http://" + request.getServerName() + ":" + String.valueOf(request.getServerPort()) + request.getContextPath() + "/" +path ;

                    &(2:).fromString( fileUrl ) ;

    }

  4. Call the “GenerateFileURL” source code anytime you need get the real URL for a file, remember this:
    aThe parameter “FilePath” represent your file URL that uses the context name.
    b. 
    The parameter “URL” represent the field that will store the real URL for the file.