Jasper report pdf name problem - java

I am creating pdf report in spring mvc 3 using dynamic jasper report. I am setting these headers before writting report to stream.
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
response.setContentType("application/pdf");
Report is generated and display correctly in browser but it misses its name when I try to save it, I set name here fileName.
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
What could be the issue.

With attachment, the file will be served with the provided name properly. When inline, browsers seem to ignore filename, and usually give the servletname part of the URL as default name when saving the inline contents.
You could try mapping that URL to an appropriate filename, if that is suitable. For example, with <servlet-mapping>. I'm not familiar with spring mvc, so maybe there's an equivalent.
Here's a SO related question: Securly download file inside browser with correct filename
You may also find this link useful: Filename attribute for Inline Content-Disposition Meaningless?

Related

How to force the browser to use Application/Octet-stream header from a Spring Controller

I have a page which displays links to files which are stored as BLOBs in the database. In multiple browsers one of the links is displaying csv information inline instead of downloading as a CSV file. A user would be able to right click and download, but how can I enforce a download as the default browser action, as the content type and content-disposition do not seem to be sufficient. I am on an older version (3.2.4) of the Spring Framework.
Edit: If I do not set the headers all links are displayed inline.
Also, when the headers are set, there is an error reported in the Safari console for the links that download and are not displayed inline:
"Failed to load resource: Frame load interrupted"
//front end code
<td><a id="downloadFile" href="<c:url value='/test/file/errors/${fileError.id}'/>"
class=".icon-download">
<span class=".icon-download"></span>${fileError.filename}</a></td>
//Spring controller
#RequestMapping(value = "/test/file/errors/{id}", method = RequestMethod.GET)
public void downloadFile(#PathVariable("id") Long id,
HttpServletResponse response) {
InputError file = commonService.get(InputError.class, id);
try {
InputStream is = new ByteArrayInputStream(file.getFile());
IOUtils.copy(is, response.getOutputStream());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFilename() + "\"");
response.flushBuffer();
} catch (IOException ex) {
throw new RuntimeException("IOError writing file to output stream");
}
}
In case anyone else has the same problem, I solved this by setting the headers before the IOUtils.copy.
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFilename() + "\"");
IOUtils.copy(is, response.getOutputStream());
Have you tried using the download attribute introduce in html5 ?
<td><a id="downloadFile" href="<c:url value='/test/file/errors/${fileError.id}'/>"
class=".icon-download" download="filename_here.txt">
<span class=".icon-download"></span>${fileError.filename}</a></td>
As mentioned by Benoit, if you can use HTML 5 the download option would work.
However, despite it being somewhat nit-picky, your current answer should work. So in this case, have you tried using the appropriate casing for the MIME-type?
Content-Type="application/octet-stream"
Part of the value in using Spring's MediaType class is remove 'magic number' values like this from your code, and ensure odd behavior doesn't happen.
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

Where downloaded file name set?

I have some code to download pdf file like below way:
byte[] text = textContent.getText();
InputStream inputStream = new ByteArrayInputStream(text );
response.setContentType("application/pdf");
ServletResponseUtil.write(response, inputStream);
File has downloaded successfully with some name but where does that names come from?
You can specify the filename in the response header.
response.setContentType("application/pdf");
response.addProperty("Content-Disposition", "attachment; filename=" + myFilename);
The assumption here is that the resource is served during the resource phase of a portlet.
For full list of Content-Disposition options, see RFC 6266.
As long as there is no Content-Disposition header, the browser will derive the file name from the request URL.

Jasper report pdf has wrong name when downloaded in java

I have an application(web one) where the users can view jasper reports. The only problem is that when they decided to save the pdf to their local computers, the pdfs have incorrect filenames. In Chrome they are named download.pdf and in Firefox they are named something like sgsgjsg.pdf.
What can I so in order for the files to be saved with the correct name ?
Note that I set the header type like this:
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"%s\"" + exportFile.getName());
inputFileInputStream = new FileInputStream(fullPathFile);
outputFileOutputStream = response.getOutputStream();
Put inside header filename the value what you needed.
For instance:
response.setHeader("Content-Disposition", "attachment; filename="\"download.pdf\"");
It is should works same for all browsers.

Broken encoding in Excel report from Java

in my application user is able to download an excel report. When user downloads xls using FF then encoding is fine and umlauts are shown correct. If user does the same in IE and Chrome then umlauts in xls are broken.
Excel report is generated in a servlet using Apache POI v3.10.1 and filled with the same data every time. But the xls files downloaded in FF and IE (or Chrome) are different.
Any idea why?
BTW, I tried to set encoding in response header
response.setHeader("Content-Type", "application/vnd.ms-excel; charset=UTF-8");
but no success.
UPD 03.04. A piece of code from servlet:
OutputStream out = response.getOutputStream();
...
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls" + "\"");
...
workbook.write(out);
IE somehow cannot understand your response.setHeader("Content-Type", "application/vnd.ms-excel; charset=UTF-8");. Try this instead response.setContentType("application/vnd.ms-excel");. It works on my IE8 and FF35.

Make a link to download local file

In my project (Java SpringMVC3) I get an XLS file via HttpClient and I want that file to be downloaded like it's a real download. A popup window showing download dialog.
How can I do that?
Controller should copy the content of file to response object. Do not forget - controller function must return NULL. Below I show a working example from my application:
String filename = /* path to a file */
File file = new File(filename);
response.setContentType(new MimetypesFileTypeMap().getContentType(file));
response.setContentLength((int)file.length());
response.setHeader("content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
InputStream is = new FileInputStream(file);
FileCopyUtils.copy(is, response.getOutputStream());
return null;
Basically you need to implement a Controller that takes care of the download and specify the response's header-mime type. then you invoke that Controller from the view.
Here is a short example how to specify a header-mime type
HTTP Header Mime Type in Websphere Application Server 7

Categories