Solved

Set filename for binary data in HttpServletResponse

Posted on 1998-10-02
11
1,462 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
Comment Utility
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
Comment Utility
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
Comment Utility
So you generate the HTML dynamically. Isn't there a field in the HTML response header that says this?

Cheers,
  Nik.
0
 
LVL 1

Author Comment

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

and

res.setHeader( "filename" , fileName );

Neither works..
0
 
LVL 4

Expert Comment

by:seguret
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:GregL
Comment Utility
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
Comment Utility
Hi again,

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

Cheers,
  Nik.
0
 
LVL 1

Author Comment

by:GregL
Comment Utility
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
Comment Utility
seguret's answer is basicly right.  Try

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

0
 
LVL 1

Author Comment

by:GregL
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now