Jasper report pdf has wrong name when downloaded in java - 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.

Related

Excel download not working after populating the values in java

In my application using spring and angularjs and java, On clicking a button ,there is an ajax call which fetches the data from db and the data needs to be written to an excel file and the same file needs to be downloaded in the browser itself. I am attaching the code snippet for the same .
Now the problem is, even though the data is being fetched and I am able to bind it as worksheet using poi , the excel file never comes in the browser as download.
kindly help me in finding a right solution. Thank You.
fileName.append(Calendar.get(Calendar.SECOND));
fileName.append(oCalendar.get(Calendar.MILLISECOND));
Sheet sheet = workbook.createSheet("Transaction" + fileName.toString());
CellStyle style = workbook.createCellStyle();
style.setFont(font);
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Name");
header.createCell(1).setCellValue("ACC");
header.createCell(2).setCellValue("Date");
header.createCell(0).setCellValue("TRANSACTION_RECONCILIATION_IDENTIFIER");
header.createCell(1).setCellValue("ORIGINAL_RECONCILIATION_IDENTIFIER");
header.createCell(2).setCellValue("STR_TRANSACTION_Date");
response.setHeader("Content-Disposition", "attachment; filename=\"TransactionDetails.xls\"");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
After writing the output stream you should use
response.flushBuffer();
Looks like you're missing the contentType definition. Before calling the write() method try setting the contentType like this:
response.setContentType("application/octet-stream");
This should tell the browser that a download is expected from the server. A relevant question was asked here: response.setContentType("APPLICATION/OCTET-STREAM")
In my code I write like this:
response.setContentType("application/octet-stream");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition",
"attachment; filename=TransactionDetails.xls");
workbook.write(response.getOutputStream());
Hope this helps

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.

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

Jasper report pdf name problem

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?

Categories