• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Pipe a remote file to response

New Here ,
May 20, 2024 May 20, 2024

Copy link to clipboard

Copied

How can I pipe a remote file directly to the response, without having to read the file in the memory or writing it to disk on the server? The remote file can be huge and I'm not going to do anything to the file itself. If possible I'd like to be able to set the basic headers like 'content-type' and 'content-length'. 

Views

500

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

You can do it using three lines of code:

<cfhttp  method="get" getasbinary="yes" result="httpResult" charset="utf-8" url="url_of_remote_file">
<!--- Here, ".ext" stands for an arbitrary file extension. Specify the one you want ---> 
<cfheader name="content-disposition" value="inline;filename=binaryfile.ext">
<!--- Application/octet-stream is the default MIME type for binary files. ---> 
<cfcontent type="application/octet-stream" variable="#httpresult.filecontent#">

Example:

<cfhttp  method="get" getasbinary="yes" result="httpResult" charset="utf-8" url="https://community.adobe.com/t5/image/serverpage/image-id/694431i786EBFDC18FC0D6D/image-size/original?v=v2&px=-1">
<!--- I have used JPG extension because I know the URL points to a pivture --->
<cfheader name="content-disposition" value="inline;filename=binaryfile.jpg">
<cfcontent type="application/octet-stream" variable="#httpresult.filecontent#">

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

@BKBK Thank you for your reply. 

But isn't that cfhttp will hold the entire remote file in-memory? I have files in the hundreds of MB hence I'd like to avoid holding the files in-memory or writing them to disk. More like "piping" the remote file to the response directly. If CF can't do this, maybe I can tap on Java, but what package or library to use? TIA

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

Ah, thanks for the clarification. Yes, what you have clarified can be done in ColdFusion.

 

You've already guessed what I, too, have in mind: using Java. The good news is, if you can do this in Java, tehn you can do it in ColdFusion. The challenge is then to convert the Java code to CFML.

 

Here is some sample code, courtesy of ChatGPT:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/download")
public class FileDownloadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Path to the file to be downloaded
        String filePath = "C:/path/to/your/file/example.pdf";
        File file = new File(filePath);

        // Set the content type
        response.setContentType("application/pdf"); // Change this to the appropriate MIME type
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
        response.setHeader("Content-Length", String.valueOf(file.length()));

        // Stream the file content directly to the response output stream
        try (FileInputStream fileInputStream = new FileInputStream(file);
             OutputStream responseOutputStream = response.getOutputStream()) {
             
            byte[] buffer = new byte[8192];
            int bytesRead;

            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                responseOutputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}

ChatGPT Explanation:

  1. Servlet Setup: The servlet is annotated with @WebServlet to define the URL pattern. In this example, it listens to /download.
  2. File Path: The path to the file to be streamed is specified.
  3. Response Headers:
    • Content-Type is set to the MIME type of the file (e.g., application/pdf).
    • Content-Disposition is set to display the file inline with the filename.
    • Content-Length is set to the size of the file.
  4. Streaming the File:
    • A FileInputStream is created to read the file.
    • The response output stream (response.getOutputStream()) is used to write the file content to the response.
    • A buffer is used to read the file in chunks (8 KB in this example) to avoid loading the entire file into memory.

This approach ensures that the file is streamed in manageable chunks, minimizing memory usage and avoiding the need to load the entire file into memory.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

Should you decide to convert the Java code into CFML, you could return to the forum as often as you need for feedback.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

@BKBK Thanks for the heads up!
That's a lot to process, coming from a CF developer with minimal Java experience. 
I'll give it a shot, at least there's some clear direction now. Thanks again! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

You could always ask ChatGPT to rewrite that Java code for use in ColdFusion. 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

@sdsinc_pmascari That's a great idea 😀
OMG AI is getting my job... 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2024 May 24, 2024

Copy link to clipboard

Copied

quote

@sdsinc_pmascari 
OMG AI is getting my job... 


By @charlieCF

See TED talk "With AI, anyone can be a coder now", by the CEO of GitHub, no less.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 24, 2024 May 24, 2024

Copy link to clipboard

Copied

LATEST

@BKBK Nice sharing 👍

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Copy link to clipboard

Copied

quote

You could always ask ChatGPT to rewrite that Java code for use in ColdFusion. 🙂


By @sdsinc_pmascari

Indeed, @sdsinc_pmascari .

I did ask ChatGPT to give me the CFML equivalent of the above Java code. The result, though good, needed additional work. For example to correct the syntax and to minimize memory use.

 

Nevertheless, ChatGPT's CFML code is a good starting point.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation