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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:18:18+00:00 2026-05-28T00:18:18+00:00

I’ve written a download Servlet to return a file based on a messageID parameter.

  • 0

I’ve written a download Servlet to return a file based on a messageID parameter. Below is the doGet method.

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // This messageID would be used to get the correct file eventually
    long messageID = Long.parseLong(request.getParameter("messageID"));
    String fileName = "C:\\Users\\Soto\\Desktop\\new_audio1.amr";
    File returnFile = new File(fileName);


    ServletOutputStream out = response.getOutputStream();
    ServletContext context = getServletConfig().getServletContext();
    String mimetype = context.getMimeType("C:\\Users\\Soto\\Desktop\\new_audio1.amr");

    response.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
    response.setContentLength((int)returnFile.length());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + "new_audio.amr" + "\"");

    FileInputStream in = new FileInputStream(returnFile);
    byte[] buffer = new byte[4096];

    int length;
    while((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
}

I then wrote some code to retrieve the file.

String url = "http://localhost:8080/AudioFileUpload/DownloadServlet";
    String charset = "UTF-8";

    // The id of the audio message requested
    String messageID = "1";

    //URLConnection connection = null;


    try {   
        String query = String.format("messageID=%s", URLEncoder.encode(messageID, charset));
        //URLConnection connection;
        //URL u = new URL(url + "?" + query);

        //connection = u.openConnection();
        //InputStream in = connection.getInputStream();

        HttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);


        HttpGet httpGet = new HttpGet(url + "?" + query);
        HttpResponse response = httpClient.execute(httpGet);

        System.out.println(response.getStatusLine());

        InputStream in = response.getEntity().getContent();

        FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Soto\\Desktop\\new_audio2.amr"));

        byte[] buffer = new byte[4096];
        int length; 
        while((length = in.read(buffer)) > 0) {
            fos.write(buffer, 0, length);
        }
        //connection = new URL(url + "?" + query).openConnection();
        //connection.setRequestProperty("Accept-Charset", charset);

        //InputStream response = connection.getInputStream();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Now this code works fine. I can download the audio file and it works correctly. What I want to know is how to, if possible, get the name of the file as it is downloaded instead of giving it my own name. Also, is it possible to get the file without having to read from the stream (maybe some library that does it for you)? I kind of want to hide the dirty stuff.

Thanks

  • 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-28T00:18:18+00:00Added an answer on May 28, 2026 at 12:18 am

    With attachment, the file will be served with the provided name properly. When inline, browsers seem to ignore filename, and usually give the servletname part of the URL as default name when saving the inline contents.

    You could try mapping that URL to an appropriate filename, if that is suitable.

    Here’s a SO related question: Securly download file inside browser with correct filename

    You may also find this link useful: Filename attribute for Inline Content-Disposition Meaningless?

    I think you cannot download file without streaming. For I/O you must use stream.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
Does anyone know how can I replace this 2 symbol below from the string
I have a reasonable size flat file database of text documents mostly saved in

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.