Solved

Set filename for binary data in HttpServletResponse

Posted on 1998-10-02
11
1,507 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

691 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