How to save/open files on server with GWT - java

I have a BIRT report that I've created in GWT and I'd like to save it to the server and then open it back up. I feel like hardcoding the url is the wrong route to go (ie C:/files/foo/foo.html) but I'm not having much luck figuring out another way of doing it. I tried using GWT.getModuleBaseForStaticFiles() but if i use that I get an unsatisfied link error in BIRT. What can I use to save/open a file, what is considered the best practice? Thanks.
edit: bad wording, the BIRT report is generated server-side. I'd like to be able to save it server side and then open it up in a new browser window (using window.open I'd imagine?). Nothing is uploaded from the client side.

If your BIRT report is a static file (Not generated during the runtime of your application) You can create a simple Servlet that read the file and copy the data to the OutputStream of the response. If it's generated at the runtime, the client will request it to the server, the server will generate it and return the URL location of the report. The client can open the report with that URL.
Example:
If your BIRT is located under foo/foo.bar you can map a servlet as /foo/*. After mapping it the user will request the url www.yourselver.com/foo/foo.bar The servlet at the doGet method can read the file and stream the content back to the client.

Related

java - send a file to client by file URL without download on the server

In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.

How to export excel file in GWT?

My requirement is to export an excel from GWT application on click of export button. In my case There is no need to send data from client to server as I can directly obtain data from database(same data is being populated on the client that is being exported).
All I tried to send a RPC call from Client site . but I am not able to figure out how to export excel on receiving the RPC call at server site.
This is certainly possilbe 100% client side.
You can use an Excel JavaScript builder like https://github.com/stephenliberty/excel-builder.js and call it from GWT with jsni or jsinterop.
Later you can save the file by
For Internet explorer for example filesaver.js https://github.com/eligrey/FileSaver.js/ in the same way.
For other browsers using HTML5 downloadv (using FileDownloadBuilder.createFileDownload().generateTextDownloadLink()) from https://github.com/akjava/html5gwt
Note for the generating an Excem file you could also just create an html file with table tags and save it as .xls, it with filesaver.js.
I use the html solution and filesaver.js/html5 download in my project and this works without a problem, and without the need for any serverside code, or any plugin on the client. 100% HTML5/javascript.
There is no way generating a excel file on the client site with native GWT without adding a third party product. (see knarf answer below)
I personally prefer this solution:
open an new window on the client
use a url, that triggers a servlet on the server
let the server generate the excel file
save the newly generated file inside the webspace
return the url to the excel file to the client
This works for me.

java post and download file

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.

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.

Categories

Resources