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 842041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:53:48+00:00 2026-05-15T05:53:48+00:00

I have a servlet that construct response to a media file request by reading

  • 0

I have a servlet that construct response to a media file request by reading the file from server:

 File uploadFile = new File("C:\\TEMP\\movie.mov");
 FileInputStream in = new FileInputStream(uploadFile);

Then write that stream to the response stream. My question is how do I play the media file in the webpage using embed or object tag to read the media stream from the response?

Here is my code in the servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getParameter("location"); 
    uploadFile(response); 
}

private void uploadFile(HttpServletResponse response) {
    File transferFile = new File("C:/TEMP/captured.mov"); 
    FileInputStream in = null;

    try {
        in = new FileInputStream(transferFile);
    } catch (FileNotFoundException e) {
        System.out.println("File not found"); 
    }

    try {
        System.out.println("in byes i s" + in.available());
    } catch (IOException e) {
    }

    DataOutputStream responseStream = null;

    try {
        responseStream = new DataOutputStream(response.getOutputStream());
    } catch (IOException e) {
        System.out.println("Io exception"); 
    }

    try {
        Util.copyStream(in, responseStream);
    } catch (CopyStreamException e) {
        System.out.println("copy Stream exception"); 
    }

    try {
        responseStream.flush();
    } catch (IOException e) {
    }

    try {
        responseStream.close();
    } catch (IOException e) {
    }
}

And here is html page as Ryan suggested:

<embed SRC="http://localhost:7101/movies/transferservlet" 
    WIDTH=100 HEIGHT=196 AUTOPLAY=true CONTROLLER=true LOOP=false 
    PLUGINSPAGE="http://www.apple.com/quicktime/">

Any ideas?

  • 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-15T05:53:49+00:00Added an answer on May 15, 2026 at 5:53 am

    To start, it is firing a GET request, but the servlet is listening on POST requests only. You need to do this task in the doGet() method rather than doPost().

    You also need to instruct the webbrowser what information exactly you’re sending. This is to be done with the HTTP Content-Type header. You can find here an overview of the most used content types (mime types). You can use HttpServletResponse#setContentType() to set it. In case of Quicktime .mov files, the content type ought to be video/quicktime.

    response.setContentType("video/quicktime");
    

    Further, every media format has its own way of being embedded using the <embed> and/or the <object> element. You need to consult the documentation of the media format vendor for details how to use it. In case of Quicktime .mov files, you need to consult Apple. Carefully read this document. It is well written and it handles crossbrowser inconsitenties as well. You would likely prefer to Do It the Easy Way with help of a simple JavaScript to bridge crossbrowser inconsitenties transparently.

    <script src="AC_QuickTime.js" language="javascript"> </script>
    <script language="javascript">
        QT_WriteOBJECT('movies/filename.mov' , '320', '240' , '');
    </script>
    

    That said, the posted servlet code is honestly said terrible written. Apart from the doPost() incorrectly been used, the IO resource handling is incorrect, every line has its own try/catch, exceptions are been suppressed and poor information is written to stdout, InputStream#available() is been misunderstood, the DataOutputStream is been used for no clear reason, the InputStream is never been closed, etcetera. No, that is certainly not the way. Please consult the basic Java IO and basic Java Exception tutorials to learn more about using them properly. Here’s a slight rewrite how the servlet ought to look like:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
        File file = new File("/path/to/all/movies", filename);
    
        response.setHeader("Content-Type", "video/quicktime");
        response.setHeader("Content-Length", file.length());
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
    
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
    
        try {
            input = new BufferedInputStream(new FileInputStream(file));
            output = new BufferedOutputStream(response.getOutputStream());
    
            byte[] buffer = new byte[8192];
            for (int length = 0; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
        } finally {
            if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
            if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
        }
    }
    

    Map it in web.xml as follows:

    <servlet>
        <servlet-name>movieServlet</servlet-name>
        <servlet-class>com.example.MovieServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>movieServlet</servlet-name>
        <url-pattern>/movies/*</url-pattern>        
    </servlet-mapping>
    

    The aforeposted JavaScript example shows exactly how you should use it. Just use path /movies and append the filename thereafter like so /movies/filename.mov. The request.getPathInfo() will return /filename.mov.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.