How to implement file download with Spring MVC in Java? - java

There is a lot upload examples, but I couldn't find any for download. I'm using Spring 3.0 and I need to generate some text buffer and then let user download it as a file. (There is no static resource.)
So far I've tried to use #ResponseBody and writing to response output stream. In both cases browser display content of the 'file' instead of showing Save dialog box. How do I make browser display a dialog box?
Thanks.

You have to set the Content-Disposition header at your action (and not only the response body):
Content-Disposition: attachment; filename=your-file.pdf

Related

how to process file save prompt using java code

From a form in JSP I'm doing HTTP POST to a server and in return the server sends a text file to save, which results in browser opening "Save as" dialog. I want to capture the data stream through code instead of requiring the user to 'save the file'. Basically, I need to consume the content of file through java code and this should be transparent to the user.
I tried reading the request inputstream, but there is no content in it.
Maybe I misunderstood you, but I think you need a java.applet.Applet. In that case, you need to use java.net.URLConnection to do the POST request. Next, save the stream to file.

Parsing multi-form request (iframe)

I have a httpservice on my android device. So
I send a single file via XMLHttpRequest, if possible, or iframe in every other case.
If I use Google Chrome it works fine and I have no questions, because it uses XMLHttpRequest to send file.
But when I use IE or Opera which send file via iframe I get a problem, because I receive request like below:
------------Gg3pxYwLKOqlVLkQwq3Hyn
Content-Disposition: form-data; name="test"; filename="Android.png"
Content-Type: image/png
‰PNG
some symbols ....
.......................
------------Gg3pxYwLKOqlVLkQwq3Hyn
I want to parse it and take filename and file content as binary or base64 without header and footer. Can I parse it by Android's instruments or should use 3rd part library? Please, help.
I've found solution: http://commons.apache.org/fileupload/

How do I use a GWT RPC to save a file on the client side?

I'm looking for specific code on how to send a file from the server-side of a GWT application so that the client-side user can save it on his machine. Currently, the app allows the user to upload a file, reads it, and puts certain values from the file into editable text boxes. When the user hits the "save as" button, it collects that edited data, puts it back into the file string, and sends that string to the server, where I want it to be put into a file and pushed back to the user on the client side, so that they can save it to their machine. How exactly do I accomplish that?
Sorry if this seems like an obvious thing, but I'm relatively new to GWT and java in general. Thanks!
I think that you want the way for download a file using content-type from server using GWT.
The easiest way that I have found is create a iFrame :
import com.google.gwt.user.client.ui.NamedFramerdddccvc
...
NamedFrame iframe = new NamedFrame(frameName);
iframe.setVisible(false);
parent.addChild(iframe);
iframe.setUrl(url);
It's important that the url from the server return a page with content type "text/plain" or using the valid requested.
What you can do is create a servlet, that generates the text as content and set the matching mimetype for the content. In you app you can call this servlet via the by Fernando suggested IFrame method.
There are many suggestions here on Stackoverflow on how to do it. Search for [java] file download serlvet and you will find lots of examples/guidelines on how to do this.

Using JSP to download a file

I am currently trying to use JSP to build some small litle apps and have not got stuck on something, downloading files from a webserver. I just cant seem to work out how I should go about this task.
Are there any JSP developers here who know to go about this and could point me in the right direction?
If the resource is static, just put it in the public webcontent (there where your JSP/HTML/CSS/JS/etc files also are) and include a link to it in your JSP.
download
The servletcontainer will worry about setting the right HTTP response headers.
If the resource is dynamic, create a servlet which obtains an InputStream of the content somehow (new FileInputStream, resultSet.getBinaryStream(), etc..etc..) and writes it to the OutputStream of the response along at least the Content-Type and Content-Disposition response headers. Finally just link to that servlet in your JSP.
download
You can find a basic example in this article.
The Content-Type header informs the client about the content type of the file so that it knows what application it should use to open it. The Content-Disposition header informs the client what to do with it, displaying it inline or saving as attachment.

How to prompt a streamed file for downloading in Java / Tomcat

I'm creating a java web application runing on a Tomcat server.
One of the functions fill in a StringBuffer variable with data.
At the end, I would like to propose the user to download the generated content packaged in a text file. This without having to store the file on the server.
I've been searching for a code snippet but couldn't find anything corresponding ...
I hope I've been clear enough on my problem.
Thanks in advance,
See Making A Download Servlet
Don't forget to add the servlet to your web.xml.
You have to send a content-type along with the response, so that the browser knows what to do with the body of the response.
Normal text has the content-type text/plain, html is text/html. Images are image/gif and so on. For an unknown mime type you normally set "application/octet", which afaik every browser treats as a download. But I recommend to use the propery content type, so the browser might start a matching application to handle the content (e.g. Office for Documents or XML Editor for XML Files ..)
To send a filename along, which the browser suggests for saving, use the following header (example):
Content-Disposition: attachment; filename="downloaded.pdf"
For sending custom headers, use the setHeader() method in the response object.

Categories

Resources