?
Solved

Set filename for binary data in HttpServletResponse

Posted on 1998-10-02
11
Medium Priority
?
1,521 Views
Last Modified: 2012-06-22
I have a servlet that retrieves binary data files from a database.  I can send the file to the client with no problems.  I use req.getOutputStream().  How do I set the initial filename that the user sees in the Save As dialog?
0
Comment
Question by:GregL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1225302
Won't this help?
//
saveFileDialog1 = new java.awt.FileDialog(this);
saveFileDialog1.setMode(FileDialog.SAVE);
saveFileDialog1.setTitle("Save");
saveFileDialog1.setFile("initial.file");
0
 
LVL 1

Author Comment

by:GregL
ID: 1225303
Sorry diakov,  I am looking for a way for the servlet to set the filename the client sees.  There is no client-side java..

Thanks,

GregL
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225304
So you generate the HTML dynamically. Isn't there a field in the HTML response header that says this?

Cheers,
  Nik.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:GregL
ID: 1225305
I have tried
res.setContentType( contentType + "; filename=" + fileName );

and

res.setHeader( "filename" , fileName );

Neither works..
0
 
LVL 4

Expert Comment

by:seguret
ID: 1225306
Your file will be accessed through an URL.
If you write
<a href="myServlet/toto.hqx">download the file</a>
your user will be asked to save the file "toto.hqx".


0
 
LVL 1

Author Comment

by:GregL
ID: 1225307
Sorry segurets.. I am sending the data back to client in response to a form POST.  Your answer assumes I am using a GET.  This is not a simple link to a file, it is a database transaction that packages the data and sends it back to the client.  I must return the data using a binary stream.

Thanks,

GregL
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225308
Hi again,

try setContentType("application/filename.ext") with some mime type specifications like.

Cheers,
  Nik.
0
 
LVL 1

Author Comment

by:GregL
ID: 1225309
I tried diakov's suggestion.  No luck..  Here is a snippet of my code that may help..
Hashtable h = (Hashtable)o;
String contentType = (String)h.get( "mime_type" );
String fileName = (String)h.get( "file_name" );
byte [] content = (byte [])h.get( "content" );
if( (contentType != null) && (fileName != null) && (content != null) )
{
    res.setContentType( contentType );
    res.setContentLength( content.length );
    res.setHeader( "pragma", "no-cache" );
    OutputStream out = res.getOutputStream();
    out.write( content, 0, content.length );
    out.close();
}
else
{
    res.setContentType( "text/html" );
    PrintWriter out = res.getWriter();
    out.print( "The hashtable was not valid!" );
    out.close();
}

0
 

Expert Comment

by:frank123
ID: 1225310
seguret's answer is basicly right.  Try

<form action="myServlet/toto.hqx" method=post>

0
 
LVL 1

Author Comment

by:GregL
ID: 1225311
Sorry, I found the real answer..

res.setHeader( "Content-Disposition","attachment; filename=" + fileName + ";" );

Thanks, for the help..

Greg L.
0
 
LVL 7

Accepted Solution

by:
linda101698 earned 800 total points
ID: 1225312
I am posting the solution found by GregL so it can be saved in the previously asked questions.  Please see your customer service question for an explanation.

Linda Gardner
Customer Service @ Experts Exchange

Comment From: GregL
                        Date: Tuesday, October 06 1998 - 07:56AM PDT
    I found the real answer..

     res.setHeader( "Content-Disposition","attachment; filename=" + fileName +
     ";" );

     Thanks, for the help..

     Greg L.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question