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

  • Home
  • SEARCH
  • 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 6016201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:56:56+00:00 2026-05-23T02:56:56+00:00

I am sending a file to the browser in a servlet. The highest JDK

  • 0

I am sending a file to the browser in a servlet. The highest JDK I can use is 1.4.2, and I also have to retrieve the file via a URL. I am also trying to use “guessContentTypeFromStream”, but I keep getting null which raises an exception when used in the code sample below. I currently have to hard code or work out the content-type myself.

What I would like to know is, how I can re-factor this code so the file transmission is as fast as possible and also use guessContentTypeFromStream ? (Note “res” is HttpServletResponse).

URL servletUrl = new URL(sFileURL); 
URLConnection conn = servletUrl.openConnection();
int read;
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());

String sContentType =conn.guessContentTypeFromStream(conn.getInputStream());

res.setContentType(sContentType);
//res.setContentType("image/jpeg");

PrintWriter os = res.getWriter();

while((read = bis.read()) != -1){
    os.write(read);
}
//Clean resources
os.flush();
  • 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-23T02:56:56+00:00Added an answer on May 23, 2026 at 2:56 am

    This is how you normally read/writes data.

    in = new BufferedInputStream(socket.getInputStream(), BUFFER_SIZE);
    byte[] dataBuffer = new byte[1024 * 16];
    int size = 0;
    while ((size = in.read(dataBuffer)) != -1) {
        out.write(dataBuffer, 0, size);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a servlet that write a pdf file as a ByteArrayOutputStream to the
I am sending a file from ASP.NET Page to the browser. To properly send
On the site I am developing, I have a file input that users can
I am sending to the browser a request to save a file with the
Sending a file/key to Amazon S3 via a html form and HTTP Post is
I need to empty an LDF file before sending to a colleague. How do
I'm sending some data over AJAX to a PHP file. It's about filter options.
M sending the parameters from the jsp page, the code is <s:url id=url action=searchAction>
I am trying to use hashing for a test case in tomcat-users.xml. (I plan
I am sending a file to the user using a HttpHandler. On all browsers,

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.