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.
Related
We have a form which once submitted a file is created and returned. I created a java method which does the post and a ok status is returned. However how am i able to download the file after the post?
Sorry for not being clear its driving me crazy. We have a business object which generates reports based on parameters sent to it. Once the form is filled in the browser a pop up comes up (save/open) file. What i want to do is create a java standalone program that will sit on my desktop so that when I run this programing (passing it my name and password and URL to post to, this is done already) it will download the file that is created on the server side. The problem is that I don't know where the file is stored (if it is stored) on the server or the name of the file. All i know is that on the browser we go to the form fill it in and the file is returned to the browser. So far the post is working.
When you are on the form in the browser (e.g. http://localhost/my/form) you should inspect the source of the page (IE is Menu View > Source ). In the source you should search for a form tag. This tag contains an action value like:
<form action="myaction.dhtml" method="...>
</form>
So the URL to request is http://localhost/myaction.dhtml and the servers response will be a "file". Good.
You may send the same request that does the browser from Java. To not code all that stuff again you may use a library like HttpComponents.
Probably your form is sending parameters too to the server (user name, password, etc). Look at the form components what parameters the server expect. Your URL may looks like this:
http://localhost/myaction.dhtml?name=Joe;pass=myPassWRD
You don't have to know where the file is stored, but you will need the correct URL that the server will use to take or generate the correct data and send to the client.
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
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.
i have got a situation now.
I need to develop a webpage where user can select a file to upload and before uploading the file to server i need to check first few lines of the file whether the data is valid or not and if the data is valid then upload the file, if not through an error message.
the file will be text file.
thanks,
Sandeep
HTML/Javascript does not offer a way of reading the contents of a local file. You must either upload it and check it in the server.
If you really want a client side check, you then must build a signed applet(or even ActiveX) to run in your webpage and handle the upload instead of using plain HTML.
You should perform your validation on the server side, right before you perform the upload.
Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading operation of the file.
Thanks.
That's not possible. The client needs to send (upload) the file content along the request body to the server side whenever you want to have the file content at the server side.
If you'd expect that you can solve this by passing only the file path around and use the usual java.io.File stuff and so on, then you're on the wrong track. Imagine that I am the client and I have a c:/passwords.txt, how would you as being the server at the other end of the network ever get its content by java.io.File?
I don't thnik this is possible. Browsers do not allow any file transfer from the client to the server without user interaction.
Tough, if you do not stick to IceFaces, it may be possible to achieve this by writing an applet, wich is granted the necessary permissions.