Where downloaded file name set? - java

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.

Related

sending bytes as pdf

EDIT: The data I wanna send is in an rds. I fetch that into an output stream and then try to send it over as a pdf to the user.
The file gets generated but on opening the browser shows 'Failed to load PDF document.'
I have read that setting the ContentType to "application/pdf" helps but it does not in my case. The code is given below
byte[] b = generateFileService
.getDeviceHumidityRecordByPeriod(deviceIdValue, parseUnixTimestamp(startTime), parseUnixTimestamp(endTime));
OutputStream output = response.getOutputStream();
response.setContentType("application/pdf");
response.addHeader("Content-disposition", "attachment;filename=test.pdf");
output.write(b);
output.close();
response.flushBuffer();
}
if I change the file name to test.csv and then use content type as txt/plain, it works perfectly and a csv file is written.
I used Apache PDFBox to write data into a pdf file. The page offset needs to be tracked to add pages dynamically. Then you can convert the pdf into bytes and send it to the client by specifying response.setContentType("application/pdf") and response.addHeader("Content-disposition", "attachment;filename=test.pdf")

how to specify the pdfbox.apache filename when downloading/exporting?

Just wondering is there any way to name the document after you specify the doc.name to a template
PDDocument doc = PDDocument.load(play.Play.application().resource("/templates/" + FileName));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
doc.save(byteArrayOutputStream);
doc.close();
therefore, when you download PDFBOX rendered file, the name of the pdf file can not be specified. is there any other way to do it?
I am not familiar with Play framework.
If you want to enable users to download the file and provide it a filename, then you need to set the HTTP header
Content-Disposition: attachment; filename=myfile.pdf
When the browser sees this header, the user will get a dialog box to save the file and will suggest the name to be myfile.pdf.

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.

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