Specify Content-Length while serving a file from the database - java

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.

Related

Streaming audio file directly from blob

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?

ms word file download issue in servlet from unix server to windows

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);

python cgi http response adds extra byte to content

I have been using a python cgi script to get files from a database everything is working fine but for some reason there seems to be an extra byte added to the file. On the database the size is 10,265 bytes but in the http response the Content-Length is 10,266 the problem seems to be from the http response itself. The problem is the files served are .jar and are being used by a java applcation which then isnt able to load them with the class loader due to this extra byte. The snippet used to serve the download from the server is :
def printFileHeader():
print 'Content-Type: text/plain;charset=utf-8'
print
def downloadAddon(addon_id):
dbConn = sqlite3.connect("addons.db")
dbCursor = dbConn.cursor()
dbCursor.execute("SELECT addon_file FROM uploaded WHERE id="+addon_id)
blobl = dbCursor.fetchone()
blobl = blobl[0]
printFileHeader()
print blobl
the downloadAddon() function is then called with the requested id but no matter where I fetch the file from (blob in database or direct file) the http response always has that extra byte in the content even though server side the file is ok. Any help is welcome.
ps. I know the header is not a proper file header but I put it this way for testing purposes.
I managed to "fix" the issue by providing the content length of the file in the header like so the code now looks like this :
def printFileHeader(size):
print("Content-Disposition: attachment; filename=addon.jar")
print("Content-Type: application/octet-stream")
print("Content-Length: "+str(size))
print
def downloadAddon(addon_id):
dbCursor.execute("SELECT addon_file FROM uploaded WHERE id="+addon_id)
blobl = dbCursor.fetchone()
blobl = blobl[0]
printFileHeader(len(blobl))
print(blobl)
this solves the issue but I still dont understand why so any explanations are still welcome. Also while checking the response before and after the fix here are the last 6 bytes of the file :
Before (with extra byte) : AAAAAK
After : AAAAA=
Any explanation as to why is appreciated

Trying to force download of xml file fails in larger files

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?

Apache James Mail Server - Save attachments on filesystem

I'm using Apache James version 3.0-beta1 and I would like to know if there is a way to save separately the body of the e-mails from the attachments. Right now both of them are saved inside the DB, that leads to a noticeable increase in the table size due to the fact that all the attachments are saved inside the MAIL_BYTES column in the shape of a byte stream.
Is there a way to move outside the DB the attachments and leave inside the DB only the body of the emails? On the long run this default behaviour will make my DB collapse.
You may simply write a mailet to get the attachments of the mail and then save them to a specific folder in your filesystem. To be more specific, in mailet, get the MimeMessage from org.apache.mailet.Mail, then use it to check if there are any attachments by using getFileName() method. This method returns file names if Disposition and ContentType headers are not null . If the result is not null, then that means in than bodyPart you have a file attached.Then using getInputStream() you can save it to anywhere you want.

Categories