I’m trying to download a file and, dynamically, I’m setting the file name. But while downloading the file I’m receiving the file name as "3823720b-3d63-4e2c-8fc2-6abdc34b5cd8.docx" But I’m passing my file name as "Sample.docx"
if (Files.exists(file)) {
response.setContentType(existingDocuments.getContentType());
response.addHeader("Content-Disposition", "attachment; filename=" + existingDocuments.getFileName());
Files.copy(file, response.getOutputStream());
response.getOutputStream().flush();
}
this is working for me
response.header("content-disposition", "attachment; filename=\"" + fileNameDisplay + "\"");
you might also need to add
response.setContentType("application/octet-stream");
Related
Internet Explorer 11 is ignoring my filename. When I try to "Save As" the archive name don't match with what I put on "filename=". Its getting from URL. Why others browsers works just fine? What do I have to do?
String szFilename = "archive.pdf";
response.setContentType("application/pdf");
response.setContentLength(pdfBytes.length - nBufferStart);
response.setHeader("Content-Disposition", "inline; filename=" + szFilename);
String szFilename = "archive.pdf";
response.setContentType("application/pdf");
response.setContentLength(pdfBytes.length - nBufferStart);
response.setHeader("Content-Disposition", "inline; filename=" + szFilename);
I have created a sample using the similar code, it seems that your code works well in IE 11. Please try to put the file name in double quotes, like this(it also works well in IE11 browser):
response.setHeader("Content-Disposition", "inline; filename=\"" + szFilename + "\"");
I have just created small jsp file in which I am downloading a jar file from weblogic server that is installed in my computer through eclipse using the following piece of code which I grabbed from net. But when I try the jar file, it says it is corrupt. The sizes of original and downloaded jar files are identical. I can download and open text files using the code. What is wrong in the code?
<%
String filename = "Words2.jar";
String filepath = "C:\\Users\\OD00259\\Desktop\\";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\"" +
filename + "\"");
java.io.FileInputStream fileInputStream=new java.io.FileInputStream
(filepath + filename);
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
%>
Try Change the content-type to application/java-archive. Also set the content-length header to include the size of the file.
Also, I would recommend you to use some checksum tools like md5sum for verifying the file downloads comparison instead of relying on the size to compare.
There is an interesting error on a page. I wonder if you have any idea about it.
In order to download a file, we write the file to responseStream but if file is less than 8,3KB, we get file not found error although file is there.
If i increase the file size (using some dummy values), it works
I suspect it has something to do with the contentLength of response but it seems fine to me
file = new File(ef.getPath());
response.setContentLength((int) file.length());
fileIs = new FileInputStream(file);
FileCopyUtils.copy(fileIs, response.getOutputStream());
Please help me if you have any idea?
How try with adding Content-Disposition header and setContentType()
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
String mimeType = new MimetypesFileTypeMap().getContentType(fileName);
response.setContentType(mimetype);
I have the following code on Spring Controller
#RequestMapping(value = "/download", method = RequestMethod.POST)
public void downloadActive(#RequestParam(value = "type") String offerType, HttpServletResponse response,
HttpSession session) throws Exception {
StringBuilder b = new StringBuilder();.
.
.
response.addHeader("Content-Disposition: ", "attachment; filename=my" + offerType + "Offers.csv");
response.getWriter().write(b.toString());
..
}
This code get executed when a download button from EXt Js is clicked. On chrome, it directly downloads the file as a *.csv format and when the user opens it, it will use Excel automatically to open the file, if they already have installed and this is the right behavior I wanted to have.
However, on firefox, it prompts a save as window with open with and save options. If I use open with option, it tells me the filename is filename.csv but the type is chrome htm file. I haven't mentioned htm anywhere in my code and I dont know why its trying to open it as htm instead of csv file. Once it tries to open it, it brings excel application and the contents are in a bad format.
Is there a way to avoid this problem on firefox ?
Thanks
Adding double quotes (") for the file name fixes the problem.
response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "");
Even if I am not sure why it worked, changing the addHeader Method to setHeader fixed the issue.
response.setHeader("Content-Disposition: ", "attachment; filename=my" + offerType + "Offers.csv");
PrintWriter printWriter = resourceResponse.getWriter();
java.util.Date date = new java.util.Date();
resourceResponse.setContentType("text/csv");
resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL,
"max-age=0, must-revalidate");
resourceResponse.setProperty(
HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + "export_build" + "_"
+ date.getTime() + ".csv");
printWriter.write(stringBuilder.toString());
Make sure to write the content after printing the header to ensure content is packaged in a file properly.
We have the below requirement.
We will have to create an excel/pdf report and then download it on click of a button in a java web application. The pdf/excel file is dynamically created using application data.
We should not create any physical file on the server.
How do we go about this? Are there any streams through which I can read and write in the same go without having to close in between.
You could use memory-based streams (such as ByteArrayInputStream and ByteArrayOutputStream) and use the same underlying byte buffer to address the read/write in the same go part of the question.
As others have pointed out, you can just write directly to the output stream of the response.
Look at ServletResponse.getOutputStream().
You need to write to this stream from the one created by your report API. Don't forget to set the proper content-type using setContentType() method of the same class.
Here you can find how you can do it with jxl API and it may help you also.
How do I output an Excel file from a Servlet?
Whatever PDF or Excel API you are using to generate the files, you should lookup the constructor or method which takes an OutputStream to write the generated PDF/Excel content to. You should just feed it with response.getOutputStream() instead of FileOutputStream.
For example, iText for PDFs:
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
// ...
And Apache POI for Excel:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...
Have a Servlet serve the pdf/excel file as a byte array.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] bytes = null; // get this from somewhere in your app
String fileName = "filename.pdf"; // whatever you wish to name the file
ServletOutputStream out = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
out.write(bytes);
out.flush();
}
MIME type for MS Excel files would be application/vnd.ms-excel.