I have an app which allows users to upload a file. User is sent to a preview page where they can download the file they just uploaded to sorta verify that things are correct. But for some reason the filename is not correct when it reaches the servlet, what could cause this?
$('a[id^=dl_link_]').click(function(e) {
e.preventDefault();
$('#dl_form input[name=file_name]').val($(this).text());
$('#dl_form input[name=uid]').val(upload.tempId);
$('#dl_form').submit();
});
When I add logs, I see that the file name is correct; ie "this is a test file.docx".
But when this data gets to the backend I get the following:
java.io.FileNotFoundException: /Users/yao/__TEMP__/upload_temp/1111/0gGNMY8PcAWEs3M/this�is�a�test�file.docx (No such file or directory)
The file path is constructed by combining parts together. The servlet receives the uid and the filename, everything else is from some other backend methods.
What could be the cause of this?
Maybe you need to call
encodeURIComponent()
on the file name. It'll convert the space chars to %20 and then be converted properly on the back end.
As I see the problem is related with the space character. This question might help you: accessing files with spaces in filename from java
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?
I'm Trying to do an android app which contains an URI of a JSON file . The JSON File URI contains some reserved characters in the link and hence when executing, the link is being automatically replaced with the encoded form of those characters. therefore Im not being able to retrieve the JSON file from the link.
The link when entered in Browser shows output but through program it does not show.The link after encoding when entered in a browser show nothing but a blank page.
Sorry I Cant post the Link here...( Characters which are shown in the URI are {?,_ etc..} present in the link of the file.
How shall I COUNTER this problem?
(How to use the Escape characters to resolve this problem?)
Using JQuery and Spring's #ModelAndView annotation for the controller.
I'm trying to code a process in which the user clicks an icon and if a certain criteria on the DB is met, a zip file will be produced on the server containing a bunch of files, then this zip file should be sent to the browser for saving.
If the criteria isn't met, then an error message should be sent to the browser telling there isn't any file to be created and produced.
However if I use JQuery' .post method, I can receive the error message (if that is the case) but never the zip binary file.
If I use a regular Href Link I can receive the file (if that is the case) but don't know how to receive the message when the file cannot be produced.
Is there an alternative or a standard way to do this?
Thanks for your support!
-Gabriel.
You should probably split your server-side method in two:
the first one validates the criteria. If unsuccessful, it notifies of an exception, otherwise it returns a URL to the method in next point
the second one actually returns the zip file
In your frontend, the code will look something like this:
$.post(urlToPoint1, data, function(response) {
if (response.success) {
// download the file using the url provided
// (pointing to method described in point 2)
window.location.href = response.url;
}
else {
alert('whatever');
}
});
We are in the process of converting over to using the XSLT compiler for page generation. I have a Xalan Java extention to exploit the CSSDK and capture some meta data we have stored in the Extended Attributes for output to the page. No problems in getting the EA's rendered to the output file.
The problem is that I don't know how to dynamically capture the file path and name of the output file.
So just as POC, I have the CSVPath hard coded to the output file in my Java extension. Here's a code sample:
CSSimpleFile sourceFile = (CSSimpleFile)client.getFile(new CSVPath("/some-path-to-the-output.jsp"));
Can someone point me in the CSSDK to where I could capture the output file?
I found the answer.
First, get or create your CSClient. You can use the examples provided in the cssdk/samples. I tweaked one so that I captured the CSClient in the method getClientForCurrentUser(). Watch out for SOAP vs Java connections. In development, I was using a SOAP connection and for the make_toolkit build, the Java connection was required for our purposes.
Check the following snippet. The request CSClient is captured in the static variable client.
CSSimpleFile sourceFile = (CSSimpleFile)client.getFile(new CSVPath(XSLTExtensionContext.getContext().getOutputDirectory().toString() + "/" + XSLTExtensionContext.getContext().getOutputFileName()));
Currently, I have a servlet act as web service.
When I pass in parameters using POST, it will return me an executable binary file (application/octet-stream). However, beside binary file, I would also like to get additional information (in text format) about this binary file.
Is it possible to achieve this by using only single POST request? But, how is it possible, to switch from application/octet-stream to text/plain within single POST response?
It is not possible to change the MIME type within a single response.
However, i think t is possible to put your additional information into the response header using the HttpServletResponse.addHeader method.
You could return a multipart MIME response (multipart/mixed; boundary=XXX instead of application/octet-stream) with the binary part encoded in Base64.
I'm not sure if the JavaMail API can be used to construct the content, but it's worth a look.
Within a single POST it isn't possible. But two ideas:
Show your text file as another web page that starts the download of your executable
Bundle both files into a zip archive