Solved

Set filename for binary data in HttpServletResponse

Posted on 1998-10-02
11
1,480 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 200 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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