EDIT
I solved the issue itself by zipping the XML up and sending it out as application/zip. Still, the question remains why it does not work in plain XML.
I'm currently trying to let users of our website download an xml-file with so called "late responses", in case the automatic processing fails and their requests need to be looked at by a person. The site is basically just a simple GUI over a soap-webservice for the non-technical-inclined, but thats not that important for the issue.
When there are late responses, the site shows the total number and offers to download them, I'm currently archieving this via
// XML-content is aggregated and written to response outputstream
response.setCharacterEncoding("UTF-8");
response.setContentType(MediaType.APPLICATION_XML_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=" + "ergebnisprot_" + new DateTime().toString("ddMMyyyyHHmmSS") + ".xml");
response.flushBuffer();
This works well if only about one to five responses are aggregated. The browser gets a download-popup and everything goes the way it is supposed to go.
But with larger files, depending on the individual size, but usually from about 10 responses upward, this popup does not appear. Instead, the browser renders the xml itself, which is undesirable, since it's considered of no use to the user to not have the file on his harddrive, or to have to then copy it himself into a file.
Does anyone know why this behaviour happens? Did I miss something in my header configuration?
Related
So I am working on this project where I want to store an audio file in a LARGEBLOB on a database, the size of the file is limited to about 10MB, and be able to load the data through a java servlet that allows for playing of the media file.
Most of the sources I have been able to find suggests storing it locally, however, I want to avoid this solution based on the fact that I'd like to rebuild the website somewhere completely different and not have to rely on the folder structure to be the same.
The issues that I am encountering area mainly that the web browser misinterprets the binary data provided by the servlet. It manages to retrieve that it is an audio file of some sort, however; it is unable to determine the type of audio file, which leads me to believe that the servlet is either not providing enough data, or that I am not doing enough to instruct the web browser on how to play the file.
For example, if I have a file audio.mp3 which I have uploaded to the database into a table Tracks and stored in a column TrackFile. Assuming the query of selecting the right song from the table, what data would the servlet need to provide in order for the browser to play the file when accessing the servlet. Currently when I load the servlet, the browser seems to assume that the type is audio/mpeg instead of audio/mp3. The content currently delivered by the servlet also looks something like this:
response.setHeader("Content-Type", this.getServletContext().getMimeType(t.getTrackName() + '.' + t.getFileType()));
response.setHeader("Content-Length", String.valueOf(t.getTrackData().length));
response.setHeader("Content-Disposition", "inline; filename=\"" + t.getTrackName() + '.' + t.getFileType() + "\"");
response.getOutputStream().write(t.getTrackData());
where t is an object which holds all the data which can be retrieved from the database table about a specific track. The method getTrackData() returns a byte[] with contents of the column TrackFile in it. The source of this method is: link, although I adapted it in order to make it work with audio files, although it doesn't.
Are there any obvious things that I should have caught onto based on the fact that I can't get it to play back the file or is what I want to achieve generally impossible so to say?
While uploading doc file(example test.doc) to server(unix machine), I am using apache commons jar which gives me FormFile instance at server side which is having all the data in byte array form.
When I write the same byte array to response output stream and send it to browser to download the same file, weird content is shown. I get one pop up to select encoding in which i would like to see the data and weird data is shown in that doc.The content type is set as follows :
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename=test.doc");
I think that while writing data to output stream, meta data related to doc file is also written which causes this issue.
Is there anything specific for doc or docx file formats, which needs to be done so file is in proper format and i can see correct data which i uploaded or I am missing something?
Any help would be appreciated.
Thanks in Advance.
Let me know if more info is required.
There's a known issue in Microsoft which provide workaround for the
Encoding Pop Up
It may not be a fix for your problem because I have not run any test around. But to check the correct mime types please refer to this link:
https://technet.microsoft.com/en-us/library/ee309278(office.12).aspx
Updated:
You can use response type as ArrayBuffer and set the content as Blob.
Blob([response], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'});
Or this could work
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="+ fileName);
I want to be able to display on the browser a file with wicket. I have a form witch takes the id and storage area of the document, then by pressing the submit button, I would like to display the file on the browser if it's possible else download it.I get the file as a byte [], it's compulsory, I cannot have it another way. I searched a lot for some answers but the fact that I'am using wicket 6.8.0 is handicapand because every solution that I find uses some obsolete methods (for the 6.8.0 version).
My question is: Is there any one who can help me do this without changing the wicket version (I cannot change it). The solution doesn't have to be in wicket.
Sorry if my English is incorrect
Thank you in advance
I found it. For persons who want to do this with wicket 6.8.0, it's the solution. It's quite simple though. Here's the code:
WebResponse fileResponse = (WebResponse) getRequestCycle().getResponse();
fileResponse.setContentType(mimeType);
fileResponse.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
fileResponse.write(myByteFile);
getRequestCycle().setResponse(fileResponse);
I get the file type by using the Tika API.
I need to read several megabytes (raw text strings) out of my GAE Datastore and then write them all to a new PDF file, and then make the PDF file available for the user to download.
I am well aware of the sandbox restrictions that prevent you from writing to the file system. I am wondering if there is a crafty way of creating a PDF in-memory (or a combo of memory and the blobstore) and then storing it somehow so that the client-side (browser) can actually pull it down as a file and save it locally.
This is probably a huge stretch, but my only other option is to farm this task out to a non-GAE server, which I would like to avoid at all cost, even if it takes a lot of extra development on my end. Thanks in advance.
You can definitely achieve your use case using GAE itself. Here are the steps that you should follow at a high level:
Download the excellent iText library, which is a Java library to work with PDFs. First build out your Java code to generate the PDF content. Check out various examples at : http://itextpdf.com/book/toc.php
Since you cannot write to a file directly, you need to generate your PDF content in bytes and then write a Servlet which will act as a Download Servlet. The Servlet will use the Response object to open a stream, manipulate the Mime Headers (filename, filetype) and write the PDF contents to the stream. A browser will automatically present a download option when you do that.
Your Download Servlet will have high level code that looks like this:
public class DownloadPDF extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//Extract some request parameters, fetch your data and generate your document
String fileName = "<SomeFileName>.pdf";
res.setContentType("application/pdf");
res.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
writePDF(<SomeObjectData>, res.getOutputStream());
}
}
}
Remember the writePDF method above is your own method, where you use iText libraries Document and other classes to generate the data and write it ot the outputstream that you have passed in the second parameter.
While I'm not aware of the PDF generation on Google App Engine and especially in Java, but once you have it you can definitely store it and later serve it.
I suppose the generation of the PDF will take more than 30 seconds so you will have to consider using Task Queue Java API for this process.
After you have the file in memory you can simply write it to the Blobstore and later serve it as a regular blob. In the overview you will find a fully functional example on how to upload, write and serve your binary data (blobs) on Google App Engine.
I found a couple of solutions by googling. Please note that I have not actually tried these libraries, but hopefully they will be of help.
PDFJet (commercial)
Write a Google Drive document and export to PDF
In a servlet, I want to read an EML from my database and serving it to the client with a "download file" UI. When I specify the Content-Length header, the download takes minutes to start. When I don't, everything works well, but I do want to set that header :) What am I missing?
// part is javax.mail.Part
response.setHeader("Content-Disposition", "attachment" + filename);
response.setContentType(mime);
response.setContentLength(part.getSize()); // This line causes the problem
IOUtils.copy(part.getInputStream(), out);
Just a guess - maybe the file has to be fetched from DB to get its size ? Save the size to separate column and serve the value from there. Also working with the file-in-DB through java.sql.Blob should work.
Unfortunately in your sample there is no info where you take the part object from.