Java webapp: adding content header to make browser โ€œsave asโ€ behavior

Even if it is not part of the HTTP 1.1 / RFC2616 web applications that want to force the resource to load (rather than display) in the browser, it can use the Content-Disposition header as follows:

 Content-Disposition: attachment; filename=FILENAME 

Even tough, it is defined only in RFC2183, and not in the HTTP 1.1 part, it works in most web browsers as needed.

So, on the client side, everything is pretty good.

However, on the server side, in my case, I have a Java webapp, and I don't know how I should set this header, especially in the following case ...

I will have a file (for example, called a "bigfile") hosted on an Amazon S3 instance (my S3 bucket should be accessible using a partial address, for example: files.mycompany.com/) so that users can access this file on files .mycompany.com / bigfile.

Now is there a way to create a servlet (or .jsp) so that the Content-Disposition header is always added when the user wants to download this file?

What would the code look like and what are it, if any?

+6
java web-applications jsp content-disposition
source share
4 answers

You would not have a URL that would be a direct link to the file. Instead, you will have a URL that leads to your servlet code (or some action code in your server structure). This, in turn, would have to access the contents of the file and dig it out to the client after setting the header. (You should also remember to deal with cache control headers, if necessary.)

The HttpServletResponse class has an API that allows you to set all the necessary headers. You must make sure that you have configured the headers before you start downloading the contents of the file, because the headers should literally be the first in the stream sent to the browser.

This is not so different from a situation where you may have a servlet that will generate downloads on the fly.

edit . I will leave it above for posterity, but I want to note that there is (or maybe) a way to transfer some HTTP headers to S3 when you store the file, so Amazon will spit them back when the file is submitted. I'm not quite sure how you will do this, and I'm not sure that Content-disposition is a headline that you can configure in this way, but I will continue to search.

+2
source share

I got this job, as Walkney noted. Instead of directly contacting the asset - in my case pdfs - it now refers to a JSP called download.jsp that takes and parses the GET parameters and then serves the pdf as a download.

Download here

The jsp code is used here. Its work in IE8, Chrome and Firefox:

 <%@page session="false" contentType="text/html; charset=utf-8" import="java.io.IOException, java.io.InputStream, java.io.OutputStream, javax.servlet.ServletContext, javax.servlet.http.HttpServlet, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.io.File, java.io.FileInputStream" %> <% //Set the headers. response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment; filename=downloaded.pdf"); [pull the file path from the request parameters] File file = new File("[pdf path pulled from the requests parameters]"); FileInputStream fileIn = new FileInputStream(file); ServletOutputStream outstream = response.getOutputStream(); byte[] outputByte = new byte[40096]; while(fileIn.read(outputByte, 0, 40096) != -1) { outstream.write(outputByte, 0, 40096); } fileIn.close(); outstream.flush(); outstream.close(); %> 
+4
source share

Place the .htaccess file in the root folder with the following line:

 Header set Content-Disposition attachment 
0
source share

I just found this through google.

And I had a similar problem, but I still want to use the servlet (as I create the content).


However, the next line is all you need in the servlet.

 response.setHeader("Content-Disposition", "attachment; filename=downloadedData.json"); 
0
source share

All Articles