Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:04:16+00:00 2026-05-31T11:04:16+00:00

I am using Gralis 1.3.7. I am writing a controller that needs to get

  • 0

I am using Gralis 1.3.7. I am writing a controller that needs to get a PDF file from another server and return it to the client. I would like to do this in some reasonably efficient manner, such as the following:

class DocController {
    def view = {
        URL source = new URL("http://server.com?docid=${params.docid}");

        response.contentType = 'application/pdf';
        // Something like this to set the content length
        response.setHeader("Content-Length", source.contentLength.toString());
        response << source.openStream();
    }
}

The problem I am having is figuring out how to set the content length of the response of my controller based on the information coming back from source. I wasn’t able to find the documentation on the URL class as enhanced by grails.

What’s the best way to proceed?

Gene

EDITED: Fixed parameter values in setHeader

UPDATED 16 mar 2012 10:49 PST

UPDATED 19 March 2012 10:45 PST
Moved follow-up to a separate question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:04:17+00:00Added an answer on May 31, 2026 at 11:04 am

    You can use java.net.URLConnection object that will allow you to do a bit more detailed work with the URL.

    URLConnection connection = new URL(url).openConnection()
    
    def url = new URL("http://www.aboutgroovy.com")
    def connection = url.openConnection()
    println connection.responseCode        // ===> 200
    println connection.responseMessage     // ===> OK
    println connection.contentLength       // ===> 4216
    println connection.contentType         // ===> text/html
    println connection.date                // ===> 1191250061000
    println connection.lastModified
    
    // print headers
    connection.headerFields.each{println it}
    

    Your example should looks something like this:

    class DocController {
        def view = {
            URL source = new URL("http://server.com?docid=${params.docid}");
            URLConnection connection = source.openConnection();
    
            response.contentType = 'application/pdf';
    
            // Set the content length
            response.setHeader("Content-Length", connection.contentLength.toString());
    
            // Get the input stream from the connection
            response.outputStream << connection.getInputStream();
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am performing some mass writing in a .csv file using Groovy. More specifically,
We're writing a flash application that can download a MP3 file, convert it to
I'm following the racetrack example from Jason Rudolph's book at InfoQ , using grails-1.2.1.
Using Grails and CXF, I have published a small web service that looks like
I'm using Grails scaffolding and would like to make a change in the default
I have a Grails 2.0.0 project that was created using grails create-app . In
Using Grails 2.0.0 I am created service, that creates instances of Player class and
In my Grails GSP file I'm using the HTML meta tag: <meta http-equiv=Content-Type content=text/html;
I am using Grails 1.3.7 and cannot figure out how to get version 4.0
I'm using webflows in Grails and I'm currently writing tests for it. Now, inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.